Python keywords | How many Keywords Python language have?

Keywords are special or we can say reserved words of any particular language. These words have special meaning and functionalities in the programming language in which they belong. In other words, we can say these are the words who are designed to perform the special task. These words can not be used to perform any other task other than they are designed to perform. Every programming language has its own keywords. Therefore, Python programming language has also some keywords which we called them Python keywords.

 

Python Keywords

Here we will see the various python keywords.

Now the question is How many Keywords are there in Python programming language? So, There is a total of 33 keywords in Python. which are:

and for raise
as from return
assert global TRUE
break if try
class import while
continue in with
def is yield
del lambda not
elif None or
else nonlocal pass
except FALSE finally

These are a total of 33 keywords in python. soon we will study each keyword of python with example.

and Keyword in Python

Python and keyword is logical operator in Python. Logical operators are those operators which are used to combine conditional statements.

and keyword is used to check the condition. Let’s understand it by example

Example of Python and keyword: Suppose we have to check whether user can apply for the given job. Job condition is that user age should be greater than or equals to 18 and less than or equal to 30.

age= int (input ("enter age:"))

if age>=18 and age<=30:

print ("you are eligible to apply")

else:

print ("You are not eligible")

Output is

enter age.:19

you are eligible to apply

enter age:12

You are not eligible

enter age:32

You are not eligible

In this example, and keyword is used to check the age validation.

READ  Python factorial Program Example

 

as keyword in Python

Python as keyword is used to create an alias (alias means that particular name can be known as some other name). You might alias keyword in some database. Let’s understand as keyword in python with example

Example: our requirement is to print the current date time

We can do this two way

import datetime

x = datetime.datetime.now()

print(x)

output

2019-02-25 11:26:44.251457

or we can use a keyword as to create alias of datetime.

import datetime as dt

x = dt.datetime.now()

print(x)

output

2019-02-25 11:26:44.251457

 

assert keyword in Python

assert keyword is mainly used in debugging. assert is used to test the condition. let’s understand it by taking some example.

assert example in Python

test="Python"

assert test=="Python"

print("statements after assert keyword")

In above example we have one variable test having value  “Python”. With assert keyword we will check whether value is equal to “Python” or not. If it equal to “Python” it will do nothing and control will go to next line. So in this case, program output will be

Output

statements after assert keyword.

Now, suppose assert value is not equal to “Python”, then assert will throw an AssertionError error, and code after AssertionError will not execute.

test="Python"

assert test=="hello"

print("statements after assert keyword")

Output of this program

assert test=="hello"

AssertionError

Clearly, the lines of code will not execute after AssertionError. We can also make AssertionError some user friendly. We can show some message to tester or programmer, see example below

test="Python"

assert test=="hello","Values are not same"

print("statements after assert keyword")

Here we have written some message that “Values are not same”, so when above program will execute friendly message will come along with AssertionError.

READ  How to use Python while loop | while loop in Python

Output:

assert test=="hello","Values are not same"

AssertionError: Values are not same.

 

break Keyword in Python

break keyword in Python is used to stop or break the current loop. Let’s understand break keyword in python with example.

Example: Suppose there are 20 numbers, we want to print only 7 number, so when number reach to 7, it breaks the loop and control goes to next line of the code.

for i in range (20):

if i &amp;amp;amp;amp;gt; 7:

break

print(i)

print ("out of the loop")

Output will be

0

1

2

3

4

5

6

7

out of the loop

 

Note: Rest keywords of python will explain soon….

 

 

Please follow and like us:

2 Comments to “Python keywords | How many Keywords Python language have?”

  1. […] is one of the Python Keyword. def is used to create the Python user define function. When we have to create new function we will […]

  2. […] to create objects and methods. we will read about Python classes in other posts. In this post of Python Keywords, we will study about class […]

Leave a Reply