In Python, a variable is a name that refers to a value stored in the computer’s memory. It acts as a container to store data of various types. You can think of variables as labeled storage locations that hold values.
Here’s an example that demonstrates the use of variables in Python:
python code:
# Assigning values to variables
name = "John"
age = 25
height = 1.75
is_student = True
# Printing the values stored in variables
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Is Student:", is_student)
In this example, we declare and assign values to four different variables: name, age, height, and is_student. The name variable stores a string value “John”, the age variable stores an integer value 25, the height variable stores a float value 1.75, and the is_student variable stores a boolean value True.
We then use the print() function to output the values stored in these variables.
The output will be:
output:
Name: John
Age: 25
Height: 1.75
Is Student: True
Variables in Python are dynamically typed, meaning you don’t need to explicitly declare their types. The type of a variable is determined based on the value assigned to it. You can assign new values to variables, and their types can change dynamically during the execution of a program.