Python Tutorial > Python Basics > Python Keywords (Part 2): How Many Types of Reserved Keywords?
Posted in

Python Keywords (Part 2): How Many Types of Reserved Keywords?

Python Keywords (Part 2)
How many types of reserved keywords are there in Python?

Previous Tutorial: Python Keywords (Part 1)

Python Keywords

Welcome and continue this Python keywords tutorial. In the previous tutorial, we learned about keyword definitions and how many keywords there are in Python. We also learned about the difference between keywords and built-in functions. In this tutorial, we are going to learn about different kinds of reserved keywords in detail.

How Many Reserved Keywords in Python?

The number of keywords changed according to the Python version. In the recent Python version, it has 35 reserved keywords. According to their work, Python keywords are divided into different categories.

Different Categories With Keywords:
Value Keywords: True, False, None
Operator Keywords: and, or, not, in, is
Control Flow Keywords: if, elif, else
Iteration Keywords: for, while, break, continue
Structure Keywords: def, class, pass, lambda
Context Keywords: with, as
Returning Keywords: return, yield
Import Keywords: import, from
Exception Handling Keywords: try, except, finally, assert, raise
Asynchronous Programming Keywords: async, await
Variable Handling Keywords: del, global, nonlocal
All 35 keywords are divided into 11 categories.

Now let’s learn about each category with brief details.

Value Keywords in Python

True, False, None; these are the three keywords that are known as value keywords in Python.

True Keyword

True keyword represent a Boolean value and the result of a comparison operator. It always represents an expression that will result in TrueTrue keyword is the same as one (1). For example:

CODE:
x = 10
y = 11
print(x < y)
OUTPUT:
True

Because our expression was right (x < y). So we get True in the output.

False Keyword

False keyword represent a Boolean value and the result of a comparison operator. It represents an expression that will result in not True The False keyword is the same as zero (0).

CODE:
x = 12
y = 11
print(x < y)
OUTPUT:
False

Because our expression was not true (x < y), we get False in results.

None Keyword

None keyword represents no valueNone keyword is used to define a null value or no value at all. If a function does not return anything, it returns None in Python.

In other programming languages, None is represented as null, nil, none, undef, or undefined. Always remember, in Python, there is no null; if somebody is referring to null in Python, then it means they’re referring to None.

NOTE: Always remember that None is not zero, False, or an empty string. None is a data type of its own, and only None can be None.

CODE:
x = None
print(type(x))
OUTPUT:
<class 'NoneType'>

Also, we use None when we want to assign an empty value. For example:

CODE:
x = 10
y = 20
z =
print(x +y)
OUTPUT:
    z =
       ^
SyntaxError: invalid syntax

Here we created three variables. We didn’t assign any value to variable z; we want to do it later. But we cannot leave a variable empty like this. That’s why we get an error. So in such a case, we assign None for the empty value.

CODE:
x = 10
y = 20
z = None
print(x +y)
OUTPUT:
30

Now we get the output without any error.

So these are the three keywords (True, False, None) considered as value keywords.

Operator Keywords in Python

and, or, not, in, is; these are the five keywords that are known as operator keywords in Python.

and Keyword

and keyword is a logical operator. It is used to combine two or more conditional expressions in Python. After that, we can evaluate that condition, and the output comes in True and False. If both conditions are correct, then the result will be True and if one of the conditions is not correct, then the result will be False. For example:

CODE:
a = 10
b = 9
print(a > b and a == 10)
OUTPUT:
True

Here we provide two conditions (a > b and a == 10), we use and keyword between both conditions, and because both conditions are correct, we get True in the output.

or keyword

or keyword is also a logical operator. It is used to combine two or more conditional expressions in Python. After that, we can evaluate that condition, and the output comes in True and False. If one of the conditions is correct, then the result will be True and if both conditions are not correct, then the result will be False. For example:

CODE:
a = 10
b = 9
print(a < b or a == 10)
OUTPUT:
True

Here we provide two conditions (a < b and a == 10), we use or keyword between both conditions, and because one of the conditions (a ==10) is correct, we get True in the output.

not Keyword

not is also a logical operator; it reverts the result. If statements are correct, then the output should be True but not operator reverses the output and we will get False. And vice versa if the statements are not correct. For example:

CODE:
a = 10
b = 9
print(a > b)
print(not a > b)
OUTPUT:
True
False

We can see, when we use not keyword (not a > b), the output is reversed. We get False instead of True.

in Keyword

in keyword is used to check if a value is present in a sequence like a list, tuple, string, etc. It is also used to iterate values with loops.

CODE:
first_list = ['Mango', 'Banana', 'Apple']
print('Mango' in first_list)
OUTPUT:
True

Because our value ('Mango') was present in the list, so we get True in the output. We can use in keyword with loops. For example:

CODE:
first_list = ['Mango', 'Banana', 'Apple']
for values in first_list:
    print(values)
OUTPUT:
Mango
Banana
Apple

Here we use in keyword with for loop, and it will iterate all the values of the list. We can also use in keyword with conditional statements. For example:

CODE:
first_list = ['Mango', 'Banana', 'Apple']
if 'Mango' in first_list:
    print('Yes, the value is present.')
else:
    print('No, the value is not present.')
OUTPUT:
Yes, the value is present.

So we can use in keyword with different conditions.

is Keyword

Leave a Reply

Your email address will not be published. Required fields are marked *