Python keywords Part-2

Python keywords Part-2


In this article we will explore more about the python keywords.

Python keyword part-1

16. try: This keyword specially used to catch the exceptions.The code within the try clause will be executed statement by statement. If an exception occurs or found then the rest of the try block will be skipped and the except clause will be executed.

17. except: We have already mentioned in the try part that except keyword works with the try. This is also used to catch the exceptions.

18. raise: This keyword is case sensitive in python. it is used to raise an Exception/Error with a customized message and stops the execution of the programs. you will have a clear knowledge when you will work with the input validations.

19. finally: This keyword is used to handle the situation. this keyword executed after the try and except block.The finally block always executes after normal termination of try block or after try block terminates due to some exception.

20. for: We use this keyword for the looping as well as for the control flow.

21. while: Like for it is also use for the looping and control flow.

22. pass: It is nothing but used as a placeholder for future code. nothing happens, when the pass statement is executed, but it will pretend the indentation error (empty code) which is not allowed in looping, function definitions.

23. import : This statement is used to include a particular module into current program.

24. from : Generally used with import, from is used to import particular functionality from the module we import.

25. as : This keyword is used to create the alias for the module imported. i.e giving a new name to the imported module.. E.g import pandas as numpy.

26. lambda : This keyword is used to make inline returning functions with no statements allowed internally. That means you can write your code of return type function in a single line.

27. return : This keyword is used to return from the function. It is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller and any statement after the return is not allowed.


28. yield: It is an keyword that is used to return from a function without destroying the states of its local variable and when the function is called, the execution starts from the last yield statement. If a function contains yield keyword then it termed as generator.

29. with : This keyword is used to wrap the execution of block of code within methods defined by context manager or used when working with unmanaged resources (like file streams).This keyword is not used much in day to day programming.

30. in : This keyword is used to check if a container contains a value or not. This keyword is also used to loop through the container. 

31. is : This keyword is used to test object identity, i.e to check whethe both the objects takes the same memory location or not.





output :





32. global : This keyword is used to define a variable inside the function to be of a global scope.
33. non-local : This keyword works similar to the global, but rather than global, this keyword declares a variable to point to variable of outside enclosing function, in case of nested functions.

# Python code to demonstrate working of 
# global and non local 
  
#initializing variable globally 
a = 10
  
# used to read the variable 
def read(): 
    print (a) 
  
# changing the value of globally defined variable 
def mod1(): 
    global a  
    a = 5
  
# changing value of only local variable 
def mod2(): 
    a = 15
  
# reading initial value of a 
# prints 10 
read() 
  
# calling mod 1 function to modify value 
# modifies value of global a to 5 
mod1() 
  
# reading modified value 
# prints 5 
read() 
  
# calling mod 2 function to modify value 
# modifies value of local a to 15, doesn't effect global value 
mod2() 
  
# reading modified value 
# again prints 5 
read() 
  
# demonstrating non local  
# inner loop changing the value of outer a 
# prints 10 
print ("Value of a using nonlocal is : ",end="") 
def outer(): 
    a = 5
    def inner(): 
        nonlocal a  
        a = 10
    inner() 
    print (a) 
  
outer() 
  
# demonstrating without non local  
# inner loop not changing the value of outer a 
# prints 5 
print ("Value of a without using nonlocal is : ",end="") 
def outer(): 
    a = 5
    def inner(): 
        a = 10
    inner() 
    print (a) 
  
outer() 

output: 

10
 5
 5
 Value of a using nonlocal is : 10

 Value of a without using nonlocal is : 5








#learn python for beginners
#python online training
#django course
#advanced python course
#python course fees
#introduction to data science in python
#free python tutorial
#learn python basics
#python study
#python and ai
#python computer science
#learn python step by step
#best course to learn python
#learn python with projects
#python artificial intelligence tutorial
 #micropython
 #python for data science
 #data science from scratch
 #micropython esp32
 #machine learning using python
 #matplotlib
 #python 3
 #scikit learn
 #learn python for data analysis free

 #learn python free
 #best sites to learn python
 #free python tutorial
 #best free online python course
 #python free online course

































Post a Comment

2 Comments

If you have any doubt, Please let me know.