A variable is a container that stores a value assigned to it. You can store the value in the variable and later use it when required. To understand the concept of variables completely, in this article, we will look at how to declare, assign and re declare variables in Python programming.
Before we learn that, did you think of why do we need a variable?
Well, let's come this way. Say, you are planning to build a house, then what is the first most important thing you need? Think of it for a minute.
Obviously Land is the first most important thing and this land is where your home is built. Similarly, in programming, whenever you decide to take an input from the user, you need some space to store that input. This space is provided by a variable. In a way, we just declare a variable and technically acquire space from the computer.
Moving on, naming variables isn't easy. There are some guidelines you need to know before you name a variable.
Here are a set of rules to remember while naming variables in Python.
Unlike other programming languages, Python has no specific command to declare variables. In python, you need not declare the variable or its type (int, float, string etc). Rather, a variable is declared the very moment you first assign a value to it.
#Declaration of variables in Python x = "FACE Prep"print (x) Output: FACE Prep
Explanation: In the above example, we have assigned the string FACE Prep to the value x. Here the interpreter by default considers the declaration of x and understands that x needs to store a string.
Also, you can change the data type of variables in Python i.e a variable of integer data type can be converted to string data type and so on. This process is called Type conversion.
#Assign value 9 to x x = 9 print (x + 5)
Explanation: Here a single value 9 is assigned to a single variable x using the assignment operator =.
In Python, you can assign multiple values to a single variable. But the value assigned last will be stored in that variable. For example, in the below 2 examples, we have assigned multiple values to the variable x.
#Assign multiple values to x x = 9 x = 10 x = 1 print (x + 5) (or) #Assign multiple values to x in a single linex = 9; x = 10; x = 1 print (x + 5)
Explanation: x = 1 will overwrite all the previous assignments. Hence x + 5 = 1 + 5 = 6.
Note: A lot of you might use the syntax x = 9, 10, 1 to assign multiple values to x. But, this will throw an error.
Python lets you assign a single value to multiple variables using the assignment operator =. Consider this example,
#Assign single value to multiple variables x = y = z = 9 print (x + y + z)
In Python, you can assign multiple values to multiple variables in one single line using the below syntax. The values assigned to these variables can be of different data types as well.
Syntax: Variable 1, Variable 2, Variable 3.... = Assign 1, Assign 2, Assign 3......
#Assign multiple value to multiple variables x, y, z = 9, 8.3, "FACE Prep" print (x) print (y) print (z)
Output: 9 8.3 FACE Prep
All this while you learnt about variables, have you thought of what happens if you try to store two values in one variable?This concept is called the redeclaration. In Python, can declare variables with a new value, even though we have declared them previously. In that case, the latest declaration will be taken into consideration. The below examples will help you get some clarity.
#Declare variable x = "Python is Fun" print (x) #Redeclare variable x = 10 print (x)
Output: Python is Fun 10
#Declare & Redeclare variables x = "Python is Fun" x = 10 print (x)