Python Keywords
Keywords in Python are reserved words that have special meanings and are used for specific purposes. These words are part of the Python syntax and cannot be used as identifiers (variable names, function names, etc.). Python has a set of keywords that are reserved, and you must use them only as intended.
List of Python Keywords
Python has the following built-in keywords:
Keyword | Description |
---|---|
False | Represents the boolean value False. |
None | Represents a null value or "no value at all". |
True | Represents the boolean value True. |
and | Logical operator used to combine conditional statements. |
as | Used to create an alias while importing a module. |
assert | Used for debugging purposes to test if a condition is true. |
async | Used to declare a function as asynchronous. |
await | Used to pause the execution of async functions. |
break | Terminates the nearest enclosing loop. |
class | Used to define a new user-defined class. |
continue | Skips the rest of the loop and starts the next iteration. |
def | Used to define a new function. |
del | Deletes objects in Python. |
elif | Used for conditional branching; short for "else if". |
else | Defines the alternative path in an if statement. |
except | Used to catch and handle exceptions. |
finally | Defines a block of code to be executed after a try block, regardless of whether an exception occurred. |
for | Used to create a for loop. |
from | Used to import specific parts of a module. |
global | Used to declare a global variable. |
if | Used to create a conditional statement. |
import | Used to import a module. |
in | Used to check if a value is present in a sequence (e.g., lists, tuples). |
is | Used to test object identity. |
lambda | Used to create anonymous functions. |
nonlocal | Used to declare a non-local variable. |
not | Logical operator to invert the truth value. |
or | Logical operator used in conditional statements. |
pass | A null statement; a placeholder for future code. |
raise | Used to raise an exception. |
return | Exits a function and returns a value. |
try | Used to wrap a block of code that may throw an exception. |
while | Used to create a while loop. |
with | Used to wrap the execution of a block of code with methods defined by a context manager. |
yield | Used to return a generator. |
Understanding Python Keywords
Let's go through some of the most commonly used Python keywords with examples:
1. if, elif, and else
The if
, elif
, and else
keywords are used for conditional branching in Python.
Code Example
# Conditional statements
x = 10
if x > 0:
print("Positive")
elif x < 0:
print("Negative")
else:
print("Zero")
Output
2. for and while
The for
and while
keywords are used for looping in Python.
Code Example
# Using for and while loops
for i in range(3):
print("For Loop:", i)
j = 0
while j < 3:
print("While Loop:", j)
j += 1
Output
3. def and return
The def
keyword is used to define a function, and return
is used to return a value from the function.
Code Example
# Function definition and return
def add_numbers(a, b):
return a + b
result = add_numbers(5, 7)
print("Sum:", result)
Output
4. try, except, and finally
The try
block is used to catch exceptions, except
handles the exception, and finally
is executed regardless of whether an exception occurred or not.
Code Example
# Exception handling
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Execution complete.")
Output
5. class and object
The class
keyword is used to define a class, and objects are instances of classes.
Code Example
# Defining a class and creating an object
class Car:
def __init__(self, model, year):
self.model = model
self.year = year
my_car = Car("Toyota", 2020)
print("Car Model:", my_car.model)
print("Car Year:", my_car.year)
Output
Python keywords are essential to understanding the syntax and structure of Python code. These keywords have predefined meanings and uses, making it crucial to use them correctly. Understanding how to use Python keywords effectively will help you write clean and efficient Python code.