Professional Documents
Culture Documents
07conditional Statement
07conditional Statement
CONDITIONAL STATEMENT
Condition gives the result as a bool type, either True Syntax for if else statement:
or False. If the condition result is True, then if block
if (condition):
statements will be executed. If the condition result is
False, then next line of code execute. if block statement
Here is a problem, What if a 90 years old man II Pause the next operators for a
applied for the job, will you accept him? Hell No while
Therefore we need to add additional condition to Ex 7.5 ( if else statement with compound condition)
specify required age range.
a = int(input("Enter your age\n"))
► Resume the next operators for a if a > 17 and a < 45:
while
print("Pass for interview.")
What if we want to combine two relational else:
operators. How? Let us see…
print("Application Denied.")
In such scenarios, we use Logical Operators
Enter your age
What are Logical Operators?
90
Logical Operators are operators which do some
logical operation on the operands and return True or Application Denied.
False are called logical operators. EXERCISE 7.3
In python, there are three types of logical operators. Create two variable and "nationality" and "cgpa"
They are and, or, not. These operators are used to and store values is your name and nationality, then
construct compound conditions, combinations of create scholarship conditional statement based on
more than one simple condition. Each simple nationality and cgpa?
condition gives a boolean value which is evaluated,
Hint: Output looks like below
to return final boolean value.
Enter your nationality
Note: In logical operators, False indicates 0(zero)
and True indicates non-zero value. Logical operators Indian
on boolean types Enter your cgpa
and : If both the arguments are True then 8.7
only the result is True Congratulations, you won.
or : If at least one argument is True then the
result is True
not : complement of the boolean value What if you have more additional condition like (
Applicant should have GPA above than 9 else he/she
Ex 7.4 ( Logical operators on boolean types ) will be in waiting list ) ?
a,b = True,False PS. Quality is determined by accuracy and
completeness.
print(a and b) #and
print(a or b) #or LESSON - FOUR
LESSON - FIVE
print("Accepted.") if (condition):
else: if block statement
print("Waiting List...") elif:
else: elif block statement
print("Application Denied.") else:
else block statement
next line of code
CONDITIONAL STATEMENTS 29
LESSON - SIX
Create a variable "score" and take input from user, a=float(input("Enter 1st No:\n"))
then classify them based on their score? b=float(input("Enter 2nd No:\n"))
Classification:
if a > b:
"A" – 90 to 100
print(a,"is the largest No.")
"B" – 80 to 90
elif a = b:
"C" – 70 to 80
print(a,"and",b,"are equal.")
"D" – 60 to 70
else:
"F" – below 60
print(b,"is the largest No.")
CONDITIONAL STATEMENTS 30
Before Swap a=1 b=2 There are six bitwise operators, those are
a = 1 text = "Welcome"
b = 2 print("Well" in text)
print("Before Swap a=",a,"b=",b) print("wel" in text)
a = a ^ b print("Wel" in text)
b = a ^ b print("come" not in text)
a = a ^ b False
print("After Swap a=",a,"b=",b) False
SUMMARY OF BITWISE (for binary numbers) What if you want to check a person whether
he/she is belong to class 10A or not?
Ex 7.21 ( Membership Operators )
class_10A = ["John","Eden","Bob"]
print("john" in class_10A)
print("Lucas" not in class_10A)
False
True
What if you want to search a keyword in a text?
What if a user just want to evaluate some
LESSON - EIGHT calculation without using variables?
MEMBERSHIP OPERATORS LESSON - NINE
What is Membership Operators?
eval() FUNCTION
Membership operators are operators which are used
This is an in-built function available in python,
to check whether an element is present in a sequence
which takes the strings as an input. The strings which
of elements or not. Here, the sequence means strings,
we pass to it should, generally, be expressions. The
list, tuple, set and dictionaries.
eval() function takes the expression in the form of
There are two membership operators. i.e. string and evaluates it and returns the result.
❶ in operator: The in operators returns True if Ex 7.22 ( eval() function /Arithmetic Operator/ )
element is found in the collection of sequences. eval(’10 + 10′)
returns False if not found.
20
❷ not in operator: The not in operator returns True
if the element is not found in the collection of EXERCISE 7.6
sequence. returns False in found.
Create two integer variables "a" and "b" take input
from a user, then calculate their average by using
eval() function?
CONDITIONAL STATEMENTS 33
What if we want to take input from user? Ex 7.31 ( eval() function /math() function/ )
Ex 7.23 ( eval() function /Take inputs from a user/ ) from math import *
20 - 12 + 3 * 4 / 3 EXERCISE 7.7
Calculate
12.0
■ ln(e)
Ex 7.24 ( eval() function /Compound Operator/ )
■ cos(90°)
a = 1
■ 10 the power of 5
eval('a += 2')
■ Square root of 625
SyntaxError: invalid syntax ■ Ceil and Floor of 6.5
Ex 7.25 ( eval() function /Unary Minus Operator/ ) Using eval() function?
a = 5
eval('-a ')
-5
Ex 7.26 ( eval() function /Relational Operator/ )
False
Ex 7.27 ( eval() function /Logical Operator/ )
0
Ex 7.28 ( eval() function /Bitwise Operator/ )
16
Ex 7.29 ( eval() function /Membership Operator/ )
s = "John"
eval('"Jo" in s')
True
Ex 7.30 ( eval() function /Identity Operator/ )
eval('1 is 2')
False