Change language

Keywords in Python | Set 2

| |

16. try : this keyword is used to handle exceptions, is used to catch errors in the code, except for the keyword. The code in the try block is checked if there is any type of error other than the block being executed.

17. Except : As explained above, this works in conjunction with "trying" to catch exceptions.

18. promotion : Also used for exception handling to explicitly raise exceptions.

19. finally : no matter what is the result of the "try" block, the block called "finally" is always executed. Detailed article —  Python Exception Handling

20. for : This keyword is used for flow control and for loops.

21. while : Works similar to “for”, used for flow control and for loops.

22. pass : This is a null statement in python. Nothing happens when it is encountered. This is used to prevent indentation errors and is used as a placeholder

Detailed article —  imports, from and how

26. lambda : This keyword is used to create inline return functions without internal instructions. Detailed article —  map, filter, lambda

27. return : This keyword is used to return from a function. Detailed article —  yield keyword

29. with : This keyword is used to wrap the execution of a block of code in methods defined by the context manager. This keyword has little use in everyday programming.

30. in : This keyword is used to check if the container contains a value. This keyword is also used to traverse the container.

31. is : This keyword is used to check the identity of an object, that is, to check if both objects occupy the same memory space or not.

# Python code to demonstrate how it works
# this is

  
# using & quot; in & quot; to check

if ’s’ in ’pythonengineering’ :

  print ( "s is part of pythonengineering" )

else : print ( "s is not part of pythonengineering" )

 
# using & quot; in & quot; for looping

for i in ’pythonengineering’ :

  print (i, end = "" )

 

print ( "" )

 
# used to check the identity of the object
# the line is unchanged (cannot be changed after selection)
# hence occupy the same place in memory

print ( ’’  is ’’ )

 
# used to check the object’s identity
# dictionary is mutable (can be changed after selection)
# hence occupy different memory space

print ({} is {})

Output:

 s is part of pythonengineering pythonengineering True False 

32. global : This keyword is used to define a variable within a function that has global scope.

33. non-local : This keyword works similarly to the global one, but instead of the global one, this keyword declares a variable pointing to the variable of the outer enclosing function, in the case of nested functions.

# Python code to demonstrate how it works
# global and non-local

  
# initialize the variable globally

a = 10

  
# used to read the variable

def read ():

print (a)

  
# changing the value of the global variable

def mod1 ():

global

a = 5

 
# change the value of local variable only

def mod2 ():

a = 15

 
# read initial value
# prints 10
read ()

  
# call the mod 1 function to change the value
# change the value of the global a to 5
mod1 ()

 
# read changed value
# prints 5
read ()

 
# call mod 2 function to change the value
# changes the local a value to 15, does not affect the global value
mod2 ()

 
# read changed value
# prints 5 again
read ()

 
# demo not local
# inner loop that changes the value of the 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 ()

 
# demonstration without non-local
# the inner loop does not change the value of the outer a
# prints 5

print ( "Value of a without using nonlocal is:" , end = "")

def outer () :

a = 5

def inne r ():

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 

This article courtesy of Manjeet Singh (S. Nandini) . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method