Introduction to Variables in Python:

Variables in programming are containers that can store different types of data such as numbers, text, and boolean values. In Python, variables are dynamically typed, meaning you don't need to specify the data type of a variable when you declare it. The data type is automatically determined based on the value assigned to the variable.

Declaring a variable in Python is very simple, you just need to give it a name and assign a value to it. The name of a variable can consist of letters, digits, and underscores, but cannot start with a digit.

Let's look at some examples of declaring variables in Python:

# declaring a variable and assigning it an integer value
x = 5

# declaring a variable and assigning it a float value
y = 3.14

# declaring a variable and assigning it a string value
name = "John Doe"

# declaring a variable and assigning it a boolean value
is_student = True

Now that we've seen some examples of declaring variables in Python, let's move on to some code examples.

Easy Example:

In this example, we will declare a variable name and assign it a value of "Alice". We will then print out the value of the variable using the print() function.

# declaring a variable and assigning it a string value
name = "Alice"

# printing the value of the variable
print(name)

Output:

Alice

Medium Example:

In this example, we will declare two variables x and y and assign them integer values of 5 and 10 respectively. We will then add these two variables together and assign the result to a new variable z. Finally, we will print out the value of z.

# declaring two variables and assigning them integer values
x = 5
y = 10

# adding the two variables together and assigning the result to a new variable
z = x + y

# printing the value of the new variable
print(z)

Output:

Copy code
15

Hard Example:

In this example, we will create a program that calculates the area of a rectangle using user input. We will prompt the user to enter the length and width of the rectangle, and then calculate the area by multiplying these two values together. We will then print out the result.

# prompting the user to enter the length and width of the rectangle
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))

# calculating the area of the rectangle
area = length * width

# printing out the area of the rectangle
print("The area of the rectangle is", area)

Output (example):