![]() |
VOOZH | about |
Python uses keywords and identifiers to create and organize programs. Keywords are predefined words that Python recognizes and reserves for specific purposes, while identifiers are names created by programmers to represent variables, functions, classes, and other objects.
Keywords are predefined words that Python recognizes as part of its syntax. They are used to perform specific tasks, such as defining functions, controlling program flow, and handling exceptions.
The keyword module provides:
| Category | Keywords |
|---|---|
Value Keywords | |
| Operator Keywords | and, or, not, is, in |
Control Flow Keywords | if, else, elif, for, while, break, continue, pass, try, except, finally, raise, assert |
| Function and Class | def, return, lambda, yield, class |
| Context Management | with, as |
| Import and Module | import, from |
| Scope and Namespace | global, nonlocal |
| Async Programming | async, await |
List of all the keyword names can be accessed using the below code:
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',...
Identifiers are user-defined names used to identify and reference program elements such as variables, functions, classes, modules, and objects. They help programmers organize and access different components of a program and must follow Python's naming rules.
Note: An identifier is a general name used for variables, functions, classes, and other objects in Python, whereas a variable is a specific type of identifier used to store data. Therefore, every variable is an identifier, but not every identifier is a variable.
Valid identifiers:
- var1
- _var1
- _1_var
- var_1
Invalid Identifiers
- !var1
- 1var
- 1_var
- var#1
- var 1
Example 1: Example of and, or, not, True, False keywords.
example of True, False, and, or, not keywords True True True
Example 2: Example of a break, continue keywords and identifier.
1 2 3 4 5
Example 3: example of for, in, if, elif, and else keywords.
One Two else block execute else block execute
Example 4: def, if and else keywords.
given number is even
Example 5: Example of try, except, raise.
0.1 Exception raises None
Example 6: Example of a lambda keyword.
2 3 4 5 6
Example 7: use of return keyword.
5
Example 8: use of a del keyword.
['a', 'b', 'c', 'd', 'e'] ['a', 'b', 'd', 'e']
Example 9: use of global keyword.
10
Example 10: example of yield keyword.
1 2 3 4 5 6
Example 11: Use of assert keyword.
Output:
AssertionError: List is empty.Example 12: Use of pass keyword
Example 13: Use of finally keyword
Inside try block Inside finally block Inside Exception block Inside finally block
Example 14: Use of import keyword
factorial of 5 is : 120
Example 15: Use of "is" keyword
True False
Example 16: Use of from keyword
gcd of 345 and 675 is : 15
Example 17: Use of async and await keyword
120
Explanation: