In this article, you will learn about Python keywords. We have added a complete list of Python keywords with detailed examples. So that you can easily understand What is a keyword in python, How many keywords are in Python with short programs and syntax.
Python consists of a pre-defined set of words that are reserved for specific purposes. These keywords cannot be used as a variable name, function name, or any other identifier.
List of Python keywords
The keywords can be seen using a code in python, which is given below –
import keyword print("The keywords in Python are:") print(keyword.kwlist)
Output
The keywords in Python are
[‘False’, ‘None’, ‘True’, ‘and’, ‘as’, ‘assert’, ‘async’, ‘await’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]
Commonly used keywords in python
Now, we will learn about some of the commonly used keywords in python among these in detail –
True: This keyword is used to represent whether a given condition is true. Such keywords are called Boolean.
False: This is used to print a Boolean false. This keyword prints false when a statement is false.
None: This is used to denote a null/void value. It is an object of datatype – None
Example –
print (False == 0)
print (False == 1)
print (None == 0)
print (None == [])
Output
True False False False
And keywords in Python
And: This is a logical operator and returns the value true if both the conditions are true, or returns false if even one of the conditions is not true.
Truth Table for AND
A | B | AND |
True | True | True |
True | False | False |
False | True | False |
False | False | False |
or keywords in Python
Or: This is a logical operator and returns true if even one of the conditions is true, or returns false only if both the conditions are false.
A | B | OR |
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Not keywords in Python
Not: This logical operator inverts the truth value.
in keywords in Python
in: This keyword has two purposes. The first one is that it is used to check whether a container contains a value. And, the second purpose is that it is used to iterate through the container.
The above keywords are shown with the examples below –
print (True or False) print (False and True) print (not True) if 'y' in 'yuvayana': print ("y is present in yuvayana") else: print ("y is not present in yuvayana") for i in 'yuvayana’: print (i, end=" ")
Output –
True False False y is present in yuvayana y u v a y a n a
for keywords in Python
For: This keyword is used to control flow and for looping
While: Just like ‘for’ this is also used to control looping
break keywords in Python
break: used to control the looping. After the break keywords, the control comes out of the loop and passes to the next line written after the loop
continue keywords in Python
Continue: this is used to control looping. When this keyword runs, the current iteration is skipped and the next iteration is run. But the loop does not end.
Example
For i in range (8) : Print (i, end = “ “) If i == 4 Break Print () While i<10 If i == 6; i+=1 continue else print (i,end = “ ”) i+=1
Output –
0 1 2 3 4 0 1 2 3 4 5 6 7 8 9
if keywords in Python
if: This keyword is used for decision-making. The control goes in when “if” statement block runs.
else keywords in Python
else: This keyword is used for decision-making. When ‘if’ condition proves to be false, then the control goes inside ‘else’.
elif keywords in Python
elif: This is short for ‘else if’.
i = 50 if (i == 25): print ("i is 25") elif (i == 50): print ("i is 50") else: print ("i is not present")
Output –
i is 50
def keyword in Python
This is a keyword used to define user-defined functions
def func(): print(“This is def keyword”) func()
Output –
This is def keyword
return keywords in Python
return: This keyword is used to return from a function.
yield keywords in Python
yield: This keyword is used to return a generator from a function.
def func(): x = 0 for a in range(10): x += a yield x for a in func(): print(a)
Output –
45 0 1 3 6 10 15 21 28 36 45
Class Keywords in Python
This is one of the most commonly used and useful keywords in Python. This is used to create user-defined classes. Example –
class Person: name = "Harshit" age = 20 def fun(self): print("I'm", self.name) print("I'm", self.age) John = Person() print(John.attr1) John.fun()
Output –
Harshit I’m Harshit I’m 20
with keywords in Python
This keyword is used for resource management and exception handling. But nowadays this keyword is not used much.
Example –
With open(‘file_path’ , w) as file: file.write(‘hello world !’)
As Keywords in Python
This keyword is used to create an alias in Python.
Example –
import calendar as cal print(cal.month_name[2])
Output –
February
pass keywords in Python
This keyword is used as a placeholder. It is a null statement, and nothing is performed when it is run.
n = 10 for i in range(n): pass
lambda keywords in Python
Lambda is a small anonymous function, used to make inline returning functions
Example:
a = lambda x: x*x*x print(a(7))
Output – 343
Import, From keywords in Python
Import: This keyword is used to import any module in our program.
Example –
import math print(math.factorial(5)) from math import factorial print(factorial(5))
Output –
120 120
Exception Handling Keywords in Python
a = 4 b = 0 try: k = a//b print(k) except ZeroDivisionError: print("Can't divide by zero") finally: print('This is always executed') print ("The value of a / b is : ") assert b != 0, "Divide by 0 error" print (a / b)
output keywords in Python
Can't divide by zero This is always executed The value of a / b is : AssertionError: Divide by 0 error
del keywords in Python
This keyword is used to delete reference to an object
my_variable1 = 100 my_variable2 = "YuvayanaTech" print(my_variable1) print(my_variable2) del my_variable1 del my_variable2 print(my_variable1) print(my_variable2)
Output
100 YuvayanaTech NameError: name 'my_variable1' is not defined
Global and Local Keywords in Python
Global: This keyword is used to define a variable of global scope. This means it can be accessed globally even from outside the function.
Non-Local: This keyword is similar to global but this is used to work with variables inside nested functions
a = 15 b = 10 def add(): c = a + b print(c) add() def fun(): var1 = 10 def gun(): nonlocal var1 var1 = var1 + 10 print(var1) gun() fun()
Output
25 20
So, this was the article about some of the commonly used keywords and their functioning. Python provides a wide range of useful keywords which makes programming in it easier for programmers.
Attempt Free Python MCQ Test