Learn about variables in Python, their declaration, initialization, updation, and dynamic typing with real-world examples, visuals, and easy code snippets.
Table of Contents
What is a Variable in Python?
A variable is like a jar with a label that tells you what’s inside. Just as you can have a jar labeled “Cookies” that holds cookies. Similarly, in Python programming, a variable is a named container in memory (RAM) that stores a value you can change.
Declaring and Initializing Variables in Python
In Python, you don’t need to specify the data type for a variable. To declare a variable, you just give it a name and assign it a value in one step. For example, when you write age = 25
, you create the variable age
and set its value to 25
. Thus, Python automatically manages the data type for you.
Syntax
variable_name = value
- variable_name: The name you give to your variable.
- value: The data or value you want to store in the variable.
Example
age = 25
Updating Variables in Python
To update a variable in Python, you just assign a new value to it. For example, if you have a variable age
set to 25
, you can change it to 30
by writing age = 30
. Thus, the old value is replaced with the new one.
Syntax
variable_name = new_value
- new_value: The new data or new value you want to store in the variable.
Example
x = 25 # Initializing a variable named 'x' with the value 25 x = 30 # Updating the variable 'x' to a new value 30
The picture below uses memory blocks to show how updating a variable works in Python.
- On the left side,
X
is assigned the value25
, which meansX
points to a memory block holding25
(X = 25)
. - On the right side,
X
is then updated to30
, so nowX
points to a new memory block holding30
(X = 30)
.
This shows that when you update a variable, it points to a new value in memory.
Assigning Multiple Values to Variables in One Line in Python
You can assign multiple values to multiple variables in one line, making your code simple and clear. For example, x, y, z = 5, 10, 15
sets x
to 5
, y
to 10
, and z
to 15
. This lets you quickly set up several variables at once.
Syntax
variable1, variable2, variable3 = value1, value2, value3
Example
a, b, c = 5, 10, 15
- The variable
a
is assigned the value5
. - The variable
b
is assigned the value10
. - The variable
c
is assigned the value15
.
Dynamic Typing
Dynamic typing is one of the special features of Python which means you don’t need to specify a data type for a variable. Instead, Python variables determine the type based on the assigned value on the back-end. Moreover, you can change the variable’s type at any time by assigning a different value.
For example, here a is a
is initially an integer, then a string and then a list.
a = 10 # Assigning an integer value a = "Hello" # Reassigning a string value a = [1, 2, 3] # Reassigning a list
How to Check Type of Variable in Python
To check the type of Python variables, you will use the type()
function. This function returns the type of the given variable, which helps you understand what kind of data the variable holds. Here’s how you can do it:
# Assigning values to variables age = 25 name = "Alice" price = 19.99 # Checking data types print(type(age)) # Output: <class 'int'> print(type(name)) # Output: <class 'str'> print(type(price)) # Output: <class 'float'>
Explanation
- The
type()
function is used to check the data type of the variablesage
,name
, andprice
. - It prints the type of each variable, showing that
age
is an integer,name
is a string, andprice
is a float.
Here’s the visualization in action!
Python Code Demonstrating Operations with Variables
The variables age
, name
, and price
are declared and initialized with initial values. Later, they are updated, and each change is printed. The variables x
, y
, and z
are assigned multiple values in a single line. The variable var
shows Python’s flexibility by changing from an integer to a string to a list. This demonstrates Python’s ease in handling various data types.
# Declare and initialize variables age = 25 name = "Alice" price = 19.99 # Print initial values print("Initial values:") print("Age:", age) print("Name:", name) print("Price:", price) # Update variables age = 26 name = "Bob" price = 29.99 # Print updated values print("\nUpdated values:") print("Age:", age) print("Name:", name) print("Price:", price) # Assign multiple values x, y, z = 5, 10, 15 # Print multiple assigned values print("\nMultiple assigned values:") print("x:", x) print("y:", y) print("z:", z)
Remember! The best way you can learn programming is through visualization of code. Go to Python tutor, and visualize each step of the code’s execution.
Conclusion
In this blog, you’ve learned key Python concepts of variables. Including how variables store and update values, and how dynamic typing works in Python, where variables can change the type of data they hold at run-time. By knowing these basics, you can write better and more flexible code. Whether you’re setting up, changing, or handling multiple variables, these simple ideas will help you in your Python programming.
Pingback: Embark on Your Coding Journey with Syntax Scenarios - lucky lify