πŸ”‘ Python Keywords β€” The Core Building Blocks of the Language

Introduction 🌟

Python keywords are special reserved words that have predefined meanings in the Python language. They cannot be used as variable names or identifiers because they control the structure, logic, and flow of a program.

Note

πŸ’‘ Think of keywords as the grammar rules of Python β€” they define how code should behave.

1. What Are Python Keywords? πŸ€”

Keywords are case-sensitive and always written in lowercase. Python 3 has 35 keywords (this number may change in future versions).

You can list all keywords using:

list_keywords.py

import keyword
print(keyword.kwlist)

2. Complete List of Python Keywords πŸ“‹

KeywordPurpose
FalseBoolean value false
NoneRepresents null value
TrueBoolean value true
andLogical AND
asAlias for modules
assertDebugging tool
asyncAsynchronous functions
awaitUsed inside async functions
breakExit a loop
classDefine a class
continueSkip current loop cycle
defDefine a function
delDelete an object
elifElse-if condition
elseElse condition
exceptHandle exceptions
finallyRun code after try/except
forLooping statement
fromImport specific module parts
globalDeclare global variable
ifConditional statement
importImport a module
inCheck membership
isIdentity operator
lambdaCreate small anonymous functions
nonlocalUse non-local variable
notLogical negation
orLogical OR
passDo nothing placeholder
raiseRaise an exception
returnReturn from a function
tryAttempt risky code block
whileWhile loop
withContext manager
yieldPause a generator function

Note

🧠 These keywords are reserved β€” you cannot create variables like if = 10 orclass = "hello".

3. Using Some Important Keywords πŸ§ͺ

if / elif / else β€” Conditional Logic

if_else.py

x = 10
if x > 0:
    print("Positive")
elif x == 0:
    print("Zero")
else:
    print("Negative")

for β€” Looping

for_loop.py

for i in range(3):
    print("Hello")

def β€” Creating a Function

function.py

def greet():
    print("Hello from Python!")

greet()

class β€” Creating a Class

class_example.py

class Person:
    def __init__(self, name):
        self.name = name

p = Person("Sathish")
print(p.name)

try / except β€” Error Handling

try_except.py

try:
    print(10 / 0)
except ZeroDivisionError:
    print("Cannot divide by zero!")

4. Checking If a Word Is a Keyword πŸ”

You can check whether a word is a Python keyword using keyword.iskeyword().

check_keyword.py

import keyword

print(keyword.iskeyword("if"))    # True
print(keyword.iskeyword("hello")) # False

5. Tips for Working With Keywords πŸ’‘

  • Always use lowercase β€” keywords are case-sensitive.
  • Never use keywords as variable, class, or function names.
  • Use descriptive variable names to avoid conflicts.
  • Keep this list handy as you learn Python syntax.

Conclusion πŸŽ‰

>>β€œKeywords form the grammar of your code β€” master them, and your programs gain clarity and power.” πŸ”₯

You're now familiar with every Python keyword and how to use them. Want a tutorial on operators, variables, data types, loops, functions, or something else? Just ask! 😊