
Today, you will learn about variables in Python, what variables are, different ways to declare them, and naming conventions.
First, What is a Variable?
A variable is a name given to a memory location. We use variables to store values (data) in a program.
Example:
user_id = 1200
Here, user_id is a variable, and 1200 is the data. When you run the code, 1200 will be stored in a memory location (inside RAM) and we can access it any time using the user_id. Let’s print its value.
user_id = 1200
print(user_id)
Run it, and it prints 1200 on the console.
If you are curious about the location, there is a method called id(). It returns the address of the memory location of the variable.
user_id = 1200
print(id(user_id))
Run the above code, and it prints the address that looks like 1672751937008. The address will be different every time you run the program. So, you get different results each time you execute the code.
We can change the variable’s values at any time.
Example:
user_id = 1200
user_id = "2000"
print(user_id)
In the above program, we have assigned 1200 (an integer) to the user_id. Next, we changed it to 2000 (a string). Now, the value of user_id is “2000”. Run the code and it prints 2000.
There are multiple ways to create (declare) variables. Let us look at them.
How to Declare a Variable in Python?
To declare a variable, write the variable name, = (assignment operator), and the value (the data). This is what we have done in the above code.

Note: You do not need to write the data type while declaring a variable.
Assign Multiple Values at a Time:
In Python, you can create multiple variables and assign different values to them in one statement.
a, b, c = 100, 200, 300
print("a =", a)
print("b =", b)
print("c =", c)
Output:
a = 100
b = 200
c = 300
You can also assign the same value to multiple variables in one statement.
num1 = num2 = num3 = 110
print("num1 =", num1)
print("num2 =", num2)
print("num3 =", num3)
Output:
num1 = 110
num2 = 110
num3 = 110
Naming Conventions and Rules for Variables in Python:
- Variable name should not be any keyword.
- Variable names are case-sensitive (user_id, USER_ID, and User_id are three different variables).
- You have to use lower case letters (a-z), upper case letters (A-Z), numbers (0-9), and underscore only.
- Don’t start a variable name with a digit.
- If you want to create a variable name having two words, use underscore to separate them. For example, in the above programs, we have created the user_id variable. Here, the underscore separates the two words user and id.
Related: