KEMBAR78
Unit 2 | PDF | Control Flow | Boolean Data Type
0% found this document useful (0 votes)
22 views23 pages

Unit 2

Conditional statements in Python allow for decision making and code execution based on conditions. There are several types of conditional statements including if statements which execute code if a condition is true, if-else statements which allow alternative code execution if a condition is false, and nested conditional statements which allow conditional logic within other conditional blocks.

Uploaded by

Sushmita Sharma
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)
22 views23 pages

Unit 2

Conditional statements in Python allow for decision making and code execution based on conditions. There are several types of conditional statements including if statements which execute code if a condition is true, if-else statements which allow alternative code execution if a condition is false, and nested conditional statements which allow conditional logic within other conditional blocks.

Uploaded by

Sushmita Sharma
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
You are on page 1/ 23

CONDITIONAL STATEMENTS

Decision making is anticipation of conditions occurring while execution


of the program and specifying actions taken according to the conditions.
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
if it is either zero or null, then
it is assumed as FALSE value.
CONDITIONAL STATEMENTS
Python programming language provides following
types of decision making statements:
•If statements:
•An if statement consists of a boolean expression
followed by one or more statements.
•If-else statements:
•An if statement can be followed by an optional else
statement, which executes when the boolean expression
is FALSE.
•Nested if else statements:
•You can use one if or else if statement inside
another if or else if statement(s).
CONDITIONAL STATEMENTS
If statements:
Syntax:
if (expression):
statement(s)

Ex:
val=5
if (val==5):
print("I am five")

OUTPUT:
CONDITIONAL STATEMENTS
If statements:
EX: Program to check the given number is Negative

num=float(input("Enter the number:: "))

if num<0:

print("The number {0} is Negative: ".format(num))

OUTPUT:
CONDITIONAL STATEMENTS
If-else statements:
Syntax:
if (expression):
statement(s)
else:
Ex: statement(s)
val=6
if (val==5):
print("I am five")
else:
print(“I am not five”)

OUTPUT:
CONDITIONAL STATEMENTS
If-else statements:
EX: Program to check the given number is Positive or Negative

num=float(input("Enter the number:: "))

if num<0:
print("The number {} is Negative: ".format(num))

else:
print("The number {} is Positive: ".format(num))

OUTPUT:
CONDITIONAL STATEMENTS
If-elif-else statements:
Syntax: if (expression):
statement(s)
elif (exp):
statement(s)
else:
statement(s)
Ex:
val=10
if val>10:
print("Value is greater than 10")
elif (val<10):
print("value is less than 10")
else:
print("Value is equal to 10")
OUTPUT:
CONDITIONAL STATEMENTS
If-elif-else statements:
EX: Program to check the given Year is Leap or Not
year=int(input("Enter the year:: "))

if year%400==0 and year%100==0:


print("The year {0} is Leap year. ".format(year))

elif year%4==0 and year%100!=0:


print("The year {0} is Leap year. ".format(year))

else:
print("The year {0} is NOT Leap year. ".format(year))

OUTPUT:
CONDITIONAL STATEMENTS
Nested If-else statements:
Syntax: var = 100
if expression1: if var < 200:
statement(s) print (“Value is less than 200”)

if expression2: if var == 150:


statement(s) print (“Which is 150” )

elif expression3: elif var == 100:


print (“Which is 100”)
statement(s)

else: else :
statement(s) print (“This is inner else”)

else:
else: print (“This is out else part”)
statement(s)
CONDITIONAL STATEMENTS
Nested If-else statements:
EX: Program to check the largest number of given three numbers
num1=float(input("Enter the First number:: "))
num2=float(input("Enter the Second number:: "))
num3=float(input("Enter the Third number:: "))
if num1>num2:
if (num1>num3):
print("The number {} is the largest number.".format(num1))
else:
print("The number {} is the largest number.".format(num3))
elif (num2>num3):
print("The number {} is the largest number.".format(num2))
else:
print("The number {} is the largest number.".format(num3))
Example:
1. Write a program to find small number of two numbers (if)
2. Write a Program to input age and check whether the person is eligible to vote or not.
3. Write a program to find biggest number among three numbers.
Python For Loops
Python For Loops
• A for loop is used for iterating over a sequence
(that is either a list, a tuple, a dictionary, a set, or a
string).
• This is less like the for keyword in other
programming languages, and works more like an
iterator method as found in other object-orientated
programming languages.
• With the for loop we can execute a set of
statements, once for each item in a list, tuple, set
etc.
Example
Print each fruit in a fruit list:
fruits = ["apple", "kiwi", "cherry"]
for x in fruits:
print(x)
Python While Loops
Looping Through a String
Even strings are iterable objects, they contain a sequence of
characters:
Example
Loop through the letters in the word "banana":
for x in "banana":
print(x)
The pass Statement
• if statements cannot be empty, but if you for some reason have
an if statement with no content, put in the pass statement to
avoid getting an error.
• Example

a = 33
b = 200

if b > a:
pass
The break Statement
• With the break statement we can stop the loop even if the
while condition is true:
• Example
Exit the loop when i is 3:
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
The break Statement
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
The continue Statement
• With the continue statement we can stop the current iteration,
and continue with the next:
• Example
Continue to the next iteration if i is 3:
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
The continue Statement
fruits = ["apple", "banana", "cherry"]
The continue Statement
for x in fruits:
if x == "banana":
continue
print(x)
The range() Function
Example
Using the range() function:
for x in range(6):
print(x)
for x in range(2, 6):
print(x)
Nested Loops

• A nested loop is a loop inside a loop.


• The "inner loop" will be executed one time for each iteration
of the "outer loop":
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]

for x in adj:
for y in fruits:
print(x, y)

You might also like