0% found this document useful (0 votes)
31 views9 pages

CH 3 Unit 1 Conditional Statement, Looping and Control Statement

Conditional statements notes of pyhton

Uploaded by

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

CH 3 Unit 1 Conditional Statement, Looping and Control Statement

Conditional statements notes of pyhton

Uploaded by

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

Ch 3 Unit 1 Conditional Statement, looping and Control statement

Conditional Statement
S.No Statement & Description
.

if statements

1
An if statement consists of a boolean expression followed by one or
more statements.

if...else statements

2 An if statement can be followed by an optional else statement, which


executes when the boolean expression is FALSE.

nested if statements

3 You can use one if or else if statement inside another if or else


ifstatement(s).

var=100

if(var==100):print("Value of expression is 100")

print("Good bye!")

Syntax
The syntax of the if...else statement is −

if expression:

statement(s)

else:

statement(s)
Example
#!/usr/bin/python3

amount=int(input("Enter amount: "))

if amount<1000:

discount= amount*0.05

print("Discount",discount)

else:

discount= amount*0.10

print("Discount",discount)

print("Net payable:",amount-discount)

Output
In the above example, discount is calculated on the input amount. Rate of
discount is 5%, if the amount is less than 1000, and 10% if it is above
10000. When the above code is executed, it produces the following result −
Enter amount: 600
Discount 30.0
Net payable: 570.0

Enter amount: 1200


Discount 120.0
Net payable: 1080.0

The 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.

Similar to the else, the elif statement is optional. However, unlike else, for
which there can be at the most one statement, there can be an arbitrary
number of elif statements following an if.
syntax
if expression1:

statement(s)

elif expression2:

statement(s)

elif expression3:

statement(s)

else:

statement(s)

Core Python does not provide switch or case statements as in other


languages, but we can use if..elif...statements to simulate switch case as
follows −

Example
#!/usr/bin/python3

amount=int(input("Enter amount: "))

if amount<1000:

discount= amount*0.05

print("Discount",discount)

elif amount<5000:

discount= amount*0.10

print("Discount",discount)

else:

discount= amount*0.15

print("Discount",discount)

print("Net payable:",amount-discount)

When the above code is executed, it produces the following result −


Enter amount: 600
Discount 30.0
Net payable: 570.0

Enter amount: 3000


Discount 300.0
Net payable: 2700.0

Enter amount: 6000


Discount 900.0
Net payable: 5100.0

while Loop Statements


A while loop statement in Python programming language repeatedly
executes a target statement as long as a given condition is true.

Syntax
The syntax of a while loop in Python programming language is −

while expression:

statement(s)

count=0

while(count <9):

print('The count is:', count)

count= count +1

print("Good bye!")

Output
When the above code is executed, it produces the following result −
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
The Infinite Loop
var=1

while var==1:# This constructs an infinite loop

num=int(input("Enter a number :"))

print("You entered: ",num)

print("Good bye!")

Using else Statement with Loops


count=0

while count <5:

print(count," is less than 5")

count= count +1

else:

print(count," is not less than 5")

Output
When the above code is executed, it produces the following result −
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5

for Loop Statements


The for statement in Python has the ability to iterate over the items of any
sequence, such as a list or a string.

Syntax
For iterating_var in sequence:

statements(s)

The range() function


The built-in function range() is the right function to iterate over a sequence
of numbers. It generates an iterator of arithmetic progressions.

Example
>>list(range(5))

[0,1,2,3,4]

Example
range() generates an iterator to progress integers starting with 0 upto n-1.
To obtain a list object of the sequence, it is typecasted to list(). Now this list
can be iterated using the for statement.

>>>for var in list(range(5)):

print(var)

Output
This will produce the following output.
0
1
2
3
4

for letter in'Python':# traversal of a string sequence

print('Current Letter :', letter)

print()

fruits=['banana','apple','mango']

for fruit in fruits:# traversal of List sequence

print('Current fruit :', fruit)

print("Good bye!")

Output
When the above code is executed, it produces the following result −
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n

Current fruit : banana


Current fruit : apple
Current fruit : mango
Good bye!

Using else Statement with Loops


numbers =[11,33,55,39,55,75,37,21,23,41,13]

for num in numbers:

if num%2==0:

print('the list contains an even number')

break

else:

print('the list doesnot contain even number')

Output
When the above code is executed, it produces the following result −
the list does not contain even number

Nested loops
Syntax
For iterating_var in sequence:

For iterating_var in sequence:

statements(s)

statements(s)

The syntax for a nested while loop statement in Python programming


language is as follows −

while expression:

while expression:

statement(s)
statement(s)

Example
The following program uses a nested-for loop to display multiplication tables
from 1-10.

#!/usr/bin/python3

import sys

for i in range(1,11):

for j in range(1,11):

k=i*j

print(k,end=' ')

print()

The print() function inner loop has end=' ' which appends a space instead
of default newline. Hence, the numbers will appear in one row.

Last print() will be executed at the end of inner for loop.

Output
When the above code is executed, it produces the following result −
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

Control Statements (continue, break and pass) in


Python
1) Continue Statement
It returns the control to the beginning of the loop.
# Prints all letters except 'e' and 's'
For letter in 'geeksforgeeks':
If letter =='e' or letter =='s':
continue
print ('Current Letter :', letter)

Run on IDE
Output:

Current Letter : g

Current Letter : k

Current Letter : f

Current Letter : o

Current Letter : r

Current Letter : g

Current Letter : k

2)Break Statement
It brings control out of the loop
for letter in 'geeksforgeeks':
if letter == 'e' or letter == 's':
break
print ('Current Letter :', letter)
Run on IDE
Output:

Current Letter : g

3)Pass Statement
We use pass statement to write empty loops. Pass is also used for empty control
statement, function and classes.
# An empty loop
for letter in 'geeksforgeeks':
pass
print ('Last Letter :', letter)
Run on IDE
Output:

Last Letter : s

You might also like