- What is a Variable?
- How to Declare a Variable?
- Rules for Naming Variables
- Python is Dynamically Typed
- Types of Variables
- Changing the Value of a Variable (Reassignment)
- Multiple Variables in One Line
- Checking Variable Type
- Constants in Python
- Best Practices for Variables
- Quick Practice (Guess Output)
- Summary
- Homework for You:
What is a Variable?
A variable is a name given to a memory location where a value is stored. You can use or change the value by writing the variable’s name.
Example:
name = "Ram"
age = 25
Here, name
and age
are two variables.
name
stores “Ram” (a string type).age
stores 25 (an integer type).
In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs.
How to Declare a Variable?
To create a variable in Python, simply write:
variable_name = value
Example:
city = "Delhi"
temperature = 38.5
is_raining = False
Rules for Naming Variables
Variable names must follow specific rules:
- Start with a letter or an underscore (_):
2name = "Amit" (❌ Invalid)
name2 = "Amit" (✅ Valid) - Can contain only letters, numbers, and underscores:
student_name, rollNo, marks_1 (✅ Valid)
- No spaces are allowed:
student name = "Ravi" (❌ Invalid)
- Cannot be Python keywords: Keywords are reserved words like
if
,while
,class
. - Python is case-sensitive:
Name
andname
are considered different variables.Name = "Rahul"
name = "Rohit"These are two separate variables.
Python is Dynamically Typed
You do not need to define the type of a variable. Python automatically understands the data type based on the value you assign.
x = 10 # x is an integer
x = "hello" # Now x becomes a string
Read: Features of Python
Types of Variables
Here are some common types of data you can store in variables:
Type | Example | Description |
---|---|---|
int | age = 18 | Whole numbers |
float | pi = 3.14 | Numbers with decimal points |
str | name = "Mohan" | Text, enclosed in quotes |
bool | is_coding = True | Boolean values: True or False |
list | fruits = ["apple", "banana"] | Ordered, changeable collection of items |
dict | person = {"name": "Rita", "age": 22} | Unordered collection of key-value pairs |
Suggested Read: Data Types in Python with Examples
Changing the Value of a Variable (Reassignment)
You can change a variable’s value after it has been created.
x = 5
x = x + 2
print(x) # Output: 7
Multiple Variables in One Line
You can assign values to multiple variables on a single line.
Different values:
a, b, c = 1, 2, 3
print(a, b, c) # Output: 1 2 3
Same value:
x = y = z = 100
print(x, y, z) # Output: 100 100 100
Checking Variable Type
Use the type()
function to find out a variable’s data type.
name = "Radha"
print(type(name)) # Output: <class 'str'>
marks = 90.5
print(type(marks)) # Output: <class 'float'>
Constants in Python
Python does not have a special syntax for constants. However, there’s a common convention:
- Constant variables are named in ALL CAPS.
- We assume their values will not change during program execution.
Example:
PI = 3.14159
GRAVITY = 9.8
Best Practices for Variables
Follow these guidelines for writing clear and readable code:
- Use meaningful names: Names like
total_marks
oruser_name
are clear. - Avoid short and unclear names:
x
,y
,a1
can make code hard to understand. - Use snake_case:
student_name
andphone_number
are examples of this common Python style.
Quick Practice (Guess Output)
What will this code print?
name = "Amit"
age = 30
age = age + 5
print(name, age)
Output: Amit 35
Summary
Concept | Description |
---|---|
Variable | A box that stores a value |
Syntax | variable_name = value |
No Type Decl. | Python automatically understands the type |
Reassignment | You can change the value |
Constants | Use ALL CAPS by convention |
Homework for You:
Try creating these variables:
- A variable to store your best friend’s name.
- A variable for your birth year.
- A variable to check if you’re a student (True or False).
- A constant for your
SCHOOL_NAME
(e.g., “ABC School”).