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
if 's' in 'geeksforgeeks' :
print ( "s is part of geeksforgeeks" )
else : print ( "s is not part of geeksforgeeks" )
for i in 'geeksforgeeks' :
print (i,end = " " )
print ( "\r" )
print ( ' ' is ' ' )
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
a = 10
def read():
print (a)
def mod1():
global a
a = 5
def mod2():
a = 15
read()
mod1()
read()
mod2()
read()
print ( "Value of a using nonlocal is : " ,end = "")
def outer():
a = 5
def inner():
nonlocal a
a = 10
inner()
print (a)
outer()
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 in Keyword
The in keyword in Python is a powerful operator used for membership testing and iteration. It helps determine whether an element exists within a given sequence, such as a list, tuple, string, set or dictionary. Example: [GFGTABS] Python s = "Geeks for geeks" if "for" in s: print(
3 min read
is keyword in Python
In programming, a keyword is a âreserved wordâ by the language that conveys special meaning to the interpreter. It may be a command or a parameter. Keywords cannot be used as a variable name in the program snippet. Python language also reserves some of the keywords that convey special meaning. In Py
2 min read
Python Raise Keyword
In this article, we will learn how the Python Raise keyword works with the help of examples and its advantages. Python Raise KeywordPython raise Keyword is used to raise exceptions or errors. The raise keyword raises an error and stops the control flow of the program. It is used to bring up the curr
3 min read
Set add() Method in Python
The set.add() method in Python adds a new element to a set while ensuring uniqueness. It prevents duplicates automatically and only allows immutable types like numbers, strings, or tuples. If the element already exists, the set remains unchanged, while mutable types like lists or dictionaries cannot
5 min read
Python Set Methods
A Set in Python is a collection of unique elements which are unordered and mutable. Python provides various functions to work with Set. In this article, we will see a list of all the functions provided by Python to deal with Sets. Adding and Removing elementsWe can add and remove elements form the s
2 min read
Keyword Module in Python
Python provides an in-built module keyword that allows you to know about the reserved keywords of python. The keyword module allows you the functionality to know about the reserved words or keywords of Python and to check whether the value of a variable is a reserved word or not. In case you are una
2 min read
Python | Set 4 (Dictionary, Keywords in Python)
In the previous two articles (Set 2 and Set 3), we discussed the basics of python. In this article, we will learn more about python and feel the power of python. Dictionary in Python In python, the dictionary is similar to hash or maps in other languages. It consists of key-value pairs. The value ca
5 min read
Iterate over a set in Python
The goal is to iterate over a set in Python. Since sets are unordered, the order of elements may vary each time you iterate. You can use a for loop to access and process each element, but the sequence may change with each execution. Let's explore different ways to iterate over a set. Using for loopW
2 min read
Sets in Python
A Set in Python is used to store a collection of items with the following properties. No duplicate elements. If try to insert the same item again, it overwrites previous one.An unordered collection. When we access all items, they are accessed without any specific order and we cannot access items usi
9 min read
Python False Keyword
False is a boolean value in Python that represents something untrue or a "no" condition. It is one of the two Boolean constants (True and False) and is mostly used in conditions, loops and logical operations. In Python, False is treated as 0 in mathematical operations and as a falsy value in conditi
2 min read