Let’s learn how to handle variables in Python code i.e. Python Variables. The variable in any programming language including Python is a name given to a memory location and this memory location contains the value of the variable.
Table of Contents
Create Variables in Python
There is no special keyword, command or syntax to create variables in Python. The variable is created the moment you type some name and assign a value to it.
Here is the source code to demonstrate how to create variables in Python
var1 = 100 var2 = "Some Text"
In the above Python code, we have created a couple of Python variables with the names var1 and var2. We have also assigned values to both variables. The var1 variable is set to number type with value 100 and the var2 variable is set to string type with value “Some Text”. The = sign is used to assign value to the variable i.e. variable = value.
Assign Value to Multiple Variables in Python
To perform multiple variable assignments in a single line you can specify code as var1 = var2 = var3 = value.
Here is the source code to perform multiple variable assignments in Python code
var1 = var2 = var3 = "Some Text"
The multiple variable assignments need not be of the same data type. The variables of different data types can also be assigned on the same line as per the code shown below
var1, var2, var3 = 100, "Some Text", True
Modify the Data Type of the Python Variables
The Python variables are not declared with any particular data type. The Python variable data type can be changed even after variables have been assigned values.
Here is the source code to demonstrate how to change Python variable data type
var1 = 100 print(type(var1)) var1 = "Some Text" print(type(var1))
Below is the output of the above code
<class 'int'>
<class 'str'>
As you can see from the above output that the initial data type of var1 is an integer (int) as it was assigned to the value 100 and then the data type got changed to string (str) as it was set to a new value “Some Text”
Type Casting in Python
While declaring variables in Python we do not specify the data type. The data type of Python variables is set automatically as per the type of value being set.
If you want to specify the data type while creating and assigning Python variables that can be done by casting in Python.
Here is the source code to demonstrate how to perform type casting in Python
var1 = str("Some Text") var2 = int(100) var3 = float(5) var4 = bool(False) print(var1) print(var2) print(var3) print(var4)
Below is the output of the above code
Some Text
100
5.0
False
As you can see from the above output though the value for var3 was set to 5 it got printed as 5.0 due to cast to float.
Identify the Data Type of Variable in Python
When you create variables in Python you do not specify the data type of the variable. Python sets the data type of the variables as per the type of value being set.
In Python, if you want to know about the data type of the variable then you can make use of the type() function in Python programming to get the data type of the variable.
Here is the source code to demonstrate how to get the data type of variable in Python
var1 = "Some Text" var2 = 250 var3 = 5.0 var4 = False print(type(var1)) print(type(var2)) print(type(var3)) print(type(var4))
Below is the output of the above code
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
As you can see from the above output that the type function returns the data type of the variables in Python as str (string), int (integer), float, bool, etc.
Global Variables in Python
Python variables that are created outside of the function are known as global variables. These global variables in Python are accessible from everywhere i.e. inside of a function or outside.
Here is the source code to demonstrate the global variable outside of a function.
message = "Global Variable Text" def somefunc(): print(message) somefunc()
Below is the output of the above code
Global Variable Text
As you can see from the above output the global variable message is accessible within a function as well.
If you declare a new variable with the same name as a global variable within a function then it remains local to the function and the outside global variable remains unchanged. Below is the source code to demonstrate the creation of a new variable in a function with the same name as the global function.
message = "Global Variable Text" def somefunc(): message = "Global Variable Text - From Function" print(message) somefunc() print(message)
Below is the output of the above code
Global Variable Text - From Function
Global Variable Text
As you can see from the above code there are two variables with a name message one that is global and the other that is local to function somefunc().
Global keyword in Python
To create global variables in Python within a function in Python you will have to make use of the “global” keyword at the time of creating the variable. Below is the source code to demonstrate how to make use of a global keyword to create global variables in functions in Python.
message = "Global Variable Text" def somefunc(): global message message = "Global Variable Text - From Function" print(message) somefunc() print(message)
Below is the output of the above code
Global Variable Text - From Function
Global Variable Text - From Function
As you can see from the above output that after using the global keyword with variables in the function both the variables with the name message are the same and contain the same value as well.
Case-Sensitive Variables in Python
Python programming language is case sensitive so it means that if we declare two variables with the name message & Message then they are two different variables in Python.
Here is the source code to demonstrate the case-sensitive variables in Python
var1 = "Some Text" Var1 = 100 vAr1 = False vaR1 = 100.0 print(var1) print(Var1) print(vAr1) print(vaR1)
Below is the output of the above code
Some Text
100
False
100.0
As we can see from the above output all the variables have the same name but still are different variables in Python as they are with different combinations of uppercase and lowercase letters.
Print Variables in Python
In Python, if you want to output data of the variable then you can make use of the print() function in Python programming to print the data of the variable.
Here is the source code to demonstrate how to print variables in Python
message = "Hello, World!" print(message)
Below is the output of the above code
Hello, World!
As you can see from the above output the Python programming print() function has printed the value of the message variable i.e. Hello, World!
Summary
In this article, we learnt about Python Variables and we also explored how to perform various operations on variables in the Python programming language. You will have to thoroughly read this article and understand everything about Python variables as variables are very critical to any programming language.
You can also check my series on Python for Beginners – Series: Python Tutorial – Learn Python Programming for Beginners
References – Python Documentation
Please provide your suggestions & feedback in the comments section below