Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In the previous lessons, we learned how to write Python code and follow Python's syntax rules.
Now we are ready to learn one of the most important concepts in programming:
Variables allow us to store information and use it later in our program.
By the end of this lesson, you will understand:
snake_case namingkeyword moduleA variable is a name that refers to a value in a Python program.
Think about your kitchen.
You may have containers with labels such as:
The label helps you identify what is inside each container.
Variables work in a similar way.
For example:
Here:
student_name is the variable name."Amit" is the value.age is another variable name.18 is its value.You can think of it like this:
Don't worry about the complicated details of computer memory yet.
For now, remember:
Python does not require a separate variable declaration.
You can create a variable simply by assigning a value to a name.
Python understands that:
You can then use the variables:
Output:
A variable can refer to different kinds of values.
These values represent different data types:
| Variable | Value | Type |
|---|---|---|
name | "Sumit" | String |
age | 25 | Integer |
height | 5.8 | Float |
is_student | True | Boolean |
Python determines the type from the value.
You don't have to write the type when creating the variable.
A variable can be assigned a new value.
Output:
The variable age now refers to the new value 19.
A variable can also refer to a value of a different type:
This is possible in Python because Python is dynamically typed.
You don't need to declare:
Instead, simply write:
An identifier is a name used to identify something in Python code.
For example:
student_name is an identifier.
Identifiers can be used for things such as:
So:
For beginners, you can think of a variable name as one common type of identifier.
Python has specific rules for naming variables.
Let's learn them one by one.
A variable name can start with:
A-Z or a-z_Examples:
All three are valid.
This is invalid:
Python does not allow a variable name to begin with a digit.
But numbers can appear after the first character:
These are valid.
Python variable names can contain:
Examples:
Special characters are not allowed.
For example:
These are invalid variable names.
This is invalid:
Python interprets the space as separating different parts of the statement.
Use an underscore instead:
This is invalid:
Why?
Because Python uses - as the subtraction operator.
For example:
So Python does not treat student-name as one variable name.
Use an underscore:
Python treats uppercase and lowercase letters as different.
For example:
These are three different names.
are not the same identifier.
For beginners, it is better to avoid creating multiple variables that differ only by capitalization.
Instead of:
prefer clear and different names.
Let's look at some examples.
All of these follow Python's basic naming rules.
However, valid does not always mean recommended.
For regular variables, Python programmers usually prefer snake_case.
For example:
is generally clearer than:
Why are they invalid?
| Name | Problem |
|---|---|
2myname | Starts with a number |
my-name | Contains - |
my name | Contains a space |
@my_name | Contains @ |
&my_name | Contains & |
Python has some special words that already have a meaning in the language.
These words are called keywords.
For example:
These words are part of Python's syntax.
Therefore, you cannot use them as normal variable names.
For example:
This is invalid.
Similarly:
is invalid.
Python will report a syntax error.
Python's keyword list can be checked directly from Python.
Run:
This displays the keywords supported by your installed Python version.
You can also count them:
Don't memorize every keyword immediately.
As you learn Python, you will naturally become familiar with the most commonly used ones.
Some important keywords include:
The exact keyword list can vary between Python versions, so checking keyword.kwlist is more reliable than memorizing a fixed number.
Python provides the keyword module for this purpose.
Output:
Because class is a Python keyword.
Now try:
Output:
Because student is not a Python keyword.
returns:
True → the word is a keywordFalse → the word is not a keywordPython has official style recommendations called PEP 8.
PEP 8 helps developers write code that is easier to read and maintain.
Naming conventions are different from syntax rules.
For example:
is a valid Python identifier.
But for a normal variable, PEP 8 recommends:
This style is called snake_case.
Use lowercase letters with underscores between words.
Constants are values that are intended to remain unchanged.
Python commonly uses uppercase names with underscores for constants.
This is a naming convention, not a rule that prevents the value from changing.
For example:
Python does not automatically stop you.
The uppercase name simply communicates:
Class names commonly use PascalCase.
Each word starts with a capital letter.
Example:
Another example:
Notice:
| Identifier | Valid? | Explanation |
|---|---|---|
student_marks | ✅ Yes | Valid and follows snake_case |
_internal_id | ✅ Yes | Can start with _ |
batch2026 | ✅ Yes | Digits can appear after the first character |
2026batch | ❌ No | Cannot start with a number |
user-name | ❌ No | - is not allowed in identifiers |
total marks | ❌ No | Spaces are not allowed |
class | ❌ No | class is a keyword |
scholar#id | ❌ No | # starts a comment |
isidentifier()Python strings have a useful method called:
It checks whether a string follows Python's identifier naming rules.
Example:
Output:
Now:
Output:
isidentifier() checks whether the name has valid identifier syntax.
It does not tell you whether the name is a Python keyword.
For example:
Output:
But:
is still not allowed as a variable name because it is a keyword.
So for a complete check, you can use both:
Output:
A technically valid name is not always a good name.
Compare:
with:
The second one immediately tells us what 450 represents.
A good variable name should help another person understand your code without needing to guess.
| Practice | ✅ Do | ❌ Don't |
|---|---|---|
| Clarity | daily_temperature = 32 | t = 32 |
| Style | student_count | studentCount |
| Spaces | total_marks | total marks |
| Hyphens | user_name | user-name |
| Keywords | class_name | class |
| Meaningful names | total_price | x123 |
| Length | student_count | Extremely long names |
Don't make every variable name extremely short.
But also don't create unnecessarily long names.
Good:
Usually not helpful:
Let's put the concepts together.
Output:
This example uses:
snake_caseprint()Let's review everything we learned.
A variable is a name that refers to a value.
An identifier is a valid name used in Python code.
A variable name:
__@, $, %, #| Purpose | Recommended Style | Example |
|---|---|---|
| Variables | snake_case | student_name |
| Functions | snake_case | calculate_total() |
| Constants | UPPER_CASE | MAX_ATTEMPTS |
| Classes | PascalCase | StudentRecord |
A.
B.
C.
D.
Create a file named:
Then write:
This small program checks three things:
For example:
Modify the program and add these names:
Before running the program, predict which names will be valid.
Then run the program and compare your answers.
In the next lesson, we will continue your Python learning journey with Creating a Variable (2: Variables).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Comments & Best Practices | Creating a Variable |
Test your understanding of this lesson with 4 questions. Each question has one correct answer.