Python Keywords
Python Keywords |

Python Keywords

Python Keywords are the reserved words used in this particular programming language conveying a special meaning to the compiler or interpreter. Each keyword having a special meaning and specific operation can’t be used as a variable name, function name, or any other identifier.

Here in this article ( Python Keywords ), we have compiled the list of important keywords used in the Python programming language. 

deftry.import iswhile class
continueexcept from globalwith
Orraiseasnonlocal yield
breakfinally pass lambda async
elififreturn assertawait
delelsein for Not

True: This specific keyword is used to represent the Boolean true. In case the condition is true, then it returns “True”. All non-zero values are treated as true in Python.

False: This specific keyword is used to represent the Boolean false. In case the condition is false, then it returns “False”. All zero values are treated as false in Python.

None : This specific keyword is used to denote the null value or void value. An empty or zero value in Python can be treated as ‘None’.

And: This specific keyword is a logical operator and is utilized to check the multiple conditions. It returns true in case both the mentioned conditions are true. 

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseTrue
  1. Or: This specific keyword is a logical operator and returns true in the case when one mentioned condition is true. 
ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

2. def: This particular keyword is used in the Python programming language to declare the defined functions. 

def xyz(a,b):  
    c = a+b  
    print(c)  
xyz(10,20) 

Output

30

3. continue: To stop the execution of the current iteration, this particular keyword can be used.

x = 0 
while x < 3:  
  x += 1   
  if x == 2:  
    continue  
  print(x) 

Output

1
3

4. break: The main function of this keyword is to terminate the loop execution and control transfer to the end of the loop. 

for x in range(5):  
    if(x==3):  
        break  
    print(x)  
print("End of program")  

Output

0
1
2
End of program

5. elif: The main function of this keyword is to check the multiple conditions. elif is the short form for else-if. In case the previous condition is false then you need to check until the true statement is discovered.

 marks = int(input("Enter the obtained marks:"))  
if(marks>=80):  
    print("Excellent")  
elif(marks<80 and marks>=70):  
    print("Very Good")  
elif(marks<70 and marks>=60):  
    print("Good")  
else:  
  print("Average")  

Output

Enter the obtained marks:83
Excellent

6. del: the main function of this keyword is to delete the reference of the object. 

a=10  
b=12 
c=15
d=20
del a  
print(b,c,d) 


Output

12 15 20

7. try: this specific keyword is utilized In Python for exception handling, used to catch the errors in the codes by using the ‘except’ keyword. The code is checked in the ‘try’ block and if there is any error then the ‘except’ block is executed. 

8. except: as explained above, this specific keyword is used along with the ‘try’ keyword to catch the exceptions. 

9. raise: This specific keyword is utilized through the exception in a forceful manner. 

10. finally: The main function of the ‘finally’ keyword is to create a block of code the execution of which will always take place no matter the errors are raised by the else block or not.

11. if: To represent the conditional statement is the main function of this keyword. It is the ‘if statement’ that decides the execution of a particular block. 

a = 20
if (a == 20):
    print ("a is 20")

Output

a is 20

12. else: The else statement is always used with the if statement in Python. The else block is executed in a case when if statement returns false.

n = 9 
if(n%2 == 0):  
    print("Even")  
else:  
print("odd") 

output

odd

13. import: When it comes to the main function of the ‘import’ keyword, it is utilized to import the modules in the current script of Python. A runnable Python code is contained in the module. 

import math  
print(math.sqrt(36))

output

6.0

14. from The ‘from’ keyword is utilized to import the particular function or attributes in the current script of Python.

from math import sqrt  
print(sqrt(36))  

Output

6.0

15. as: The main function of this specific keyword is to create a name alias. While importing a module it provides the user-define name. 

import calendar as cal 
print(cal.month_name[10])

Output

October

16. pass: This particular keyword is utilized in Python to execute nothing or create a placeholder for the future code. If an empty class or function is declared then it ill through an error, so to declare an empty class or function the ‘pass’ keyword is used. 

17. return: This particular keyword is utilized to return the result value or none to called function. 

def sum(x,y):  
    z = x+y  
    return z  
      
print("The sum is:",sum(30,15))  

Output

The sum is: 45

18. in: This particular keyword is utilized to check if there is any value in the container and can also be used to loop through the container.

my_list = [116, 18, 20, 39, 40, 60]
number = 18
if number in my_list:
    print("number is present")
else:
print("number is not present")

Output

number is present

19. is: To check if the two variables refer to the same object or not, the ‘is’ keyword is used. If the variables refer to the same object then it returns true otherwise false. 

a = 10
b = 18
c = 18
print(a is b) # it compare memory address of x and y 
print(b is c) # it compare memory address of x and z

Output

False
True

20. global: To create a global variable inside a function, the ‘global’ keyword is used in Python. The global keyword can be accessed by any function.

def my_func():  
    global a   
    a = 20  
    b = 20  
    c = a+b  
    print(c)  
my_func()  
def func():  
    print(a)  
func() 

Output

40
20

21. nonlocal: This particular keyword is similar to the global and is utilized to work with a variable inside the nested function. 

def outside_function():    
    x = 20     
    def inside_function():    
        nonlocal x    
        x = 30    
        print("Inner function: ",x)    
    inside_function()    
    print("Outer function: ",x)    
outside_function() 

Output

Inner function: 30
Outer function: 30

22. lambda: In case one wants to create an anonymous function in Python, then the ‘lambda’ keyword is to be used. It is an inline function that has no name. 

a = lambda x: x**2  
for i in range(1,7):  
  print(a(i))

Output

1
4
9
16
25
36

23. assert: This particular function is used for debugging purposes and to check the correctness of the code. In case the statement happens to be true then nothing happens but if the statement turns out to be false then “Assertion Error’ is raised. 

x = 9  
y = 0  
print('x is dividing by Zero')  
assert y != 0  
print(x / y)

Output

x is dividing by Zero
Traceback (most recent call last):
File “”, line 4, in
AssertionError

24. for: This specific keyword is utilized for iteration. To iterate over the sequences, the ‘for’ keyword is used in Python.

list = [1,2,3,4]  
for i in list:  
print(i) 

Output

1
2
3
4

25. while: A code block is repeatedly executed by the while loop while a particular condition holds true. 

a = 0  
while(a<8):  
    print(a)  
    a = a+1 

Output

0
1
2
3
4
5
6
7

26. with: Whenever working with the unmanaged resources, the ‘with’ keyword will be used. It allows one to make sure that a resource is ‘cleaned up’ when the code which has used it finishes running, no matter if the exceptions are thrown. 

27. yield: The yield keyword has the same usage as the return keyword, except that the generator will be returned by the function.

28. async: The async keyword is used with the def keyword so that an asynchronous function can be defined. Also, a function can be made asynchronous by adding the async keyword before the regular definition of a function. 

import asyncio  
async def main():  
    print ("Let's start ")  
    for _ in range(5):  
        await asyncio.sleep(1)  
        print ("Hi")  
    print ("Finished waiting.")  
asyncio.run(main())  

Output

Let’s start
Hi
Hi
Hi
Hi
Hi
Finished waiting.

29. await: The await keyword is used in the asynchronous functions so that a point can be specified in the function where the control is given back to the event loop for other functions to run. 

30. Not: This specific keyword is a logical operator and is used to invert the true value. 

31. class: This particular keyword is utilized to represent the class in Python. When we talk about class, it refers to the blueprint of the objects. It is nothing but the collection of the variables and methods. 

The above-mentioned was a list of important Python keywords that every beginner should know. 

Also visit, simiservice.com and our previous article

Rate this post