5 Python Keywords
Keywords in python are reserved words that can not be used as a variable name, function name or any other identifier. There are 33 keywords present in python. Kewords are basic buiding blocks for python code. Let’s know them one by one in short…
and
or
not
Above three keywords are used to perform logical and, or and not operations respectively.
as:
It is used to create an alias for an object, while importing a package. ex. import pandas as pd
assert:
Assert statements are a convenient way to insert debugging assertions into a program. Basically it compares the variable with the specified value. It checks if condition returns True. If the condition is true, nothing will happen. If the condition is false, it raises AssertionError
💻 Example:
1 2 3 4 5 | a = 'hello'
assert a == 'hello'
# nothing will happen as variable a is with value 'hello'
# lets try with condition as False:
assert a == 'bye'
|
✅ Output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
continue
break
pass
These three keywords are loop control statements. continue is used to end current iteration in a for loop and continues to the next iteration. break is used to take control or execution flow out of the loop. pass does nothing, means it is used to do nothing and pass the execution further in code. sometimes we need to just pass the control in code without doing anything.
class
def
Keyword class is used to define a python class. Class is nothing but blueprint of objects. def is used to define a python function. Let’s define a class and function with name as ‘School’ which does nothing.
💻 Example:
1 2 3 4 5 6 7 | # class definition
class School:
pass
# function definition
def School:
pass
|
if
elif
else
Above three keywords are used to construct if loop conditional statements.
for
while
Above two keywords are used to construct for and while loop conditional statements respectively.
try
except
finally
Above three keywords are used to catch exceptions in code to implement exception handling.
True
False
Above two keywords represents boolean value.
del
del is used to delete the variable or object defined in code.
💻 Example:
1 2 3 4 5 | a = [1,2,3,4]
print(a)
[1, 2, 3, 4]
del a
print(a)
|
✅ Output:
[1, 2, 3, 4]
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print(a)
NameError: name 'a' is not defined
yield
It is used in python generator functions. Python generators yield instead of returning the value. Don’t worry if you dont know python generators at this point. We will study ‘Python Generators’ concept in Intermediate section.
from
It is widely used to import the method or function from another python module or file.
💻 Example:
1 2 | # importing add_nums function from myfile.py file
from myfile import add_nums
|
global
local
Both keywords define the scope of variable. Keyword global is used to make the variable global and available to all methods and functions. The variables defined outside functions are global by default. Variables defined inside function are local by default and accessible to that particular function only.
with
It is used as context manager in python. The resources like database objects, file objects can be easily managed using with statement. The resource opened with with statement are closed automatically at the end of with statement. Don’t worry! we’ll see ‘Context Managers’ in detail in ‘Python Intermediate’ section in detail.
return
This keyword used to return particular value from a method or function.
raise
It is used to manually throw or raise an exception in python.
💻 Example 1:
1 | raise SyntaxError('This method must be defined before that method')
|
✅ Output:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
raise SyntaxError('This method must be defined before that method')
File "<string>", line None
SyntaxError: This method must be defined before that method
💻 Example 2:
1 | raise ValueError('Values can not be accepted beyond 1000')
|
✅ Output:
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
raise ValueError('Values can not be accepted beyond 1000')
ValueError: Values can not be accepted beyond 1000
nonlocal
It modifies the scope of variable from local to nonlocal. Used in nested functions to make the local variable defined inside the inner function nonlocal to outer function.
lambda
This keyword is an anonymous funtion. It is widely used with higher order functions (filter, map, reduce).
is
in
The keyword is is identity operator and in is memborship operator. Both the keywords are explained in python operators section.
None
This is the keyword which represents nothing. Somtimes we get variable values as None, that means the variable contains nothing. Always remeber None is not equal to 0 or blank string (‘’).
These are the python keywords (special words) used in python to do specified tasks. We are going to use each and every keyword during upcoming sessions while coding!
Here are the quick notes on above topic:
Follow below video to clear the concepts practically.
Be prepared with below questions prior to your interview !
Frequently asked Interview Questions
What are python keywords?
Can we use keyword names as variable names?
What is the use of ‘del’ keyword?
What are the keywords used for controlling the loops?
What keywords are used for implementing conditional statements?
What are boolean keywords in python?