- Python Keywords ā Reserved words and their purpose
- Python Identifiers ā Naming rules and conventions
- Python Variables ā How to store and manage data
- Rules for Naming ā What's valid and what's not
- Variables vs Identifiers ā Understanding the difference
- Hands-on Practice ā Write and run Python code
- Interactive Quiz ā Test your knowledge
1. Python Keywords ā The Reserved Words
Keywords are unique reserved words in Python that cannot be used as variable names, function names, or other identifiers. They are the building blocks of the Python language ā the vocabulary that Python understands.
Each keyword serves a specific purpose, and together they form the syntax of Python programs. As of Python 3.11.2, there are 35 keywords that Python supports.
š” Important: All keywords in Python are case-sensitive. So IF is not the same as if. Be careful when using them!
List of Python Keywords:
All 35 Python Keywords: False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise
How to List All Keywords:
# Python script to list all keywords import keyword print(keyword.kwlist)
Output: ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Using help() to View Keywords:
>>> help("keywords")
Here is a list of the Python keywords. Enter any keyword to get more help.
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
š Note: The list of Python keywords may change in future releases. New keywords may be added, and old ones may be discontinued.
2. Python Identifiers ā Naming Your Code
Identifiers are the names given to entities like variables, functions, classes, and labels in a program. When you assign a name to a programmable entity in Python, it's technically called an identifier.
Identifiers must be different from keywords. Keywords cannot be used as identifiers. Python has a set of rules for creating meaningful identifiers.
Rules for Naming Identifiers:
- Use letters, digits, or underscores: Combination of alphabets (a-z, A-Z), digits (0-9), or underscore (_)
- Cannot start with a digit: Digits cannot be used as the first character
- Cannot use keywords: Keywords are reserved and cannot be used as identifiers
- No special symbols: Symbols like !, @, #, $, %, etc. are not allowed
- Case-sensitive:
myVarandmyvarare different
ā Valid Identifiers
shapeClassshape_1upload_shape_to_db_privatemyVar123
ā Invalid Identifiers
1shape (starts with digit)shape class (has space)class (is a keyword)my-var (has hyphen)@name (has special symbol)
# Valid identifiers >>> shapeClass = 1 ā Works fine! # Invalid identifiers (will raise errors) >>> shape Class = 1 SyntaxError: invalid syntax >>> 1shape = 1 SyntaxError: invalid decimal literal
3. Python Variables ā Storing Your Data
Variables are names that refer to memory locations where data is stored. They are the most fundamental concept in programming ā they allow you to store, retrieve, and manipulate data.
Key Facts About Python Variables:
š No Declaration Needed
Variables don't require explicit declaration. You just assign a value and the variable is created.
š Dynamic Typing
Variables can hold different types of objects at different times. They are references to objects, not the objects themselves.
š§ Object-Oriented
Every variable points to an object that has a type, value, and reference count.
Understanding Python Variables:
# Variable assignment and type checking
>>> age = 20
>>> type(age)
<class 'int'>
>>> name = 'Python'
>>> type(name)
<class 'str'>
>>> name = {'Python', 'C', 'C++'}
>>> type(name)
<class 'set'>
How Variables Work in Python:
- Creation: When you assign a value, Python creates an object in memory
- Reference: The variable name becomes a reference to that object
- Rebinding: When the value changes, Python creates a new object and updates the reference
- Garbage Collection: Old objects are automatically cleaned up when no longer referenced
# Memory address changes when value changes >>> age = 20 >>> id(age) 1716585200 >>> age = 11 >>> id(age) 1716585232 # Different memory address = new object!
4. Rules for Naming Python Variables
ā Legal Variable Names
myname = "Python"my_name = "Python"_my_name = "Python"myName = "Python"MYNAME = "Python"myname2 = "Python"
ā Illegal Variable Names
2myname = "Python" (starts with digit)my-name = "Python" (has hyphen)my var = "Python" (has space)
# ā Valid variable names myname = "Python" my_name = "Python" _my_name = "Python" myName = "Python" MYNAME = "Python" myname2 = "Python" # ā Invalid variable names (will raise errors) 2myname = "Python" # SyntaxError my-name = "Python" # SyntaxError my var = "Python" # SyntaxError
5. Variables vs Identifiers ā What's the Difference?
š” Simple way to remember: All variables are identifiers, but not all identifiers are variables! Identifiers are the general term for any named entity in Python.
Try It Yourself!
Now it's your turn! Write and run Python code directly in your browser:
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Name: Python Learner
Age: 25
Private: This is private
Language: Python
Version: 3.11
Is Popular? True
Numbers: [1, 2, 3, 4]
x is 10 (type: int)
x is Hello (type: str)
x is 3.14 (type: float)
age = 20, id: 1716585200
age = 21, id: 1716585232
ā Experiment with your own variables!
šÆ Challenge Yourself!
⢠Create a variable with your favorite programming language
⢠Try creating an invalid identifier and see the error
⢠Check the type of different variables using type()
š You've Mastered Python Keywords, Identifiers & Variables!
You now understand the naming rules of Python ā keywords, identifiers, and variables. These are fundamental to writing clean, correct Python code!
Quick Quiz - Test Your Knowledge
Let's see what you've learned:
Frequently Asked Questions
š¤ What is the difference between a keyword and an identifier? ā¼
if, while, and def. You cannot use them as names. Identifiers are names you create for variables, functions, and classes. They follow specific naming rules and cannot be keywords.
š» Can I use a keyword as a variable name? ā¼
if or class as a variable name, Python will raise a syntax error. This is why you should always check if a word is a keyword before using it as a name.
šÆ Why are identifiers case-sensitive in Python? ā¼
myVar and myvar are two different identifiers. This allows you to create more precise and meaningful names, following the Python convention of using lowercase with underscores for variables (snake_case).
š What is the difference between variables and identifiers? ā¼
š Where to Go From Here
Now that you understand Python keywords, identifiers, and variables, here are some related topics to explore:
š£ Python Tokens
Learn about keywords, identifiers, and operators
š Data Types
Explore Python's built-in data types
š§ Operators
Deep dive into Python operators