Click For Python Keywords Part 1
Types of Python Keywords
The number of keywords changed according to the Python version. In the recent version, Python has 35 reserved keywords. According to their work, Python keywords are divided into different categories.
Value Keywords In Python
True, False, None
are the value keywords in Python.
True Keyword
True keyword is a boolean value and result of a comparison operator. It always represents an expression that will result in True. True keyword is the same as one (1). For example:
Code:
x = 10
y = 11
print(x < y)
Output:
True
So because our expression was true, we get True
in result.
False Keyword
False keyword is a boolean value and result of a comparison operator. It represents an expression that will result in not being True. The False keyword is the same as zero (0).
Code:
x = 12
y = 11
print(x < y)
Output:
False
Because our expression was false, we get False
in results.
None Keyword
None keyword represents no value. None 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 it’s referring to None.
NOTE: Always remember that None is not zero, False, or an empty string. None is a data type of it’s own, and only None can be None.
Code:
x = None
print(type(x))
Output:
<class 'NoneType'>
So remember, None is a separated data type in Python.
So these are the value keywords in Python.
Operator Keywords in Python
and, or, not, in, is
are the 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 result comes in True and False. If both conditions are true, then the result will be True and if one of them condition is false, then the result will be False.
Code:
a = 10
b = 9
print(a > b and a == 10)
Output:
True
So here we checked two conditions; we combined both conditions with and
operator. And because both conditions are true, we get True in results.
or keyword
or 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 result comes in True and False. If one of them is true, then the result will be True and if both conditions are false, then the result will be False.
Code:
a = 10
b = 9
print(a < b or a == 10)
Output:
True
Here one of them condition is true so we get True in result.
not Keyword
not is a logical operator; it reverts the result. If statements are true, then the result will be False, and if statements are false, then the result will be True. For example:
Code:
a = True
print(not a)
Output:
False
So our result should be True but we use not operator
before variable a, so we get False in the result.
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 was present in the list, we get True in the result.
is Keyword
is keyword is an identity check operator. This is different from the equal operator; the equal operator checks equality. Sometimes two values or objects can be equal but not be the exact same. The is keyword determines whether two objects are exactly the same object. For example:
Code:
a = 10
b = 10
print(a is b)
Output:
True
We get True in result because a and b are the same object.
So these are the operator keywords in Python. In other programming languages, they don’t use simple words like and, or, not, in, is
. They used symbol like &&,! ||
, etc.
Control Flow Keywords in Python
if, elif, else
are the control flow keywords in Python.
if keyword
if keyword is used to create conditional statements and allows us to execute a block of code only if a condition is true. For example:
Code:
a = 1
if a < 5:
print('Yes, a is less than 5.')
Output:
Yes, a is less than 5.
So we used the if keyword with a condition and because our condition is true. So we get our print statement as a result.
elif Keyword
elif is the same as if keyword; it is also used to check the condition statement. elif is short for ‘else if‘; it means, if first if condition
is not true, then check the elif condition. We canot directly use elif; we always used elif after an if statement.
Code:
a = 1
if a < 0:
print('Yes, a is less than 5.')
elif a == 1:
print('Yes, a is equal to 1.')
Output:
Yes, a is equal to 1.
So now here our if condition is not true. So interpreter check the elif condition and condition is true so we get print statement of elif in result.
else Keyword
else keyword is used in conditional statement. It is used when the if or elif condition is not true, then we can run the else condition. For example:
Code:
a = 1
if a < 0:
print('Yes, a is less than 5.')
elif a == 11:
print('Yes, a is equal to 1.')
else:
print('Both if and elif is false.')
Output:
Both if and elif is false.
So here our both if and elif condition is false and in such a case, an else statement will run.
So these are the control flow keywords in Python. These are very common keywords, almost used in every programming.
Iteration Keywords in Python
for, while, break, continue
are the iteration keywords in Python.
for keyword
There are two types of loops in Python. for loop is one of them. We used for loop for iterating items from a sequence. For example:
Code:
first_list = ['Python', 100, 'Java']
for values in first_list:
print(values)
Output:
Python
100
Java
Here we used for loop with a list and it will iterate all the items of the list.
while Keyword
while is another type of loop in Python. It is also used for iteration. It iterates values until the condition becomes false. For example:
Code:
a = 1
while a < 5:
print(a)
a = a + 1
Output:
1
2
3
4
Here we used while loop; it will iterate the values until the condition becomes false.
break Keyword
If you need to exit a loop early, then you can use break keyword. break keyword, interrupt the loop, and stop iterating the next value. This keyword works in both for and while loops.
break with while loop:
Code:
a = 1
while a < 5:
print(a)
a = a + 1
if a == 3:
break
Output:
1
2
We used break with while loop and loop stopped after iterating only two values.
break with for loop:
Code:
first_list = ['Python', 100, 'Java']
for values in first_list:
print(values)
if values == 100:
break
Output:
Python
100
continue Keyword
continue keyword is used to end the current iteration in the loop and continue to the next iteration. continue keyword worked with both loops. With the use of continue, we can skip to the next loop iteration.
continue with while loop:
Code:
a = 1
while a < 5:
print(a)
a = a + 1
if a == 3:
continue
Output:
1
2
3
5
So loop skip the value 4 and continue with the next iteration.
continue with for loop:
Code:
first_list = ['Python', 100, 'Java', 200]
for values in first_list:
if values == 100:
continue
print(values)
Output:
Python
Java
200
We used continue with the second value, so we didn’t get 100 in output and after that iteration, continue.
End of Python keywords Part 2
2 thoughts on “Python Keywords (Part 2): Types And Uses Of Keywords In Python”