In programming, data must be stored so it can be reused later. In Python, we store data inside variables.
Variables are one of the most important foundations of programming. Without variables, you cannot perform calculations, take input, or build logic.
A variable is a name given to a value.
It allows us to store information and use it later.
Example:
name = "Aarav"
age = 21Here:
namestores textagestores a number
To display them:
print(name)
print(age)You must follow these rules:
- A variable name can contain letters, numbers, and underscore (
_) - It cannot start with a number
- It cannot use Python keywords (like
if,for,while) - Python is case-sensitive (
ageandAgeare different)
Valid examples:
student_name = "Riya"
marks1 = 90
total_score = 450Invalid examples:
1marks = 90 # Cannot start with number
class = "BCA" # 'class' is a keywordPython automatically understands the type of data you assign.
Example:
x = 10 # integer
x = "Hello" # now stringYou do not need to declare the data type separately. Python decides it automatically.
This is called dynamic typing.
We focus only on the most important types.
Whole numbers without decimals.
age = 18
total_marks = 450Numbers with decimal values.
price = 99.99
temperature = 36.5Text values written inside quotes.
name = "Kiran"
city = 'Mumbai'Both single and double quotes are allowed.
Represents only two values:
- True
- False
Example:
is_student = True
is_logged_in = FalseBooleans are mainly used in conditions (which you will learn later).
You can check the type of a variable using type().
Example:
age = 22
print(type(age))Output:
<class 'int'>
Another example:
name = "Python"
print(type(name))You can change the value of a variable anytime.
score = 50
score = 75
print(score)The latest value will overwrite the previous one.
Python allows assigning multiple variables in one line.
Example:
a, b, c = 10, 20, 30Or assigning same value:
x = y = z = 5- Forgetting quotes in strings:
name = Rahul # Error- Confusing number and string:
age = "20"
print(age + 5) # Error- Using keywords as variable names.
- Create a variable
countryand store your country name. - Create variables for
priceandquantity. - Print all variables.
- Use
type()to check their types. - Create a boolean variable called
is_available.
Example practice:
country = "India"
price = 500
quantity = 3
is_available = True
print(country)
print(type(price))
print(type(is_available))