Conditional keywords in Python- if, else, elif
- if : It is a control statement for decision making. Truth expression forces control to go in “if” statement block.
- else : It is a control statement for decision making. False expression forces control to go in “else” statement block.
- elif : It is a control statement for decision making. It is short for “else if“
if, else, and elif keyword use in Python
The code checks the value of the variable i
:
- If ‘
i'
is 10, it prints “i is 10”. - If ‘
i
'
is 20, it prints “i is 20”. - If ‘
i'
is neither 10 nor 20, it prints “i is not present.” In this case, it will print “i is 20” because the value ofi
is 20.
Python3
i = 20 if (i = = 10 ): print ( "i is 10" ) elif (i = = 20 ): print ( "i is 20" ) else : print ( "i is not present" ) |
i is 20
Note: For more information, refer to out Python if else Tutorial.
Python Keywords
Every language contains words and a set of rules that would make a sentence meaningful. Similarly, in Python programming language, there are a set of predefined words, called Keywords which along with Identifiers will form meaningful sentences when used together. Python keywords cannot be used as the names of variables, functions, and classes.
In this article, we will learn about Python keywords and how to use them to perform some tasks.