• The definitions of the variables and data types
  • The relationships between the variable and data type
  • Arithmetic operations

1 Variables and Data Types

Variables are used to store data values in memory. When you create a variable, you are assigning a name to a specific memory location where the data is stored.

Variable names usually start with a lowercase letter or an underscore (_), followed by letters, numbers, or underscores. Note that variable names are case-sensitive.

There are some types of data in “Python” language.

# Example of variables and data types
num1 = 10  # Integer variable
num2 = 3.14  # Float variable
name = "Alice"  # String variable
is_student = True  # Boolean variable

You can check the type of a variable using type() function:

# Print the type of each variable
print(type(num1))  # <class 'int'>
print(type(num2))  # <class 'float'>
print(type(name))  # <class 'str'>
print(type(is_student))  # <class 'bool'>
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>

You can assign different types of data to a variable during its lifetime. For example, you can assign an integer value, then assign a string value to the same variable later on.

# Define a variable called age and assign it a value of 25
age = 25

# Print the value of the variable age
print(age)

# Convert age to string
age = str(age)

# Check the type of age
print(type(age))
25
<class 'str'>

2 Basic Arithmetic Operations

You can conduct arithmetic operations in “Python” programming. Here is the simple addition.

# Perform basic arithmetic operations
num1 = 10  # Assigning the value 10 to the variable num1
num2 = 5  # Assigning the value 5 to the variable num2

# Adding num1 and num2 and storing the result in sum_result
sum_result = num1 + num2

# Printing the result of the addition operation
print(f"The sum of num1 and num2 is: {sum_result}")
The sum of num1 and num2 is: 15

NOTE

f"" is called the f-string in “Python” programming. Using f-string, you can show the value of a variable in a string.

There are other arithmetic operations:

Subtraction:

# Subtraction operation in Python
num1 = 10
num2 = 5
difference_result = num1 - num2
print(f"The difference between {num1} and {num2} is: {difference_result}")
The difference between 10 and 5 is: 5

Multiplication:

# Multiplication operation in Python
num1 = 10
num2 = 5
product_result = num1 * num2
print(f"The product of {num1} and {num2} is: {product_result}")
The product of 10 and 5 is: 50

Division:

# Division operation in Python
num1 = 10
num2 = 5
division_result = num1 / num2
print(f"The division of {num1} by {num2} is: {division_result}")
The division of 10 by 5 is: 2.0

Modulus (Remainder):

# Modulus (Remainder) operation in Python
num1 = 10
num2 = 3
remainder_result = num1 % num2
print(f"The remainder when {num1} is divided by {num2} is: {remainder_result}")
The remainder when 10 is divided by 3 is: 1

This operation is calculating the 1 in the expression below:

10 = 3 \times 3 + 1

Power:

# Power operation in Python
num = 2
exponent = 3
result = num ** exponent
print(f"{num} to the power of {exponent} is: {result}")
2 to the power of 3 is: 8

3 Exercise