Variables in Python Programming Language - Text-based graphic with 'Variables' highlighted in red and the rest in black.

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:

  1. 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.
  2. print(message): This line uses the print() function to display the value that is currently stored in the message variable on the screen.
  3. message = "Learning Python is fun!": Here, we are changing the value of the message variable to a new string: "Learning Python is fun!". When you assign a new value to a variable, the old value is replaced.
  4. print(message): This line will now print the updated value of the message variable.
  5. number_of_students = 25: This shows that variables can also hold numbers. Here, we create a variable named number_of_students and assign the integer value 25 to 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"

Discover more from Shafaat Ali Education

Subscribe to get the latest posts sent to your email.

Leave a comment

apple books

Buy my eBooks on Apple Books. Thanks! Shafaat Ali, Apple Books

Discover more from Shafaat Ali Education

Subscribe now to keep reading and get access to the full archive.

Continue reading