Python Tutorial 8 --- "If Else & Elif Conditionals In Python"
Python Tutorial 8 ---
"If Else & Elif Conditionals In Python"
This PROGRAM or TUTORIAL about Python's "If Else & Elif Conditionals In Python"
DOC
“If, else and elif
statement can be defined as a multiway decision taken by our program due to the
certain conditions in our code.”
For
few readers, the term “elif” is new as they are not familiar with the word and
it is also not like most of the other words such as list or loops, etc. that
have the same meaning in the English language and in Python programming. In
fact, in English “elif” means honest. But if you have ever done programming in
any language, you must be familiar with “else-if” statement, well “elif” is
just that.
Now coming towards a more
formal sort of description. “If and else” are known as decision-making
statements for our program. They are very similar to the decision making we
apply in our everyday life that depends on certain conditions. The everyday
example is thoroughly explained in the tutorial so I will not waste your time
on that, instead, I would now like to focus on more technical details.
Let
us focus a little on the working:
Our compiler will execute the if statement to check whether it is true or false
now if it’s true the compiler will execute the code in the “if” section of the
program and skip the bunch of code written in “elif” and “else”. But if the
“if” condition is false then the compiler will move towards the elif section
and keep on running the code until it finds a true statement(there could be
multiple elif statements). If this does not happen then it will execute the
code written in the “else” part of the program.
An “if” statement is a must because without an if we cannot apply “else” or
“else-if” statement. On the other hand else or else if statement are not
necessary because if we have to check between only two conditions we use only
“if and else” and even though if we require code to run only when the statement
returns true and do nothing if it returns false then an else statement is not
required at all.
Now
Let’s talk about some technical issues related to the working of decision
statements:
- There is no limit to the number
of conditions that we could use in our program. We can apply as many elif
statements as we want but we can only use one “else” and one “if”
statement.
- We can use nested if statements
i.e. if statement within an if statement. It is quite helpful in many
cases.
- Decision statements can be
written using logical conditions which are:
- Equal to
- Not equal to
- Less than
- Greater than
- Greater than equal to
- Less than equal to
We can also use
Boolean or our custom-made conditions too.
CODE
#If Else & Elif Conditionals In Python
"""print("Type your number")
var1 = 20
var2 = 50
var3 = int(input())
if var3>var2: #We must put a colon and there must be a tab space before print statemaent
print("Greater")
#The elif statement allows you to check multiple expressions for
#TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.
#Similar to the else, the elif statement is optional.
elif var3==var2: #We must use double 'equal to'; single 'equal to' is assignment operator
print("Equal")
else:
print("Lesser")"""
list1 = [2, 4, 6]
print(5 in list1, 4 in list1) #output=False True
if 4 in list1:
print("Yes it's in the list")
if 5 not in list1:
print("No it isn't in the list")
Comments
Post a Comment