You are currently viewing Basic syntax and data types in Python
Python | Basic syntax & data types

Basic syntax and data types in Python

Syntax:

Python code is typically written in a text editor or integrated development environment (IDE) and saved as a file with a .py extension. The basic syntax of Python is designed to be easy to read and understand, with indentation used to denote code blocks instead of braces or other symbols.

Here’s an example of a simple Python script:

# This is a comment

# This is a variable assignment
name = "Alice"

# This is a print statement
print("Hello, " + name + "!")

This code assigns a string value to a variable called “name” and uses the print function to display a message that includes the value of the variable.

Data Types:

Python supports several built-in data types, including integers, floating-point numbers, strings, booleans, and lists. Here are some examples and sample code for each data type:

1. Integers:

Integers are whole numbers without decimal points, such as 1, 2, 3, etc.

# This is an integer assignment
age = 25

# This is an integer calculation
years_left = 65 - age

# This is a print statement with integer values
print("You have " + str(years_left) + " years until retirement.")

2. Floating-Point Numbers:

Floating-point numbers are numbers with decimal points, such as 1.0, 2.5, 3.14159, etc.

# This is a floating-point assignment
pi = 3.14159

# This is a floating-point calculation
circumference = 2 * pi * 5

# This is a print statement with floating-point values
print("The circumference of a circle with radius 5 is " + str(circumference) + ".")

3. Strings:

Strings are sequences of characters, such as “hello”, “world”, “Python”, etc.

# This is a string assignment
greeting = "Hello, World!"

# This is a string concatenation
message = greeting + " My name is " + name + "."

# This is a print statement with string values
print(message)

4. Booleans:

Booleans are values that represent either true or false, such as True or False.

# This is a boolean assignment
is_student = True

# This is a boolean condition
if is_student:
    print("You qualify for a student discount!")
else:
    print("Sorry, you do not qualify for a student discount.")

5. Lists:

Lists are ordered collections of values, such as [1, 2, 3], [“apple”, “banana”, “cherry”], etc.

# This is a list assignment
fruits = ["apple", "banana", "cherry"]

# This is a list indexing
second_fruit = fruits[1]

# This is a list append
fruits.append("orange")

# This is a print statement with list values
print("My favorite fruits are: " + ", ".join(fruits) + ".")

These are just a few examples of the basic syntax and data types in Python. There are many more data types and advanced features available in Python, such as dictionaries, tuples, sets, functions, and classes, which can be used to write more complex and powerful programs.

6. Dictionaries:

Dictionaries are collections of key-value pairs, such as {“name”: “Alice”, “age”: 25, “city”: “New York”}.

# This is a dictionary assignment
person = {"name": "Alice", "age": 25, "city": "New York"}

# This is a dictionary lookup
person_age = person["age"]

# This is a dictionary update
person["city"] = "Los Angeles"

# This is a print statement with dictionary values
print(person)

7. Tuples:

Tuples are similar to lists, but they are immutable (cannot be changed after creation), such as (1, 2, 3), (“apple”, “banana”, “cherry”), etc.

# This is a tuple assignment
coordinates = (3, 4)

# This is a tuple unpacking
x, y = coordinates

# This is a print statement with tuple values
print("The coordinates are (" + str(x) + ", " + str(y) + ").")

8. Sets:

Sets are unordered collections of unique values, such as {1, 2, 3}, {“apple”, “banana”, “cherry”}, etc.

# This is a set assignment
numbers = {1, 2, 3, 4, 5}

# This is a set intersection
even_numbers = {2, 4, 6, 8, 10}
common_numbers = numbers.intersection(even_numbers)

# This is a print statement with set values
print("The common even numbers are: " + ", ".join(str(num) for num in common_numbers) + ".")

These examples demonstrate how Python supports a variety of data types and features that can be used to solve a wide range of programming problems. As you continue to learn Python, you’ll discover many more advanced data types and features that can help you write more efficient, powerful, and elegant code.

9. Strings:

Strings are collections of characters, such as “hello”, “world”, “Python”, etc.

# This is a string assignment
message = "Hello, world!"

# This is a string concatenation
name = "Alice"
greeting = "Hello, " + name + "!"

# This is a string interpolation
age = 25
message = "My name is {0} and I am {1} years old.".format(name, age)

# This is a print statement with string values
print(message)

10. Lists:

Lists are ordered collections of values, such as [1, 2, 3], [“apple”, “banana”, “cherry”], etc.

# This is a list assignment
numbers = [1, 2, 3, 4, 5]

# This is a list slicing
first_three_numbers = numbers[:3]
last_two_numbers = numbers[-2:]

# This is a list comprehension
squared_numbers = [num ** 2 for num in numbers]

# This is a print statement with list values
print("The squared numbers are: " + ", ".join(str(num) for num in squared_numbers) + ".")

11. Booleans:

Booleans are True or False values, which can be used to control program flow and logic, such as True, False, 1 == 1, 2 > 3, etc.

# This is a boolean assignment
is_raining = True

# This is a boolean comparison
is_sunny = False
is_nice_weather = is_sunny or not is_raining

# This is a boolean expression
temperature = 25
is_hot_weather = temperature >= 30 and is_sunny

# This is a print statement with boolean values
print("It is nice weather: " + str(is_nice_weather) + ", and it is hot weather: " + str(is_hot_weather) + ".")

These examples demonstrate how Python supports a variety of data types and features that can be used to solve a wide range of programming problems. As you continue to learn Python, you’ll discover many more advanced data types and features that can help you write more efficient, powerful, and elegant code.

Leave a Reply