Newby Coder header banner

Python Keywords


Python has a set of keywords that are reserved words that cannot be used as variable names, function names, or any other identifiers:

Method Description Example
and A logical operator
>>>x = 2 > 0
>>>x == True
True
as To create an alias
>>> from pprint import pprint as pp
>>> pp("pretty print")
'pretty print'
assert For debugging
>>> assert(10<5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
break To break out of a loop
>>> i=0
>>> j = 0
>>> for x in range(3,20):
...     j = x
...     if x % i == 2:
...         break
...
>>> j
12
class To define a class
>>> class Circle:
...     pi = 3.14
...
>>> print(Circle.pi)
3.14
continue To continue to the next iteration of a loop
>>> i=[]
>>> for x in range(1, 20):
...     if x % 2 == 0:
...         continue
...     i.append(x)
...
>>> i
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
def To define a function
>>> def even(n):
...     return n%2 == 0
...
>>> even(11)
False
del To delete an object or attribute
>>> x = {'y': 15, 'z':31}
>>> del x['z']
>>> x
{'y': 15}
>>> del x
elif Used in conditional statements, same as else if
>>> def test(n):
...     if n>0:
...         return '+ve'
...     elif n<0:
...         return '-ve'
...     else:
...         return 'zero'
...
>>> test(0)
'zero'
>>> test(-10)
'-ve'
else Used in conditional statements
>>> def test(n):
...     if n>0:
...         return '+ve'
...     elif n<0:
...         return '-ve'
...     else:
...         return 'zero'
...
>>> test(0)
'zero'
except Used with exceptions, what to do when an exception occurs
>>> def divide(x, y):
...     try:
...         print(x/y)
...     except:
...         print("Cannot divide")
...
>>> divide(20, 10)
2.0
>>> divide(20, 0)
Cannot divide
False Boolean value, result of comparison operations
finally Used with exceptions, after try-except blocks, a block of code to be executed no matter if there is an exception or not
>>> def divide(x, y):
...     try:
...         print(x/y)
...     except:
...         print("Cannot divide")
...     finally:
...         print("Unaffected by exception")
...
>>> divide(20, 10)
2.0
Unaffected by exception
>>> divide(20, 0)
Cannot divide
Unaffected by exception
for To create a for loop
>>> for i in range(0,3):
...     print(i * i)
...
0
1
4
from To import specific parts of a module
>>> from math import pi
>>> pi
3.141592653589793
global To declare a global variable
>>> i = 12
>>> def x():
...     i = 15
...
>>> def y():
...     global i
...     i = i + 2
...
>>> x()
>>> i
12
>>> y()
>>> i
14
if To make a conditional statement
>>> def test(n):
...     if n>0:
...         return '+ve'
...     elif n<0:
...         return '-ve'
...     else:
...         return 'zero'
...
>>> test(10)
'+ve'
import To import a module
>>> import math
>>> math.pi
3.141592653589793
in To check if a value is present in a list, tuple, etc
>>> 2 in range(1, 10)
True
>>> 2 in [3,4,5]
False
is To test if two variables are equal
>>> x = 5; y = 5
>>> x is y
True
lambda To create an anonymous function
>>> sq=lambda x:  x * x
>>> sq(11)
121
None Represents a null value
>>> x = None
>>> type(x)
<class 'NoneType'>
nonlocal To declare a non-local variable
>>> x = 5; y = 8
>>> def change():
...     x = 14
...     y = x - 3
...     def change2():
...         nonlocal y
...         y = y * y
...     change2()
...     print(y)
...
>>> change()
121
not A logical operator
>>>True and False
False
or A logical operator
>>> 6 is not 5
True
pass A null statement, a statement that does nothing, often used as placeholder
>>> def empty_method():
...     pass
... 
raise To raise an exception
>>> raise RuntimeError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError
return To exit a function and return a value
>>> def zero():
...     return 0
...
>>> zero()
0
True Boolean value, result of comparison operations
try To make a try...except statement
>>> def divide(x, y):
...     try:
...         print(x/y)
...     except:
...         print("Cannot divide")
...
>>> divide(20, 10)
2.0
>>> divide(20, 0)
Cannot divide
while To create a while loop
>>> i = 0
>>> while i <5:
...     print(i * i)
...     i+=1
...
0
1
4
9
16
with Used to simplify exception handling, for example when a file is opened by with keyword, then Python handles closing of the file after block is executed and file object goes out of scope
>>> >>> with open('/tmp/testfile', 'wb+') as f:
...     read_data = f.read()
... 
>>> f.closed
True
yield To end a function, returns a generator
>>> def yielder():
...     for i in range(0, 5):
...         yield i
...
>>> print(yielder())
<generator object yielder at 0x7f21275eed00>
>>> for x in yielder():
...     print(x)
...
0
1
2
3
4
>>> z = list(yielder())
>>> z
[0, 1, 2, 3, 4]