In Python, variables are used to store something. It is like a container that holds some data. One interesting thing about variables in Python that makes it different from most other programming languages is that we need not define any data types while defining or declaring the variables. Due to this thing, we also Python is not a statically typed language.
A statically typed language is such a language in which we have to define the type of the variable at the compile time. But in Python, we simply declare the variable and Python decides the data type of the variable implicitly while the run time. Also, Python variables are simply the reference of a particular memory location where the data will be actually stored. You can also call variables the most basic unit of storage in any programming language.
Quick Introduction and Example
Observe the following carefully. Here we’re storing the name CodeLucky in the variable website. Here the name of the variable is
website which is basically storing the text, CodeLucky inside the memory to which it’s referring.
As we have mentioned the type of data that we’re here storing in the variable is a String but we need not define it anywhere. Python implicitly understood its data type while runtime. In the second line of code, we’re simply using the print function which will print the contents of the variable to the console screen. You can test it by clicking on the Run button above!
Rules To Create Python Variables
There are some rules which you need to follow in order to make sure of their validity in Python.
- A variable name should always start with either some alphabet letter or an underscore. In other words, a variable name can not start with a digit or any other special character.
- A variable name must contain letters from Alphabet (A-Z, a-z), numbers (0-9), and an underscore (_).
- Although there is no limit to the length of variable names in Python as per PEP-8 guidelines (These are the style guidelines for better readability of Python Code), it is recommended to keep the variable names not more than 79 characters long.
- Like most other programming languages, there are some reserved keywords, which can not be used as variable names.
Here are a few examples showcasing valid and invalid variables in Python.
|Full_Name||Valid||A variable name can contain alphabets, numbers, and underscore.|
|_name||Valid||A variable name can start with an underscore.|
|$name||Invalid||A variable can not start with any special character other than underscore.|
|full$name||Invalid||A variable name should also not contain any other special character than the underscore.|
|full1name||Valid||A variable name can contain numbers in between,|
|2ndTravel||Invalid||A variable name can not start with a number.|
Creating the Variables
To create a variable, all you need to do is to write down the variable name followed by an equal symbol and then followed by the value that you want to store within the variable. Here in the following example, we’re storing the number 150 in the variable named
#storing the numerical value 150 into the variable marks marks = 150 #printing the marks variable value to the console print(marks)
Here you can also see that we’ve used the comments for the code readability. You can see that when we simply put the variable name into the print statement, it displayed the variable value on the console screen.
Changing the Variable Values
A variable value can be changed at any time in a Python program unless or until it is defined as a constant variable. (We will talk about making variables constant later in this article)
Changing the variable value is exactly the same as defining it. Here in the example, we’ve taken a variable named height and have stored a numerical value with decimals inside it, and then we’re printing it. After that, we’re re-declaring the variable value with another numerical value and then also printing it to the screen. Furthermore, we’re changing the variable value to a textual value to showcase the concept that variable names can store one data type at a time but it is possible to change the variable value with another data type value as well.
#defining height initially to numerical value with decimals height = 176.50 #printing the value of the variable print(height) #changing the variable value height = 190.20 #printing the current variable value print(height) #re-declaring the variable value with another data type height = "I need to measure" #printing the current variable value print(height)
Initializing Multiple Variables Together
Let’s see how we can initialize multiple variables together.
Assigning The Same Value To Multiple Variables
If you require to assign the same value to multiple and different variables, you can do it in the following way.
var1 = var2 = var3 = 5 print(var1) print(var2) print(var3)
In the above code, we’re assigning the integer value 5 to three different variables
Assigning Different Values To Different Variables
We can also define the multiple different values to multiple different variables at the same time using the comma-separated variables assignment as illustrated below.
var1, var2, var3 = 5, "Gurmeet Singh", True print(var1) print(var2) print(var3)
In the above code, we’re assigning three different values of three different data types to the variables
Scope of Variables
By default all of the variables that you create in your script outside of any functions are global. That means you can access them anywhere in your python code unless or until you make them explicitly non-global.
Let’s quickly understand the same with the help of an example.
In the following code, we’re declaring a global variable name outside of any function at the start of the code and then accessing the same in a print statement inside a function as well as outside it.
name = "Gurmeet Singh" def hello(): #calling the global variable name inside the function print(name) #calling the function hello hello() #calling the global variable name outside the function print(name)
Now let’s do an interesting experiment. We’ll declare two variables one inside the function and one outside it, keeping the name of the variable same at both places.
name = "Gurmeet Singh" def hello(): name = "Something Else" print(name) #calling the function hello hello() #calling the global variable name outside the function print(name)
As you can see in the output, although the value of the variable name is defined globally as “Gurmeet Singh”, the print statement outputting the variable name inside the function is actually using the variable’s value that is defined locally within the function, i.e. “Something Else”
Also, another observation as we’re using another print statement after calling the function, it’s outputting the original globally defined value of the variable name as “Gurmeet Singh”. This clearly indicated the scope of the two different variables.
The scope of the variable name defined within the function is limited to only that function.
The global keyword
As we’re talking about the scope of variables, there’s a very interesting use-case for the global keyword. Take the same example as in the above section. As we observed even if we change the value of the local variable, it’s not changing the value of the global variable, although they are of the same name, owning to their different scopes.
But now, let’s say you want to modify the value of the global variable name inside the function. You can do this using the global keyword.
The global keyword can be used to make the reference of the local variables to global variables. This is illustrated in the following example where we’re using the global keyword before the variable name to actually make it global and not local. Then when we’re changing the value of the same variable to “Something Else” and as we change it, it actually changes the value of the original global variable.
name = "Gurmeet Singh" def hello(): global name name = "Something Else" print(name) #calling the function hello hello() #calling the global variable name outside the function print(name)