Keywords in Python | Set 2
Last Updated :
21 Apr, 2025
Python Keywords - Introduction
Keywords in Python | Set 1
More keywords:
16. try : This keyword is used for exception handling, used to catch the errors in the code using the keyword except. Code in "try" block is checked, if there is any type of error, except block is executed.
17. except : As explained above, this works together with "try" to catch exceptions.
18. raise : Also used for exception handling to explicitly raise exceptions.
19. finally : No matter what is result of the "try" block, block termed "finally" is always executed. Detailed article -Exception Handling in Python
20. for : This keyword is used to control flow and for looping.
21. while : Has a similar working like "for" , used to control flow and for looping.
22. pass : It is the null statement in python. Nothing happens when this is encountered. This is used to prevent indentation errors and used as a placeholder
Detailed Article - for, while, pass
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 imported.
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 math as mymath.
Detailed Article - import, from and as
26. lambda : This keyword is used to make inline returning functions with no statements allowed internally. Detailed Article - map, filter, lambda
27. return : This keyword is used to return from the function. Detailed article - Return values in Python.
28. yield : This keyword is used like return statement but is used to return a generator. Detailed Article - yield keyword
29. with : This keyword is used to wrap the execution of block of code within methods defined by context manager.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. This keyword is also used to loop through the container.
31. is : This keyword is used to test object identity, i.e to check if both the objects take same memory location or not.
Python
# Python code to demonstrate working of
# in and is
# using "in" to check
if 's' in 'geeksforgeeks':
print ("s is part of geeksforgeeks")
else : print ("s is not part of geeksforgeeks")
# using "in" to loop through
for i in 'geeksforgeeks':
print (i,end=" ")
print ("\r")
# using is to check object identity
# string is immutable( cannot be changed once allocated)
# hence occupy same memory location
print (' ' is ' ')
# using is to check object identity
# dictionary is mutable( can be changed once allocated)
# hence occupy different memory location
print ({} is {})
Output:
s is part of geeksforgeeks
g e e k s f o r g e e k s
True
False
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
# 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
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read