Python Keywords with Examples

Here, we have discussed about the Python Keywords. Keywords in python are referred as reserved and predefined words with specific meaning and....

Hey guys! Here is another interesting article on Python Programming Language. 

Python keywords with Examples

In this article, we will going to discuss about the Python Keywords.

So, let's start!

Python Keywords

Keywords in Python are referred as reserved and predefined words with specific meaning and purpose. In python, to define the syntax of coding, keywords are used. These keywords can't be used as identifier names, function names or any variable name. In python programming language, all keywords, except True and False, are written in lowercase. In Python, there are total number of 33 keywords. And the list of these keywords are as follows;

No. Keywords Discription
1. and It is a logical operator. If both the operands are true then it returns true or else it returns false.
2. or It is also a logical operator. If any operand is true then it returns true or else it returns false.
3. with To simplify the exception handling, with keyword is used.
4. pass It is a null statement, it will do nothing.
5. del It is used to delete reference to the object.
6. global Global keyword is used to declare a global variable.
7. class This is used to define a class.
8. not This is another logical operator. If the operand is false then it returns true or else it return false.
9. True This keyword is a Boolean value.
10. False This keyword is also a Boolean value.
11. from To import specific parts of any module, from keyword is used.
12. in To check if a value is present in tuple, list, etc or not, in keyword is used.
13. raise This keyword is used to raise an exception.
14. finally The finally keyword is used with exceptions.
15. nonlocal The nonlocal keyword is used to declare non-local variable.
16. try Try keyword is used to make a try-except statement.
17. for The for keyword is used for loop.
18. if To make a conditional statement, if keyword is used.
19. break Break keyword is used to terminate the loop.
20. while To create a while loop, while keyword is used.
21. def This keyword is used to define function.
22. return Return keyword is used to return the value and exit a function.
23. continue This keyword is used to continue to the next iteration of a loop.
24. import To import a module, import keyword is used.
25. is To check whether the two variables are equal or not, this keyword is used.
26. yield To end a function and return a generator, yield keyword is used.
27. assert In order to check the correctness of code, assert keyword is used.
28. lambda In order to define an anonymous function, lambda keyword is used.
29. except This keyword is also used for exceptions.
30. as To create an alternative, as keyword is used.
31. elif Elif keyword is used with if keyword. It is a conditional statement.
32. else Else keyword is used with if and elif keyword. It is also a conditional statement.
33. none None is considered as a special constant which is used to denote a null value.

Python keywords Example

We can also find these keywords through the code given below;

import keyword

print("List of Python keywords is : "

print(keyword.kwlist)

Output

List of Python keywords is : 


['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']

Now let's discuss each python keywords with the help of example.

Python keywords example of for, in, if, elif and else

# 'in' python keyword example

for x in range(1, 7):

    if x == 1:

        print('Python')

# 'elif'python keyword example

    elif x == 2:

        print('Answersjet')

# 'else' python keyword example

    else:

        print('Welcome to Python keywords Program')

Output 

Python

Answersjet

Welcome to Python keywords Program

Welcome to Python keywords Program

Welcome to Python keywords Program

Welcome to Python keywords Program

Python keywords example of and, or, not, is and in

# 'or' python keyword example

print(True or False) 


# and' python keyword example

print(False and True) 


# 'not' python keyword example

print(not True) 


# 'in' python keyword example

if 'Python' in 'programming language': 

print("python is not part of programming language"

else: 

    print("python is part of Programming language") 

for x in 'Variable': 

    print(x, end=" "

print("\r") 


# 'is' python keyword example

print(' ' == ' '

print({} is {}) 

Output

True

False

False

python is part of Programming language

V a r i a b l e

True

False

Python keywords example of def

# 'def' python keyword example

def fun(): 

    print("Answersjet"

      

fun()

Output

Answersjet

Python keywords example of del

x= "answersjet" 

y= "Python"

del

print(y)

print (x)

Output 

Python

NameError: name 'x' is not defined

Python keywords example of try, except and finally

x=12

y=2

# 'try' Python keyword example

try:

z = y/x

print(z)

# 'except' Python keyword example

except Exception as o:

print(o)

# 'finally' Python keyword example

finally:

print('Welcome to Answersjet')

Output 

0.16666666666666666 Welcome to Answersjet

Python keyword example of Global

def my_func():  

    global a   

    a = 'Hey' 

    b = 'Folks'

    c = a+b  

    print(c)  

      

my_func()  

Output 

HeyFolks

Python keyword example of nonlocal

def outer_function():

a = 'Python'

def inner_function():

nonlocal a

a = 'Python'

print("Best Programming language: ",a)

inner_function()

print("Best Programming language: ",a)


outer_function()

Output 

Best Programming language: Python Best Programming language: Python

Python keyword example of return

def func_return(): x = 'Answersjet' return x def no_return(): x = 'Answersjet' print(func_return()) print(no_return())

Output 

Answersjet None

Python keyword example of as

import calendar as cal

print(cal.month_name[9])

Output 

September

Python keyword example of lambda

x = lambda y: y**4

for z in range(1,5):

print(x(z))

Output 

1

16

81

256

Python keyword example of yield

def generator():

for x in range(8):

yield x*x


y = generator()

for x in y:

print(x)

Output 

0

1

4

9

16

25

36

49

Python keyword example of import

import math

print(math.sqrt(78))

Output 

8.831760866327848

Python keyword example of while

x = 1

while x < 9:

print(x)

x = x + 1

Output 

1

2

3

4

5

6

7

8

Python keyword example of class

class Person:

name = "Deepak"

age = 22

print(Person.name)

print(Person.age)

Output 

Deepak

22

Python keyword example of break

i = 2

while i < 18:

print(i)

if i == 4:

break

i += 2

Output 

2 4

Python keyword example of None

x = None

print(x)

Output 

None

Conclusion

Above we have discussed about the Python Keywords. Keywords in python are referred as reserved and predefined words with specific meaning and purpose. In python, to define the syntax of coding, keywords are used. In Python, there are total number of 33 keywords.