Python Variables and Constants: Complete Beginner’s Guide
💡 1. Introduction
Whenever we write a program, we need to store information such as names, ages, marks, prices, and calculations. Programming languages provide variables and constants for this purpose.
🔹 Variable → A name whose value can change.
🔸 Constant → A name whose value is intended to remain unchanged.
🐍 2. What is a Variable?
A variable is a name that we use to store information in a Python program. The information stored in a variable is called its value.
Think of a variable as a label attached to a value. We can use that label later in the program whenever we want to access the stored value.
age = 20
🔍 What does this statement mean?
In the statement age = 20, Python associates the name age with the value 20.
variable name value
The symbol = is called the assignment operator. It assigns the value on the right to the variable on the left.
age = 20 ↑ ↑ name value
Once a value is stored, we can use the variable name in other statements. For example:
age = 20 print(age)
20
⭐ Remember: A variable does not need to be declared separately before using it in Python. You can create a variable simply by assigning a value to it.
🔄 3. How Python Variables Work
Python variables are a little different from variables in languages such as C, C++ and Java. In Python, we do not normally declare the data type of a variable before using it. We simply assign a value to a name.
Let’s see what happens when we assign a value to a variable and then assign a different value to the same variable.
x = 10
Here, Python associates the name x with the value 10.
x = 25
Now the name x is associated with 25 instead of 10. The previous value is no longer the value obtained through x.
Later: x → 25
🧪 We can verify this by printing the variable after each assignment:
x = 10 print(x) x = 25 print(x)
10 25
⭐ Key idea: When we write x = 25, the value associated with x changes from 10 to 25. This is why we call it a variable — its value can vary during a program.
💡 Coming up: Python also allows a variable to refer to a value of a completely different type. For example, the same name can first refer to an integer and later refer to a string. We will explore this in the next section.
🧩 4. Python Does Not Require Variable Declaration
One of the first differences you will notice when learning Python is that you usually do not need to declare a variable before using it.
In Python, a variable is created automatically when you assign a value to a name. You don’t have to specify whether the variable is an integer, floating-point number, or string.
age = 20
That’s all we need! Python understands that age refers to the value 20.
int age = 20;
In C, int tells the compiler that age is an integer variable.
int age = 20;
int age = 20;
📊 How is Python different?
| Language | Example | Type Declaration |
|---|---|---|
| Python |
age = 20
|
Not required |
| C |
int age = 20;
|
Required |
| C++ |
int age = 20;
|
Required |
| Java |
int age = 20;
|
Required |
⭐ Important: Python is dynamically typed. This means you do not normally specify the type of a variable when creating it. Python determines the type from the value assigned to it.
🔎 For example, Python can determine the type automatically:
age = 20 name = "Rahul" marks = 87.5 print(type(age)) print(type(name)) print(type(marks))
<class 'int'> <class 'str'> <class 'float'>
🎯 In short: Python lets you create a variable simply by assigning a value. Unlike C, C++ and Java, you normally don’t have to write the variable’s data type before its name.
🔄 5. Python Variables Can Change Type
One of the interesting features of Python is that the same variable name can be assigned values of different data types at different points in a program.
For example, a variable can first refer to an integer and later be assigned a string. Python determines the type from the value currently assigned to the name.
x = 10 print(x) x = "Hello" print(x)
10 Hello
🔍 What happened here?
First, the name x is associated with the integer value 10.
Later, we assign a string to the same name:
⭐ Important: Python does not require the variable name to have one fixed data type. The name x can be associated with an integer at one point and a string at another point.
⚖️ How is this different from C?
In C, when we declare:
int x = 10;
x is declared as an integer. You cannot simply assign a string such as “Hello” to it as you can in Python.
Java works in a similar way:
int x = 10;
Here, x is an integer variable, so assigning a string to it is not allowed in the same way as Python.
| Language | Can the same name refer to different types? |
|---|---|
| Python | Yes |
| C | No, not in the same way |
| C++ | No, not in the same way |
| Java | No, not in the same way |
🎯 Remember: Python is dynamically typed. You don’t normally specify a variable’s type when assigning a value, and the same name can later be associated with a value of another type.
🧮 6. Variables Can Store Different Types of Data
A Python program usually works with different kinds of information. For example, we may need to store a person’s name, age, marks, or whether a student has passed.
Python provides different data types for different kinds of values. The type of a variable is determined automatically from the value assigned to it.
name = "Rahul" age = 20 marks = 87.5 passed = True
🔍 What type of data is stored?
Each variable above contains a different kind of value. Python automatically identifies the appropriate data type.
| Variable | Value | Data Type | Meaning |
|---|---|---|---|
| name |
"Rahul"
|
str | Text |
| age |
20
|
int | Whole number |
| marks |
87.5
|
float | Decimal number |
| passed |
True
|
bool | True or False |
💡 Remember: You don’t have to write str, int, float, or bool when creating these variables. Python determines the type from the assigned value.
🔎 How can we check the type?
Python provides the built-in type() function to find out the type of a value or variable.
print(type(name)) print(type(age)) print(type(marks)) print(type(passed))
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
📚 The four types used above
str
→ Text or characters
int
→ Whole numbers
float
→ Numbers containing a decimal point
bool
→ True or False
🎯 Key takeaway: A Python program can use variables containing different types of data. Python automatically determines the type from the value, making variable creation simple and flexible.
✏️ 7. Rules for Naming Python Variables
Python has a few simple rules that must be followed when choosing a variable name. A good variable name should be both valid and meaningful.
Let’s look at some examples of names that Python accepts and names that Python rejects.
✅ Valid Variable Names
The following are valid Python variable names:
name = "Amit" age = 20 student_name = "Rahul" marks1 = 85 _total = 100
❌ Invalid Variable Names
The following names are not valid Python variable names:
1name = "Amit" student-name = "Rahul" class = 10
📌 Rules You Should Remember
1. Start with a letter or underscore
A variable name can begin with a letter
(a-z or A-Z) or an underscore
(_).
2. Do not start with a number
A variable name cannot begin with a digit.
❌ 2marks
✔ marks2
3. Letters, digits and underscores are allowed
After the first character, you can use letters, numbers and
underscores.
✔ student1
✔ student_name
✔ marks_2026
4. Do not use spaces or special symbols
Spaces and symbols such as –, @,
# and $ cannot be used in a
variable name.
❌ student-name
✔ student_name
5. Variable names are case-sensitive
Python treats age, Age and
AGE as three different names.
6. Python keywords cannot be used
Words such as if, for,
class, while and
def have special meanings in Python and cannot
be used as variable names.
⚠️ Example: class = 10 is invalid because class is a Python keyword.
🧠 Quick Check
✔
student_name
— Valid
✔
marks2
— Valid
✘
2marks
— Starts with a number
✘
student-name
— Contains a hyphen
✘
class
— Python keyword
🌟 Good Naming Practice: Choose names that clearly describe the data they store. For example, student_name is easier to understand than simply using x or a.
🔠 8. Variable Names Are Case-Sensitive
Python treats uppercase and lowercase letters as different characters. This means that variable names with different capitalization are treated as different variables.
For example, age, Age, and AGE may look similar, but Python considers them three separate names.
age = 20 Age = 30 AGE = 40 print(age) print(Age) print(AGE)
🔍 What happens here?
Python keeps the three names separate:
Age → 30
AGE → 40
⚠️ Important: age, Age, and AGE are not the same variable. Changing the capitalization changes the variable name.
▶ Output
20 30 40
🚨 A common beginner mistake
age = 20 print(Age)
Here, Age is not the same as age. If Age has not been created, Python will report a NameError.
🌟 Good Practice: Choose one consistent naming style and use it throughout your program. For normal Python variables, names such as student_name and total_marks are clear and easy to read.
🔗 9. Multiple Assignment
Python allows us to assign values to multiple variables in a single statement. This feature is called multiple assignment.
There are two useful forms of multiple assignment. Let’s look at them one by one.
① Assign the Same Value to Multiple Variables
We can assign the same value to several variables using a single statement.
x = y = z = 10 print(x) print(y) print(z)
🔍 Here, 10 is assigned to
all three variables.
x → 10
y → 10
z → 10
10 10 10
② Assign Different Values to Multiple Variables
Python can also assign different values to different variables in a single statement. The values are matched with the variables from left to right.
name, age, marks = "Ravi", 20, 85 print(name) print(age) print(marks)
🔍 Python matches the values with the variables in order:
name
→ “Ravi”
age
→ 20
marks
→ 85
Ravi 20 85
💡 Why is Multiple Assignment Useful?
Multiple assignment makes Python programs shorter and easier to read. Instead of writing several separate assignment statements, related values can be assigned together.
name, age, marks = "Ravi", 20, 85
⭐ Remember: When assigning different values, the number of variables and values should normally match. For example, a, b = 10, 20 works, but a, b = 10, 20, 30 causes an error.
🎯 In short: Python allows you to assign the same value to multiple variables or assign different values to multiple variables in one statement.
🔄 10. Swapping Variables in Python
Swapping means exchanging the values stored in two variables. Python makes this operation remarkably simple because two variables can exchange their values in a single statement.
Suppose a contains 10 and b contains 20. Let’s exchange their values.
a = 10 b = 20 a, b = b, a print(a) print(b)
🔍 How does this work?
The statement a, b = b, a tells Python to exchange the two values.
After: a → 20 b → 10
20 10
⚖️ How is this done in C, C++ and Java?
In C, C++ and Java, a common way to swap two values is to use a temporary variable. The temporary variable holds one value while the two original variables are exchanged.
temp = a; a = b; b = temp;
Step 1:
Store a in temp.
Step 2:
Put b into a.
Step 3:
Put the saved value in temp into b.
⭐ Python’s advantage: Python can exchange the values directly using a, b = b, a, without explicitly creating a temporary variable.
| Language | Common swapping approach |
|---|---|
| Python |
a, b = b, a
|
| C | Temporary variable |
| C++ | Temporary variable / standard utilities |
| Java | Temporary variable |
🎯 Remember: Python’s multiple-assignment feature makes swapping variables concise and easy to read. The statement a, b = b, a is one of the simple features that makes Python beginner-friendly.
🔒 11. What is a Constant?
So far, we have seen how variables can store values that may change during a program. Now let’s look at another useful concept — the constant.
💡 Definition: A constant is a value that is intended to remain unchanged throughout a program.
🏠 Think of it in real life:
Suppose a school has a maximum mark of 100. The program may use this value many times, but it is not normally expected to change. Such a value is a good candidate for a constant.
PI = 3.14159 MAX_MARKS = 100
Notice that the names are written using uppercase letters. This is the common Python convention for values that are intended to remain constant.
PI = 3.14159
⚠️ Important Python point: Python does not have a built-in const keyword for creating constants. Uppercase names such as PI and MAX_MARKS are a convention that tells other programmers, “This value is intended not to be changed.”
🔎 Can Python actually prevent the value from being changed?
No. Python itself does not enforce the uppercase name as a constant. The name can still be reassigned:
PI = 3.14159 PI = 3.14 print(PI)
3.14
🎯 The key idea: In Python, a constant is mainly a matter of programmer intention and naming convention. Writing a name in uppercase does not make it impossible to change.
➡️ In the next section, we’ll see how constants are represented in Python, C, C++ and Java and how their approaches are different.
⚖️ 13. Constants in Python vs C/C++/Java
Constants are values that are intended to remain unchanged while a program runs. Different programming languages provide different ways of representing such values.
Let’s compare how Python, C, C++ and Java handle constants.
🐍 Python
Python does not have a built-in const keyword. Programmers normally use UPPER_CASE names to indicate that a value is intended to remain unchanged.
PI = 3.14159
✔ Convention-based — Python does not prevent reassignment.
⚙️ C
In C, a constant can be created using the const qualifier.
const float PI = 3.14159;
The const qualifier tells the compiler that the object should not be modified through that name.
C also has another commonly seen mechanism:
#define PI 3.14159
💡 Note: #define creates a preprocessor
macro; it is different from a typed const object.
⚙️ C++
C++ commonly uses the const qualifier to define a value that should not be modified.
const double PI = 3.14159;
Here, const is part of the C++ language and the value cannot be modified through that object.
☕ Java
Java uses the final keyword for a variable that can be assigned only once.
final double PI = 3.14159;
Once PI has been assigned, it cannot be assigned another value.
📊 Quick Comparison
| Language | Common approach | Example |
|---|---|---|
| Python | Naming convention |
PI = 3.14159
|
| C |
const qualifier
|
const float PI = 3.14159;
|
| C++ |
const qualifier
|
const double PI = 3.14159;
|
| Java |
final keyword
|
final double PI = 3.14159;
|
⭐ Important difference: Python’s uppercase naming convention communicates programmer intention, but Python does not prevent reassignment. C and C++ provide const, while Java uses final for variables that should not be reassigned.
Python
→ UPPER_CASE convention
C
→ const
C++
→ const
Java
→ final
🎯 Takeaway: The idea of a constant is common across these languages, but the way it is expressed is different. Python relies mainly on convention, whereas C, C++ and Java provide language features that restrict reassignment.
🧪 18. Mini Project: Calculate the Area of a Circle
Let’s put what we have learned into practice with a small Python program. The program will ask the user to enter the radius of a circle and then calculate its area.
This simple example uses both a constant and variables.
📌 In this program:
PI → Constant
radius → Variable
area → Variable
📐 The formula for the area of a circle is:
Area = π × radius × radius
💻 Complete Python Program
PI = 3.14159
radius = float(input("Enter radius: "))
area = PI * radius * radius
print("Area =", area)
🔍 How Does the Program Work?
1. PI = 3.14159
Stores the approximate value of π using an uppercase name to
indicate that it is intended to be a constant.
2. radius = float(input(…))
Asks the user to enter the radius and stores the value in the
radius variable.
3. area = PI * radius * radius
Calculates the area using the formula.
4. print(“Area =”, area)
Displays the calculated area.
▶ Example Run
Enter radius: 5 Area = 78.53975
⭐ What did we use here?
✔ A constant: PI
✔ Variables: radius and area
✔ User input using input()
✔ Type conversion using float()
✔ Arithmetic operators for the calculation
🚀 Try It Yourself
Change the program so that it also calculates the
circumference of the circle.
Formula:
Circumference = 2 × π × radius
🎯 Takeaway: Even a small Python program can combine variables, constants, user input, calculations and output. Practicing with small programs like this is one of the best ways to become comfortable with Python.
📝 21. Practice Questions
Now it’s time to test your understanding of Python variables and constants. Try answering the following questions yourself before checking your notes or running the examples.
🚀 Try Yourself
Read each question carefully and try to answer it on your own.
Q1. What is a variable in Python?
Q2. Is variable declaration required before creating a variable in Python?
Q3. Why is the following variable name invalid?
2name = "Ravi"
Q4. Are age and Age the same variable in Python? Explain your answer.
Q5. Does Python have a built-in const keyword?
Q6. How are constants normally represented in Python?
Q7. What is the difference between the following two variables?
x = 10
and
X = 10
Q8. What is Final used for in Python?
Q9. Write a Python statement that assigns the value 100 to three variables named a, b and c.
Q10. Write one Python statement to assign “Ravi”, 20 and 85 to the variables name, age and marks.
Q11. What will be the output of the following program?
a = 10 b = 20 a, b = b, a print(a) print(b)
🏆 Challenge: Create a small Python program that stores your name, age and marks in variables and then prints them. Also create a constant called PASS_MARKS and use it in your program.
🌟 Don’t just read the examples —
try them yourself!
Every small program you write makes Python easier to understand.
