0% found this document useful (0 votes)
13 views3 pages

Lesson04-Decision Making (If-Statement)

Lesson 4 covers decision structures in Python, which evaluate expressions to produce TRUE or FALSE outcomes. It explains three main constructs: if statements, if...else statements, and nested if statements, providing syntax and examples for each. Additionally, it introduces the elif statement for checking multiple conditions and demonstrates single-line if statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Lesson04-Decision Making (If-Statement)

Lesson 4 covers decision structures in Python, which evaluate expressions to produce TRUE or FALSE outcomes. It explains three main constructs: if statements, if...else statements, and nested if statements, providing syntax and examples for each. Additionally, it introduces the elif statement for checking multiple conditions and demonstrates single-line if statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lesson 4: Decision Structures

Decision structures evaluate multiple expressions, which produce TRUE or


FALSE as the outcome. The following is the general form of a typical decision-
making structure found in most of the programming languages-

Python programming language assumes any non-zero and non-null values as


TRUE, and any zero or null values as FALSE value.

There are three constructs of decision-making statements in Python:

 if statements
 if...else statements,
 nested if statements

if statement

If the conditional expression in an if statement is True, then the block of


statement(s) inside the if statement is executed.

Example 1

age = 20
if age:
print ("Age is true")
print (age)
Example 2

if age < 18:


print ("This is a minor")
print (age)

A. Irungu
if... else statements

An else statement can be combined with an if statement. An else statement


contains a block of code that is executed when the conditional expression in
the if statement is False. The else statement is optional; and there can only be
at most one else statement following an if statement.

Syntax

if expression:
statement(s)
else:
statement(s)
Example

score=int (input ("Enter the score: "))


if score<50:
grade="Fail"
print ("Failed: Score = ", score)
else:
grade="Pass"
print ("Passed: Score = ", score)

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
score=int (input ("Enter the score: "))
if score<40 and score>=0:
grade="F"
elif score<50:
grade="D"
elif score<60:
grade="C"
elif (score<70):

A. Irungu
grade="B"
elif score<=100:
grade="A"
else:
print ("Invalid Score!")
print ("Score = " + str(score) +", Grade = " + grade)

Nested if statements

An if construct can be included as part of statements inside another if


construct.

Syntax

if expression1:
statement(s)
if expression2:
statement(s)
else
statement(s)
else:
statement(s)
Example
name = input ("Enter your name: ")
if len(name)>5:
print ("Your name has more than 5 characters")
if 'a' in name:
print ("Your name has the vowel a")
else:
print ("Your name does not have the vowel a")
else:
print ("Your name has less than 6 characters")
if 'i' in name:
print ("Your name has the vowel i")
else:
print ("Your name does not have the vowel i")
Single Statement
If the body of an if statement consists of only a single line statement, it can be
written in the same line as the header statement.

Example

if num%3==0:print ("Number is divisible by 3")

A. Irungu

You might also like