Variables in Python | Declare, Assign & Redeclare Python Variables

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 redeclare variables in Python programming.

variables in python

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.

Naming Variables in Python

Here are a set of rules to remember while naming variables in Python.

  • A variable can have a short name like a, b, x, y or lengthy names like name, mobilenumber, dateofbirth.
  • A variable name can start with an alphabet or number, but not with an underscore. Also, a variable name can contain only these – alphabets, numbers, and underscores. Special character, spaces etc will throw a syntax error.
  • Variable names are case sensitive, which means, name, NAME, Name will be considered as different variables.

Declaring 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.

Assigning Variables in Python

a) Assign a Single Value to a Single Variable

#Assign value 9 to x
x = 9
print (x + 5)
Output: 9

Explanation: Here a single value 9 is assigned to a single variable x using the assignment operator =.

b) Assign Multiple Values to a Single Variable

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 line
x = 9; x = 10; x = 1
print (x + 5)
Output: 6

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.

c) Assign Single Value to Multiple Variables

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)
Output: 27

d) Assign Multiple Values to Multiple Variables

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

Re-Declaring Variables in Python

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.

Case 1: 

#Declare variable
x = "Python is Fun"
print (x)

#Redeclare variable
x = 10
print (x)
Output:
Python is Fun
10

Case 2: 

#Declare & Redeclare variables
x = "Python is Fun"
x = 10
print (x)
Output: 10