Lesson 10 of 70 – Python Variables
14%

Python Variables

A variable is a name used to store data in a Python program. The value stored in a variable can be a number, text, decimal value, Boolean value, or other types of data.

Note: Python variables do not need to be declared with a specific data type. Python automatically determines the type of value assigned to the variable.
What is a Variable?

A variable is a named location used to store a value in memory. For example:

name = "Rahul"
age = 20

Here, name and age are variables.

Creating a Variable

In Python, a variable is created when you assign a value to it using the assignment operator =.

x = 10
name = "Amit"
price = 99.50
The variable is created automatically when a value is assigned to it.
Variable Assignment

The = operator is used to assign a value to a variable.

age = 25

This means that the value 25 is assigned to the variable age.

name = "Rahul"
city = "Patna"
marks = 85
Changing Variable Values

The value of a variable can be changed during program execution.

age = 20

age = 25

print(age)
Output:
25
Variables Can Store Different Data Types

A Python variable can store different types of values.

name = "Amit"
age = 21
height = 5.8
is_student = True

Here:

  • name stores a string.
  • age stores an integer.
  • height stores a float.
  • is_student stores a Boolean value.
String Variable

A variable can store text using single or double quotation marks.

name = "Rahul"

print(name)
Output:
Rahul
Integer Variable

An integer variable stores a whole number without a decimal point.

age = 25
marks = 90

print(age)
print(marks)
Output:
25
90
Float Variable

A float variable stores a number containing a decimal point.

price = 99.50
height = 5.8

print(price)
print(height)
Output:
99.5
5.8
Boolean Variable

A Boolean variable can contain either True or False.

is_student = True
is_teacher = False

print(is_student)
print(is_teacher)
Output:
True
False
Checking Variable Type

The type() function is used to find the data type of a variable.

age = 25

print(type(age))
Output:
<class 'int'>

Another example:

name = "Rahul"

print(type(name))
Output:
<class 'str'>
Multiple Variables

Python allows you to create multiple variables in a single program.

name = "Amit"
age = 22
city = "Patna"

print(name)
print(age)
print(city)
Assigning Multiple Values

You can assign values to multiple variables in one statement.

x, y, z = 10, 20, 30

print(x)
print(y)
print(z)
Output:
10
20
30
Same Value to Multiple Variables

The same value can be assigned to multiple variables.

x = y = z = 100

print(x)
print(y)
print(z)
Output:
100
100
100
Variable Naming Rules

Python has some rules for naming variables:

  • A variable name must start with a letter or underscore.
  • A variable name cannot start with a number.
  • A variable name can contain letters, numbers and underscores.
  • Variable names are case-sensitive.
  • Python keywords cannot be used as variable names.

Valid examples:

name = "Rahul"
age1 = 20
student_name = "Amit"
_total = 500
Invalid Variable Names

The following variable names are invalid:

1name = "Rahul"
student-name = "Amit"
class = 10

These names are invalid because they either start with a number, contain an invalid character, or use a Python keyword.

Case-Sensitive Variables

Python variable names are case-sensitive. This means uppercase and lowercase letters are treated differently.

name = "Rahul"
Name = "Amit"

print(name)
print(Name)
Output:
Rahul
Amit

Here, name and Name are two different variables.

Using Variables in Calculations

Variables can be used in mathematical calculations.

a = 10
b = 20

sum = a + b

print(sum)
Output:
30

Variables make programs easier to understand and modify.

Using Variables with Strings

Variables can also be used with strings.

first_name = "Rahul"
last_name = "Kumar"

full_name = first_name + " " + last_name

print(full_name)
Output:
Rahul Kumar
Variable Example
name = "Rahul"
age = 20
marks = 85.5
is_student = True

print("Name:", name)
print("Age:", age)
print("Marks:", marks)
print("Student:", is_student)
Output:
Name: Rahul
Age: 20
Marks: 85.5
Student: True
Key Points
  • A variable is used to store a value.
  • Python variables are created using the = operator.
  • Python automatically determines the data type.
  • Variables can store strings, numbers, Boolean values and other objects.
  • Variable names are case-sensitive.
  • A variable name cannot start with a number.
  • Use meaningful variable names to make programs easier to understand.

🧠 Quick Quiz

Question: Which symbol is used to assign a value to a variable in Python?