0% found this document useful (0 votes)
19 views27 pages

Unit 2

Uploaded by

kaifali532
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views27 pages

Unit 2

Uploaded by

kaifali532
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

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
zero and non- non-
TRUE, null values as
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:
statemen
Ex:
t(s)
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
statement(s) part”)
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.
(If-else)
3. Write a program to find biggest number among three numbers.(Nested if-else)
Loop
• In programming, the loops are the constructs that
repeatedly execute a piece of code based on the conditions.
These are useful in many situations like going through
every element of a list, doing an operation on a range of
values, etc.
• There are two types of loops in Python and these are for
and while loops. Both of them work by following the below
steps:
 Check the condition
 If True, execute the body of the block under it. And
update the iterator/ the value on which the condition is
checked.
 If False, come out of the loop
• The loops include:
 The for loop
 The while loop
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.
For loop
Syntax:

for item in sequence:


execute expression

our_list = ['Lily', 'Brad', 'Fatima', 'Zining']


for x in our_list:
print(x)

for letter in 'Lily':


print(letter)
Example
Print each fruit in a fruit list:
fruits = ["apple", "kiwi", "cherry"]
for x in fruits:
print(x)
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)
Python While Loops
• While loops execute a set of lines of code iteratively till a condition is satisfied. Once
the condition results in False, it stops execution, and the part of the program after the
loop starts executing.

• Syntax:
while condition:
statement(s)

Example:

i=1
while (i<=5):
print(i)
i=i+1
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)
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)
Differentiation b/w for and while loop
Basis of Comparison For Loop While Loop
Keyword Uses for keyword Uses while keyword
Used For loop is used when the While loop is used when the
number of iterations is already number of iterations is already
known. Unknown.
absence of condition The loop runs infinite times in Returns the compile time error in
the absence of condition the absence of condition

Nature of Initialization Once done, it cannot be repeated In the while loop, it can be
repeated at every iteration.
Functions To iterate, the range or xrange There is no such function in the
function is used. while loop.
Speed The for loop is faster than the While loop is relatively slower as
while loop. compared to for loop.
• Write a python program to find the most frequent number
in a list.
or
• Write a python program to find the most frequent words
in a text file.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy