Curriculum
In this lesson, you will learn about keywords in python and some examples of how to use it.
Keywords are reserved words in python intended to perform specific functions. It cannot be used as a variable or a value in any sort of program/script. In python, there are a totally 33 reserved or keywords that cannot be used as a variable or value.
To find out the list of keywords, run the following code in the python shell.
import keyword keyword.kwlist
The following table helps us understand the keywords and their respective functions in detail.
S.No. | Keywords | Description |
1 | and | Performs logical and operation between two variables |
2 | as | Used to create an alias |
3 | assert | Used as a debugging keyword to catch errors |
4 | break | To break out of a loop |
5 | class | To declare class |
6 | continue | Used to continue the loop |
7 | def | To declare a function |
8 | del | Used to delete an object |
9 | elif | Conditional statement and short form of else if |
10 | else | Conditional statement |
11 | except | Used in exception handling |
12 | finally | To execute a code even when there is an exception |
13 | for | Conditional statement for loop |
14 | from | Used to import specific parts |
15 | global | Declaring a global variable |
16 | if | Conditional statement if |
17 | import | Used to import a module |
18 | in | Similar to a membership operator, used to check whether the value is present in a list or tuple |
19 | is | To check whether the two variables are equal |
20 | lambda | Used to create an anonymous function |
21 | None | Represents a Null value |
22 | nonlocal | Declaring a nonlocal variable |
23 | not | Performs logical NOT operation |
24 | or | Performs logical OR operation |
25 | pass | Does nothing, the null statement |
26 | raise | To raise an exception |
27 | return | To return a value |
28 | while | A conditional statement, while loop |
29 | with | To simplify the exception handling |
30 | yield | To end a function. |
31 | FALSE | Represents a Boolean false |
32 | TRUE | Represents a Boolean true |
33 | __peg_parser__ | Used to automatically generate a parser |
Input: Let’s use some of these keywords in a program and understand how it works.
def keywords_in_python(): a=57 b=75 while(a<60): print(a) a= a+1 if a is b: print('A is equal to B') else: print('A is not equal to b') keywords_in_python()
Output:
57 58 59 A is not equal to b
Learn more about python programming and other programming languages at DeveloperPublish.com.