In this article, we will learn about variables in Python programming language. We will look at what are Python variables, how to declare or create variables, assign values to variables, use variables in Python code, cast variables, read data types of Variables use them & Python language syntax & rules around variables. We will also look at global variables and ways to declare global variables in Python.
This is the fourth article in the Series: Python Tutorial – Learn Python Programming for Beginners
Table of Contents
Variables in Python or Python Variables

Variable is a process of giving a name to a memory location and this memory location holds some data or value. To read the value from a memory location or to set the value to that memory location this name is used that called variable. This variable has a type as well depending on the value that needs to be set in that memory location like string, number, Boolean, etc.
Python variable is like an attribute that holds a value. In Python, it’s not explicitly required to set the type of the variable as Python is designed to determine the type of the variable based on the value being stored. Also in Python, there is no need to declare a variable before using them i.e. Python is not statically typed
A variable name can be a combination of alphabets & numbers but they cannot begin with a number. So variable name has to begin with the alphabet but can contain numbers. Optionally variable names can start with an _ (underscore) as well. Some even use this _ to differentiate between global & local variables.
Creating & Assigning Variables
There is no keyword or command to create variables in python. A variable is created the moment you type some name and assign value to it.
var1 = 1 var2 = "ProCodeGuide-Python"
Above we have declared or created two variables with names var1 & var2 and have also assigned them values. var1 is set to number type (1) whereas var2 is set to string type (ProCodeGuide-Python). Both these variables can be used in the code below with the values set. Anytime & anywhere In the program, these values can even be modified as well.
The equal sign (=) is used to assign values to variables. i.e. variable = value where the variable is the name of the variable being set and value is the actual value that is being assigned to that variable.
In fact, variable var1 which is set to number later on in the code can be set to string/Boolean as well i.e. variable of one type can be set to the value of different type as well this is known as dynamic typing in Python programming. Below is the example of dynamic typing
var1 = 1 var2 = "ProCodeGuide-Python" print(var1) print(var2) var1 = "ProCodeGuide" print(var1) var1 = True print(var1)
When you run the above code we get the below output

Multiple Assignments
You can even perform multiple assignments in a single line as var1 = var2 = var3 = someValue. This is supported in Python programming.
var1 = var2 = var3 = 1 var1 = 2 print(var1) print(var2) print(var3)
When you run the above code we get the below output

Multiple assignments need not be of the same type it can be for variables of different data types as well as shown in the code below. This is also a valid code in Python programming
var1, var2, var3 = 1, "ProCodeGuide", True print(var1) print(var2) print(var3)
On running the above code we get the below output

Case Sensitive Variables
Python is case sensitive so it means that if we declare two variables with the name counter & Counter then they are two different variables in Python. As shown in the below code all variables are different for the Python interpreter.
var = "ProCodeGuide" Var = 50 vAr = False vaR = 100
Casting Variables
As have you seen above that while declaring variables we are not defining the type of the variable it is automatically set as per the type of value being set. Now if you want to explicitly set the type on creation & assignment then you can cast variables as shown in the below code
var1 = str("ProCodeGuide") var2 = int(1) var3 = float(5) var4 = bool(False) print(var1) print(var2) print(var3) print(var4)
Below is the output we got after running the above code. Check the value printed for var3 which was cast to float. Though it is set to 5 it got printed as 5.0 due to cast to float.

Get Variable Types
Now if you want to get the details about the type of the variables then that is also possible in Python programming using function type(). This type() can be used to get the data type of any variable. Below is the sample code to use the type() function in Python.
var1 = "ProCodeGuide" var2 = 50 var3 = False print(type(var1)) print(type(var2)) print(type(var3))
Shown below is the output of the above python code. This code output prints type of the variable which is set implicitly based on the type of value being set.

Global Variables
Variables that are accessible from anywhere in the code i.e. either inside any function or from outside a function are known as global variables.
Global variables are created outside a function as if you create a variable within a function then they are local or restricted to that function only.
If you want to create a global variable within a function then you will have to make use of the “global” keyword while creating the variable.
def myfirstfunc(): global myFirstVariable myFirstVariable = "somevalue" myfirstfunc() print("Value is " + myFirstVariable)
Naming Conventions for Variables in Python
We have looked at how to create variables but there are also some rules around naming variable that needs to be followed otherwise your code will show an error. Here is the list of rules that needs to be followed
- Variable name cannot start with number
- Variable name should start with Alphabet or Underscore _ only
- White spaces & special characters (&, $, !, @ #, *, ~, %) are not allowed in variable names.
- Keywords reserved in Python cannot be used for variable name.
- Variable names are case sensitive i.e. Var1 & var1 are two different variables.
- Not mandatory but its always nice to have that variables names should be descriptive based on the use of the variable in the program.
Summary
Learning about variables in Python is one of the important building blocks towards learning the Python programming language. We saw how to declare variables in Python & use them. Do practice declaring variables in Python and challenge yourself with some assignments to get a good grip of practicals around variables in Python.
In our next article, we will look at basic data types in the Python programming language.
Please provide your suggestions & questions in the comments section below
References – Python Documentation
Hope you found this article useful. Please support the Author
