We're actively developing the Python course; advanced AI courses will be released soon.

Learn AI Way

Variables in Python


What Is a Variable?

A variable is a name that is used to store a value in your program memory.

In simple words, you can think a variable like a box with a label. The box contains the value and its label is name of a variable.

x = 10

name = "Hitesh"

is_ready = True
Here, three variables -  x, name, and is_ready -  are created, which contain the values 10, 'Hitesh', and True, respectively.

You don’t need to declare the data type of a variable. You don’t need to declare it in Python.

You can change the value (of a variable) any time as shown below:

x = 20
name = "Ramesh"
is_ready = False

Creating and Assigning Variables

In Python, you can create a variable using the = sign.

a = 10
b = 3.14
text = "Hello"

Multiple assignments:


You can assign several variables at once in a single line. This helps to make code short and clean.

x, y, z = 10, 20, 30
name = age = city = "New Delhi"
It means, x, y, and z get values 10,20 and 30 respectively.

In next line, three variables (name, age, and city) get the same value "New Delhi”. This is also known as chained assignment

Augmented assignment:


In this, you update the value of a variable using its own value in a shorter way.

In this type assignment, we use a shortcut operator like +=, -=, *=, or /=.

Example:

•    x += 5 means add 5 to x
•    x -= 2 means subtract 2 from x
•    x *= 3 means multiply x by 3
•    x /= 2 means divide x by 2

Example code:

count = 10     # Step 1: Assign initial value 10 to 'count'
count += 20
In simple words,
count += 20 means, take the current value of count (10), add 20 to it, and store the new value (30) back into count variable.

Variable Naming Rules & Conventions

In Python, when you create a variable then one needs to follow some rules to avoid error at run time.

Rule No 01:


You can not start a variable name with a number as shown below:

Example 01:

2count

Following error message is shown:


This is an incorrect one and error is also shown for above declaration.

Example 02:

count2 
This is correct one.

Rule No 02:


A variable name can only have:

•    Lowercase letters: a–z
•    Uppercase letters: A–Z
•    Digits: 0–9
•    Underscore: _

Example:

user_name = "Hitesh"
age2 = 25
total_sum = 100

Rule No 03:


Python treats uppercase and lowercase as different.

Example: Count and count are two different variables in Python language.

Rule No 04:


In Python, there are some reserved words like class, if, for, while, etc.

These reserved words are called keywords and you cannot use them as variable names.

Variable Types and Mutability

In Python variables, you can store any type of data like:

num = 40           # int
price = 19.99      # float
name = "Hitesh"    # string
is_open = False    # bool
data = [1, 2, 3, 4]  # list
info = {"age": 25}  # dict

Mutable vs Immutable


Mutability simply means whether a value can be changed after creation or not.


 

1. Immutable Example


x = 10
y = x
y = 15
print(x)  # 10 -> unchanged
Output: 

10
 
Here, you can see that changing y value does not affect x because numbers are immutable.

2. Mutable Example

a = [10, 20]
b = a

b.append(30)

print(a)  # [10, 20, 30]
print(b)  # [10, 20, 30]

Output:

[10, 20, 30]

[10, 20, 30]

If we print both lists a and b, the result is [10, 20, 30] because lists are mutable.

Both a and b are of list type and share the same memory location, so when one changes, the other also changes.