You are on page 1of 15

PYTHON

PROGRAMMING
Session 4
Control Structures-If statement
Control Structures
◦ Control structures are used for non-sequential and repetitive
execution of instruction.
◦ If conditional Statements
◦ Iteration for and while statements
General form of a typical decision making structure in
most of the programming languages

Decision structures evaluate multiple expressions


which produce TRUE or FALSE as outcome.

You need to determine which action to take and which


statements to execute if outcome is TRUE or FALSE
otherwise

Python programming language assumes any non-


zero and non-null values as TRUE, and if it is
either zero or null, then it is assumed as FALSE value.
If Statement
◦ The if statement contains a logical expression using which data is compared
and a decision is made based on the result of the comparison.
Syntax
if expression:
statement(s)

If the boolean expression evaluates to TRUE, then the block of statement(s)


inside the if statement is executed. If boolean expression evaluates to FALSE,
then the first set of code after the end of the if statement(s) is executed.
Example
var1 = 100
if var1 >20:
print(“in side if block“) Output

print(var1) in side if block


100
print(‘out of if structure’)
Out of if structure
----------------------------------
var2 = 0
if var2 > 100: Output
print (“I am in if block”) Good bye
c=110
print (var2,c)
print ("Good bye!“)
Example
Marks = eval(input("Enter marks obtained "))
if Marks >0 and Marks <=100:
print(' marks are valid ‘)

Password = eval(input("Enter your password"))


if Password == ‘magic$567’:
print(‘ login successful‘)
If else Statement
◦ An else statement can be combined with an if statement.
An else statement contains the block of code that executes if the
conditional expression in the if statement resolves to 0 or a FALSE
value.
◦ The else statement is an optional statement and there could be at most
only one else statement following if.
Syntax
if expression:
statement(s)
else:
statement(s)
Example
Password = eval(input("Enter your password"))
if Password == ‘magic$567’:
print(‘ login successful‘)
else:
print(‘ invalid password’)
Program to check if a number is positive
or negative
Num = eval(input(“ enter a number”))
if Num >=0:
print(‘number is positive’)
else:
print(‘number is negative’)
Question to try
◦ Write a program to check if a number is odd or even ?

◦ (hint use modulo operator)


elif statement
◦ 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.

◦ Syntax
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Example

if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Program to try
◦ Q Ask user to enter his choice
◦ If choice entered is 1 then print the value of choice and also print
‘Hello and welcome to CS’
◦ Else if the user has entered 2 then print ‘the choice entered is
two’ and print ‘It’s a nice day’
◦ Else if the user has entered 3 print ‘Goodbye’
◦ Else if the user entered 4 print ‘ Enjoy python’
◦ In case any other value print wrong choice
Question
◦ Q Ask user to input his marks
◦ If marks entered are >=75 print ‘First division’
◦ If marks entered are <75 but more 60 print ‘Second division’
◦ If marks entered are <60 but more or equal to 40 print third
division
◦ Otherwise print Fail

You might also like