Predefined Reserved Words in Python - Keywords
Keywords
are words that are reserved by a programming language because they have a special meaning and are used to perform specific syntactic operations.
These keywords form the basic syntax rules of the programming language and cannot be used as names for variables, functions, etc.
Typically, keywords are used to control the flow of programs or define data structures.
Major Keywords in Python
The major keywords used in Python are as follows.
1. Conditional Statements: if, else, elif
Defines conditional statements that are executed only if a certain condition is met.
You must include a colon (:) at the end of the line where the keyword is used, and the code block that executes depending on the condition is defined by indentation.
if x > 0: # When x is greater than 0 print("Positive") elif x == 0: # When x is equal to 0 print("Zero") else: # When x is less than 0 print("Negative")
2. Loops: for, while
Defines loops that perform repeated actions.
You must include a colon (:) at the end of the line where the keyword is used, and the repetitive code block is defined by indentation.
for i in range(5): # Repeat 5 times from 0 to 4 print(i) while x < 10: # Repeat while x is less than 10 print(x) x += 1
3. Break Loop: break
Exits a loop when a certain condition is met within the loop.
for i in range(10): if i == 5: break # Break the loop when i is 5 print(i)
The above example stops the loop when i is 5, and prints numbers from 0 to 4.
4. Skip Iteration: continue
Skips the current iteration and proceeds to the next iteration when a certain condition is met within the loop.
for i in range(10): if i % 2 == 0: continue # Skip the iteration when i is even print(i)
The above example skips the iteration when i is even and prints only odd numbers.
5. Define Function: def
A function represents a block of code that performs a specific task. You define a function using the def
keyword.
def add(x, y): # Define a function add that takes two arguments x, y return x + y
The above example defines an add function that returns the sum of two arguments x and y.
6. Return Result from Function: return
The return keyword returns the result of the function's execution.
def add(x, y): return x + y # Return the sum of x and y
7. Define Class: class
A class is a user-defined data type that bundles data and functions that operate on the data.
The below example defines a Dog
class and initializes the name
attribute using the __init__
method.
Classes are a fundamental concept in Python and will be covered in detail in a later lesson.
class Dog: # Define the Dog class def __init__(self, name): self.name = name
8. Handle Exceptions: try, except
Handles exceptional situations in the code.
If an exception occurs within the try
block, it moves to the except
block to handle the exception.
Below code handles ZeroDivisionError
, an exception that occurs when dividing by zero.
try: print(10 / 0) # Exception occurs when dividing by zero except ZeroDivisionError: print("Cannot divide by zero")
The above code handles the exception of dividing by zero and prints "Cannot divide by zero."
Aside from these, Python offers a wide variety of keywords that help control the program flow and process data.
Coding Practice
Enter the code emphasized in the practice screen: random_number = random.randint(1, 10)
.
random.randint(1, 10) generates a random integer between 1 and 10.
In Python, the 'if', 'else', 'elif' keywords are used to define conditional statements.
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result