Python Mastery – From Basics to Brilliance / Python Fundamentals

Python Keywords and Identifiers

πŸ”‘ Keywords

Keywords are reserved words in Python that have a special meaning.

Examples:

if
else
for
while
def
class
return
import
True
False
None
and
or
not

You cannot use keywords as variable or function names.

if = 10   # ❌ Invalid

You can check Python's keywords using:

import keyword

print(keyword.kwlist) 
['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']

🏷️ Identifiers

Identifiers are names given by the programmer to variables, functions, classes, etc.

name = "Tisha"
age = 23

def greet():
    print("Hello")

Here, name, age, and greet are identifiers.

Rules for Identifiers

βœ… Can contain letters, numbers and _

student_name
student123

❌ Cannot start with a number

123student  # Invalid

❌ Cannot contain spaces or special characters

student name   # Invalid
student-name   # Invalid

❌ Cannot be a keyword

class = 10     # Invalid

πŸ”  Identifiers are case-sensitive:

name = "Tisha"
Name = "Rahul"

name and Name are different identifiers.


βš–οΈ Keywords vs Identifiers

Keywords πŸ”‘

Identifiers 🏷️

Reserved by Python

Created by the programmer

Have a special meaning

Used to name things

Cannot be used as variable names

Used for variables, functions, classes, etc.

if, for, class, return

name, age, student

⭐ Remember

Keyword = Python's reserved word
Identifier = Programmer's name