Python keywords are reserved words used for specific purposes that have specific meanings and functions, and these keywords cannot be used for anything other than their specific use. These keywords are always available in Python, which means you don't need to import them into the code.
The following is the list of 35 keywords available in Python
| False | await | else | import | pass |
| None | break | except | in | raise |
| True | class | finally | is | return |
| and | continue | for | lambda | try |
| as | def | from | nonlocal | while |
| assert | del | global | not | with |
| async | elif | if | or | yield |
Let us discuss these keywords with the help of examples.
These Python keywords are utilized for decision-making and looping.
These keywords are used to execute different blocks on the basis of conditions.
| Keyword | Description |
|---|---|
| if | This keyword is used to execute a block if the condition is True. |
| elif | This keyword is used to specify additional conditions if the previous ones fail. |
| else | This keyword is used to execute a block if all previous conditions are False. |
Let us see an example showing how to use these keywords.
Output:
Equal to 20
Explanation:
In the above example, we have initialized a variable and used the conditional statement keywords like if, elif, and else to check for the given condition and execute the code accordingly.
These keywords are used to execute code repeatedly.
| Keyword | Description |
|---|---|
| for | This keyword is used to iterate over a sequence, such as list, tuple, dictionary, etc. |
| while | This keyword is used to repeat execution while a condition remains True. |
Let us see an example showing how to use these keywords.
Output:
Given List: ['apples', 'bread', 'eggs', 'milk', 'coffee beans', 'tomatoes'] Iterating Over List using 'for' Loop: apples bread eggs milk coffee beans tomatoes Iterating Over List using 'while' Loop: apples bread eggs milk coffee beans tomatoes
Explanation:
In the above example, we have initialized a list and make use of the looping constructs like for and while to iterate over it.
The keywords under this classification are used to change the flow of execution inside a loop.
| Keyword | Description |
|---|---|
| break | This keyword immediately exits the loop. |
| continue | This keyword is used to skip the current iteration and moves to the next. |
| pass | This keyword acts as a placeholder (does nothing). |
Let us see an example showing how to use these keywords.
Output:
1 3 5 7 9 11 13
Explanation:
In the above example, we have created a 'for' loop to print the numbers ranging from 0 to 20. Inside this loop, we have used the loop control keywords to change the flow of execution inside the loop. For instance, we have used the break keyword to terminate the entire loop when i becomes 14. The continue keyword is used to skips the loop for even numbers. We have used the pass keyword as a placeholder where the code can be added later.
The keywords under this category are used to define functions, classes, and return values.
| Keyword | Description |
|---|---|
| def | This keyword is used to define a function. |
| return | This keyword is used to return a value from a function. |
| yield | This keyword returns a generator object. |
| lambda | This keyword is used to define anonymous function. |
| class | This keyword allows us to define a class. |
Let us see some examples showing how to use these keywords.
Let's consider an example to demonstrate the def keyword in Python.
Output:
Welcome to Tpoint Tech!
Explanation:
In the above example, we have defined a function to print a simple statement using the def keyword.
Let's take an example to demonstrate the return and yield keyword in Python.
Output:
Addition of 12 and 5 is 17 Generated Number: 0 Generated Number: 1 Generated Number: 2 Generated Number: 3 Generated Number: 4
Explanation:
In the above example, we have defined a function as add_numbers(), where we have used the return keyword to return the sum of the numbers passed to it. We have then defined another function as generate_number(), where we have used the yield keyword to return the generator object containing the list of numbers generated.
Let us take an example to demonstrate the lambda keyword in Python.
Output:
Square of 12 is 144
Explanation:
In the above example, we have used the lambda keyword to define anonymous function to return the square of the number passed to it.
Let's take an example to demonstrate the class keyword in Python.
Explanation:
In this example, we have used the class keyword to declare a user-defined class using the keyword called class.
The keywords under this category are used to handle errors and exceptions.
| Keyword | Description |
|---|---|
| try | This keyword is used to define a block to catch errors |
| except | This keyword is used to handle exceptions |
| finally | This keyword helps execute after try-except |
| raise | This keyword is used to raise an exception manually |
| assert | This keyword is used to define debugging statement |
Let us see an example showing how to use these keywords.
Output:
This will always execute For x = 6: 1.6666666666666667 Assertion Error: x must be a positive number This will always execute For x = 0: None Assertion Error: x must be a positive number This will always execute For x = -6: None
Explanation:
In the above example, we have defined a function. Inside this function, we have used the keywords like try, except, finally, raise, and assert to handle the different errors and exceptions that might raise during the execution of the function.
The keywords under this category are used for variable access and modification.
| Keyword | Description |
|---|---|
| global | This keyword is used to declare a global variable |
| nonlocal | This keyword is used to modify parent function variables |
Let us see some examples showing how to use these keywords.
Let's take an example to demonstrate the global keyword in Python.
Output:
19
Explanation:
In the above example, we have initialized a variable and defined a function. Inside this function, we have used the global keyword to declare the initialized variable as global and updated its value.
Let's consider an example to demonstrate the nonlocal keyword in Python.
Output:
Inner function: 25 Outer function: 25
Explanation:
In the above example, we defined a nested function. Inside the outer function, we have initialized a variable. Inside the inner function, we have used the nonlocal keyword to modify the variable of the parent function.
These keywords are used in logical operations and Boolean expressions.
| Keyword | Description |
|---|---|
| and | This keyword represents Logical AND |
| or | This keyword represents Logical OR |
| not | This keyword represents Logical NOT |
| True | This keyword represents Boolean True value |
| False | This keyword represents Boolean False value |
Let us see an example showing how to use these keywords.
Let's consider an example to demonstrate the logical operation keywords in python.
Output:
Both x and y are positive At least one of x or y is greater than 10 x is not equal to 0
Explanation:
In the above example, we have initialized two variables and used the and, or, and not keywords to check various conditions.
Let's consider an example to demonstrate the Boolean expression keywords in Python.
Output:
Is 24 even? True Is 57 even? False
Explanation:
In the above example, we have defined function to check for even number. Inside this function, we have used the True keyword to return that given variable is an even number and False in case of odd number.
The keywords under this category are used for module handling and importing functions.
| Keyword | Description |
|---|---|
| import | This keyword is used to import a module |
| from | This keyword allows us to import specific part of a module |
| as | This keyword is used to rename imported module |
Let us see an example showing how to use these keywords.
Explanation:
In the above example, we have used the import, from, and as keywords to handle the importing functions.
The keywords under this category are used for class inheritance and object relationships.
| Keyword | Description |
|---|---|
| is | This keyword is used to check if two objects are the same |
| in | This keyword is used to check membership in lists, tuples, etc. |
Let us see an example showing how to use these keywords.
Output:
p and q refer to the same object p is present in lst_1
Explanation:
In the above example, we have initialized two variables. We have then used the is and in keywords to check if both variables refer to the same object and member of a given list.
These keywords are used for asynchronous programming.
| Keyword | Description |
|---|---|
| async | This keyword is used to define an asynchronous function. |
| await | This keyword allows us to pause async function execution |
Let us see an example showing how to use these keywords.
Output:
Tpoint Tech
Explanation:
In the above example, we have imported the asyncio module. We have then used the async keyword while defining asynchronous functions. We have also used the await keyword to pause the execution of async functions.
As per Python 3.11, there are 35 keywords. Here, we are going to take an example to print all keywords in Python.
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']
Explanation:
In the above, we have used the kwlist attribute from the keyword module to get the list of keywords in Python.
Python also includes four soft keywords, which act like keywords only under certain conditions. Unlike regular keywords, these keywords can still be utilized in the form of variable names or function identifiers.
For instance, the keywords like match and case were introduced for the purpose of structural pattern matching. However, since they are soft keywords, we can used them as variable names in non-pattern-matching contexts.
Python keywords are reserved words having special meanings and are central to the syntax and structure of a language. They provide important programming concepts such as control flow, manipulation of data, and function declaration. Good knowledge of these keywords are important in writing clean, and readable code.
We request you to subscribe our newsletter for upcoming updates.