Let’s learn about variables in Python.
Imagine a variable as a container that can hold a value. Think of it like a labeled box where you can put something inside. In Python, these “boxes” can hold different kinds of information, like numbers, text, or even more complex things.
Here’s a simple example of how you can create and use a variable in Python:
# We are creating a variable named 'message' and storing the text "Hello, Python!" in it.
message = "Hello, Python!"
# Now, we can print the value stored in the 'message' variable.
print(message)
# We can also change the value stored in the variable later.
message = "Learning Python is fun!"
print(message)
# Let's try with a number.
number_of_students = 25
print(number_of_students)
If you run this code, you will see the following output:
Hello, Python!
Learning Python is fun!
25
Explanation:
message = "Hello, Python!": This line does two things:- It creates a variable named
message. - It assigns the string (text) value
"Hello, Python!"to this variable. The=symbol is used for assignment.
- It creates a variable named
print(message): This line uses theprint()function to display the value that is currently stored in themessagevariable on the screen.message = "Learning Python is fun!": Here, we are changing the value of themessagevariable to a new string:"Learning Python is fun!". When you assign a new value to a variable, the old value is replaced.print(message): This line will now print the updated value of themessagevariable.number_of_students = 25: This shows that variables can also hold numbers. Here, we create a variable namednumber_of_studentsand assign the integer value25to it.
Key takeaways about variables:
- Names: You give variables names so you can refer to them later in your code. Variable names should be descriptive and follow certain rules (e.g., they cannot start with a number and cannot contain spaces).
- Assignment: The
=symbol is used to assign a value to a variable. - Data Types: Variables can hold different types of data, such as strings (text), integers (whole numbers), and more. Python can often figure out the data type automatically.
- Dynamic Typing: In Python, you don’t need to explicitly declare the data type of a variable. It’s determined at the time of assignment.
We use the variable name in the print() function’s brackets because the name acts as a reference to the value stored inside the variable. Python then retrieves the value associated with that name and displays it.
Think of it like this: Your house has an address. The address (variable name) doesn’t contain your family and belongings (the value), but it tells anyone where to find them. Similarly, the variable name points to a specific location in the computer’s memory where the actual value is stored. When you use the variable name, Python goes to that memory location to retrieve the value.
If we were to directly put the value (especially if it’s a string) without quotes, Python would try to interpret it as another variable name, potentially leading to errors. If it’s a string and we put quotes, then Python would print the literal string, not the value stored in a variable. Using the variable name tells print() to look up and use the content of that variable.
When you put quotes around a string inside print(), you are telling Python to treat that exact sequence of characters as the value to be printed, not as a variable name.
Example:
city = "Peshawar" # Here, 'city' is a variable storing the string "Peshawar"
print("city") # This will print the literal string "city"
print(city) # This will print the value of the variable 'city', which is "Peshawar"

Leave a comment