0% found this document useful (0 votes)
28 views48 pages

Class 11 - Presentation # 4 - Flow of Control

The document provides an overview of Python programming concepts, focusing on control flow statements including empty, simple, and compound statements, as well as sequence, selection, and iteration constructs. It explains the use of conditional statements such as 'if', 'if...else', and 'if...elif...else', along with practical examples and programming exercises. Additionally, it covers indentation rules and the importance of structured code in Python.
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)
28 views48 pages

Class 11 - Presentation # 4 - Flow of Control

The document provides an overview of Python programming concepts, focusing on control flow statements including empty, simple, and compound statements, as well as sequence, selection, and iteration constructs. It explains the use of conditional statements such as 'if', 'if...else', and 'if...elif...else', along with practical examples and programming exercises. Additionally, it covers indentation rules and the importance of structured code in Python.
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/ 48

Computational Thinking

© CS-DEPT DPS MATHURA ROAD


and Programming - 1
Python - Flow of Control

XI
Empty Statement
The simplest statement is the empty statement i.e. a statement which does nothing.

© CS-DEPT DPS MATHURA ROAD


In Python, the empty statement is the pass statement.

It is written as:

pass

Whenever the interpreter encounters a pass statement, Python does nothing and
simply moves to the next statement in the flow of control. The pass statement is a
do nothing statement i.e. empty statement or a null operation statement.

2
XI
Simple Statement
Any single executable statement is a simple statement in Python. Simple statement

© CS-DEPT DPS MATHURA ROAD


are single line instructions, like the ones we have seen earlier.

A simple example is

name=input(“Enter your name”)

3
XI
Compound Statement
A compound statement is a group of statements executed as one unit. The compound

© CS-DEPT DPS MATHURA ROAD


statements are written in a specific pattern as shown below:

<compound statement header>:

<indented body having multiple simple

and/or compound statements>

A compound statement has a


● A header line which begins with a keyword and ends with a colon.
● A body consisting of one or more statements each indented inside the header line. All
statements in the body are at the same level of indentation.

4
XI
Compound Statement
In Python, indentation is used to declare a block. If two statements are at the same

© CS-DEPT DPS MATHURA ROAD


indentation level, then they are a part of the same block. The group of statements is
also called a suite.

Generally four white spaces are used for indentation and is preferred over tabs.

Indentation is the most used part of the python language since it declares the block
of code.

All the statements of one block are intended at the same level indentation.

5
XI
Statement Flow Control
In a program, statements may be executed sequentially, selectively or iteratively.

© CS-DEPT DPS MATHURA ROAD


Every programming language provides constructs to do it.

sequence selection iteration

6
XI
Sequence
The sequence construct means the

© CS-DEPT DPS MATHURA ROAD


statements are executed sequentially i.e.
one after the other. This is the default flow
of statements.

Every Python program begins with the first


statement of the program. Each statement
in turn is executed. When the last
statement is executed, the program is
done. Sequence refers to the normal flow of
control in a program.

7
XI
Selection
The selection construct means the

© CS-DEPT DPS MATHURA ROAD


execution of a set of statement(s)
depending upon a condition. If the
condition evaluated to True, a course of
action (a set of statements) is followed
otherwise another course of action (a
different set of statements) is followed.
This construct (selection construct) is also
called the decision construct since it helps
in making a decision about which set of
statements is to be executed.

8
XI
Selection
Python if statement supports selection.

© CS-DEPT DPS MATHURA ROAD


● A simple example of such a case is to display the result of a student
depending upon his/her marks. The result is either “Pass” or “Fail”
depending upon the condition whether the student has scored passing
marks or not.

● To input the marks of a student, and if the marks are above 75, the
message should display that the child has performed ‘well’ otherwise it
should display that ‘An improvement is needed’.

9
XI
Iteration
The iteration construct means repetition of a set of

© CS-DEPT DPS MATHURA ROAD


statements depending upon a condition. As long as the
condition is true, a set of statements are executed
repeatedly. As soon as the condition becomes false, the
repetition stops. It is also known as looping. The set of
statements that are executed again and again are called
the body of the loop. The condition on which the execution
or exit of the loop depends is called the exit condition or
test condition.

Python for and while statements support iteration.

10
XI
Conditional Statement using if
The if statements are the conditional statements in Python that implement

© CS-DEPT DPS MATHURA ROAD


selection constructs. If statement checks for a condition, if the condition
evaluates to true, a course of action is followed otherwise that course of action
is ignored but another course of action is followed.

The simplest form of if statement tests a condition and if the condition


evaluates to true, it carries out some instructions but does nothing if the
condition evaluates to false.

if <test condition> : where statement may be a single


Statement/ statement, a compound statement
[statements] or just a pass statement.

11
XI
Conditional Statement using if
For example:

© CS-DEPT DPS MATHURA ROAD


ch=input("enter a character ")
if ch>='0' and ch<='9' :
print("you have entered a digit")

Code to find whether character inputted is digit

12
XI
Conditional Statement using if
For example:

© CS-DEPT DPS MATHURA ROAD


age=int(input("enter your age"))
if age>18:
print ("You are eligible to vote")

Code to find whether you are eligible to vote or not

13
XI
Conditional Statement using if... else
if else statement : This form of statement checks for the test condition. If the

© CS-DEPT DPS MATHURA ROAD


condition evaluates to True then the statements indented below if are executed
otherwise the statements indented below else are executed.

if <Condition1>: if Marks >= 75:

<Statement1> print("Good")
else:
else:
<Statement2>
print("Can do better")

14
XI
Conditional Statement using if... else
For example:

© CS-DEPT DPS MATHURA ROAD


age=int(input("enter your age"))
if age>18:
print ("You are eligible to vote")
else:
print ("You are not eligible to vote")

Code to find whether you are eligible to vote or not

15
XI
Conditional Statement using if..elif..else

© CS-DEPT DPS MATHURA ROAD


if <Condition1>: if Marks > 90:

<Statement1> print("Good")

elif <Condition2>: elif Marks >= 60:

<Statement2> print("Can do better")

... else:
else: print("You need to work hard")
<StatementN>
16
XI
Question 1
Write a Python code to accept Age and Name from the user, and display a message

© CS-DEPT DPS MATHURA ROAD


as Eligible to VOTE and Can get License to Drive if the Age is greater or equal to 18,
else it should display the number of years to wait for the same:

Sample Input for Sample Input


Message
Age for Name

Age: 25 Mohit Mohit you are Eligible to VOTE


Can get License to Drive

Age: 11 Surmai Surmai Wait for 7 years to Vote


Wait for Driving License
17
XI
Question 2
Write a program to input the day number and print week day.

© CS-DEPT DPS MATHURA ROAD


Day Number Week Day

1 Monday

2 Tuesday

3 Wednesday

4 Thursday

5 Friday

6 Saturday

7 Sunday
18
XI
Question 2
Write a program to input the day number and print week day.

© CS-DEPT DPS MATHURA ROAD


Day=int(input("Enter the day number:"))
Day Number Week Day
if Day == 1:
print("Monday")
1 Monday elif Day == 2:
print("Tuesday")
2 Tuesday elif Day == 3:
print("Wednesday")
3 Wednesday elif Day == 4:
print("Thursday")
4 Thursday elif Day == 5:
print("Friday")
5 Friday elif Day == 6:
print("Saturday")
6 Saturday elif Day == 7:
print("Sunday")
7 Sunday
19
XI
Question 3
Write a program to enter whether a number is positive, negative or zero.

© CS-DEPT DPS MATHURA ROAD


Number = int(input("Enter a number :")) Enter a number :6
if Number > 0: 6 is positive
print(Number, " is positive")
elif Number < 0: Another run
print(Number, " is negative")
else: Enter a number : -9
print(Number, " is zero") -9 is negative

20
XI
Question 4
Write a program to check whether an alphabet is vowel or consonant.

© CS-DEPT DPS MATHURA ROAD


ch = input("Enter a character :") Enter a character: s
if ch=="a" or ch=="e" or ch=="i" or ch=="o" Character is a
consonant
or ch=="u":
print("Character is a vowel")
else:
print("Character is a consonant")

21
XI
Question 5
Write a program to check whether a character is uppercase or lowercase alphabet.

© CS-DEPT DPS MATHURA ROAD


ch = input("Enter a character :"): Enter a character : m
if ch>="A" and ch<="Z": Character is lowercase
alphabet
print(Character is uppercase alphabet")
elif ch>="a" and ch<="z":
print("Character is lowercase alphabet")
else:
print(“not a character”)

22
XI
Question 6
Write a program to input two numbers and determine Enter the first number:6
Enter the second number:2

© CS-DEPT DPS MATHURA ROAD


whether one is a multiple of the other or not. 6 is a multiple of 2

Another run
num1=int(input("Enter the first number:")) Enter the first number:9
num2=int(input("Enter the second number:")) Enter the second number:18
if num1%num2==0: 18 is a multiple of 9
print(num1, " is a multiple of ",num2)
elif num2 % num1 == 0: Another run
print(num2, " is a multiple of ",num1) Enter the first number:7
Enter the second number:88
else:
7 and 88 are not multiples
print(num1," and ", num2," are not multiples")

23
XI
Programs
1. Write a program to Input a number and display if the number entered is even or

© CS-DEPT DPS MATHURA ROAD


odd.
2. Write a menu driven program to calculate the area of a circle or perimeter of a
circle. Depending upon the choice input by the user, display the same after
taking the radius of the circle as input from the user.
3. Write a program to check whether a triangle is valid or not if angles are given
using if else.
4. Write a program to input the coefficients of a quadratic equation and find all
roots. In case of imaginary roots, just display that the roots are imaginary.

24
XI
Program
5. Write a program to input marks of five subjects Physics, Chemistry, Biology,

© CS-DEPT DPS MATHURA ROAD


Mathematics and Computer, calculate percentage and grade according to given
conditions:
PERCENTAGE GRADE

>= 90 A

>= 80 B

>=70 C

>=60 D

>=40 E

< 40 F
25
XI
6. Write a program to input electricity unit charge and calculate the total electricity

© CS-DEPT DPS MATHURA ROAD


bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill.

26
XI
7.Write a program to input basic salary of an employee and calculate gross salary

© CS-DEPT DPS MATHURA ROAD


(Gross Salary = Basic Salary + HRA + DA) as per the following:

BASIC SALARY HRA DA


< = 10000 20% 80%

BETWEEN 10001 TO 20000 25 % 90 %

>= 20001 30% 95%

Display appropriate messages.


27
XI
Question 1
Write a program to Input a number and display if the Enter the number:6
6 is even

© CS-DEPT DPS MATHURA ROAD


number entered is even or odd.
num1=int(input("Enter the number:")) Another run
if num1%2==0:
Enter the number:13
print(num1, " is even")
13 is odd
else:
print(num1, " is odd")

28
XI
Question 2
Write a menu driven program to calculate the area of a 1. Calculate Area of Circle
2. Calculate Perimeter of Circle

© CS-DEPT DPS MATHURA ROAD


circle or perimeter of a circle. Depending upon the choice Enter your choice:1
input by the user, display the same after taking the radius Enter the radius:4.5
Area :63.64285714285714
of the circle as input from the user.

print("1. Calculate Area of Circle")


print("2. Calculate Perimeter of Circle")
choice=int(input("Enter your choice:"))
if choice==1:
r=eval(input("Enter the radius:"))
ar=22/7*r*r
print("Area :",ar)
else:
r=eval(input("Enter the radius:"))
per=2*22/7*r
print("Perimeter :",per) 29
XI
Question 3
Write a program to check whether a triangle is valid Enter first angle:70

© CS-DEPT DPS MATHURA ROAD


or not if angles are given using if else. Enter second angle:70
Enter third angle:40
Valid Triangle
a=int(input("Enter first angle:"))
b=int(input("Enter second angle:"))
c=int(input("Enter third angle:"))
if a+b+c==180:
print("Valid Triangle")
else:
print("InValid Triangle")

30
XI
Question 4 - Program to calculate grade
Math module imported to use sqrt() and fabs()

© CS-DEPT DPS MATHURA ROAD


functions
import math Enter a: 2
a = int(input('Enter a: ')) Enter b: 2
b = int(input('Enter b: ')) Enter c: 2
Roots are
c = int(input('Enter c: ')) -1.3660254037844386
d = ((b**2) - (4*a*c)) 0.3660254037844386
sol1 = (-b-math.sqrt(math.fabs(d)))/(2*a) Roots are imaginary
sol2 = (-b+math.sqrt(math.fabs(d)))/(2*a)
print("Roots are ",sol1,sol2)
if d<=0:
print("Roots are imaginary")
else:
print("Roots are real")
31
XI
Question 5 - Program to calculate grade
m1 = int(input("Enter marks of subject1:")) Enter marks of subject1:77
m2 = int(input("Enter marks of subject2:")) Enter marks of subject2:88

© CS-DEPT DPS MATHURA ROAD


m3 = int(input("Enter marks of subject3:")) Enter marks of subject3:77
m4 = int(input("Enter marks of subject4:"))
Enter marks of subject4:88
m5 = int(input("Enter marks of subject5:"))
Enter marks of subject5:99
total =m1+m2+m3+m4+m5
per=total/500*100
Percentage : 85.8
if per>90: Grade B
print("Grade A")
elif per>80:
print("Grade B")
elif per>70:
print("Grade C")
elif per>60:
print("Grade D")
elif per>40:
print("Grade E")
else:
print("Grade F")
32
XI
Question 6 - Electricity Bill
units=float(input("Enter the units consumed=")) Enter the units
if(units<=50 and units>=0): consumed=400

© CS-DEPT DPS MATHURA ROAD


bill=units*0.50 Amount to be paid : 534.0
elif(units<=150):
bill=50*0.50+(units-50)*0.75
elif(units<=250)
bill=50*0.50+100*0.75+(units-150)*1.20
else:
bill=50*0.50+100*0.75+100*1.20+(units-250)*1.50
surcharge=bill*0.20
amt=bill+surcharge
print("Amount to be paid :",amt)

33
XI
Question 7
basic=float(input("Enter the basic salary:")) Enter the basic
salary:12000

© CS-DEPT DPS MATHURA ROAD


if(basic<10000):
Gross Salary calculated:
hra=0.8*basic
25800.0
da=0.2*basic
elif(basic>10000 and basic<=20000):
hra=0.9*basic
da=0.25*basic
else:
hra=0.95*basic
da=0.3*basic
gross=basic+da+hra
print("Gross Salary calculated:",gross)

34
XI
Nested if

© CS-DEPT DPS MATHURA ROAD


if <Condition1>: if <Condition1>:
if <Condition2>: <Statement(s)>
<Statement(s)> elif <Condition2>:
if <Condition3>:
else:
<Statement(s)>
<Statement(s)> else:
elif <Condition3>: <Statement(s)>
<Statement(s)> else:
else: <Statement(s)>
<Statement(s)>

35
XI
Nested if
age = int(input("Enter your age : "))

© CS-DEPT DPS MATHURA ROAD


if age < 18:
print("You are minor")
print("You are not eligible to work")
else:
if age >= 18 and age <=60:
print("You are eligible to work")
print("Please fill in your details and apply!")
else:
print("You are too old to work as per the Government rules")
print("Collect your pension")

Enter your age : 40 Enter your age : 67


You are eligible to work You are too old to work as per the
Please fill in your details and apply! Government rules
Collect your pension
36
XI
Indentation
Say, there are different suites named as A,B,C,D etc with indents at various

© CS-DEPT DPS MATHURA ROAD


levels. The concept of indentation can be easily understood by the following:

Statements /Suite A:
Statements/Suite B
Statements/Suite C
Statements/Suite D
Statements/Suite E
Statements/Suite F
Statements/Suite G

37
XI
Indentation

© CS-DEPT DPS MATHURA ROAD


Let us understand the following script :

if a<b:
print(“A is smaller than B”) Both statements are at
print(“B is greater than A”) same indent level

The above code will print both statements if the condition “a<b” is true.

38
XI
Indentation
● One statement is linked with if

© CS-DEPT DPS MATHURA ROAD


if 5>40:
print(“5 is not smaller than 40”)
print(“Done”) Done

● Two statements linked with if

if 5>40:
print(“5 is not smaller than 40”) No output will
print(“Done”) be shown
39
XI
Indentation
● Invalid indentation is a syntax error

© CS-DEPT DPS MATHURA ROAD


print("5 is not smaller than 40")
print(“Done”)

● If you have only one statement to execute, you can put it on the same line
as the if statement.

if a > b: print("a is greater than b")

40
XI
Programs
1. Write a Menu driven program to calculate the surface area and volume of a

© CS-DEPT DPS MATHURA ROAD


cube, cuboid or sphere depending upon the user’s choice.
2. Write a program to input a number. If it is an even number print its square
otherwise print its cube.
3. Write a program to input principal amount and time. If the time is more than
10 years, print the simple interest with a rate of 8% p.a. Otherwise print it
with 12 % p.a.
4. Write a program to check if a year is leap year or not. If a year is divisible by 4
then it is leap year but if the year is century year like 2000, 1900, 2100 then it
must be divisible by 400.

41
XI
Find syntax error(s), if any, in the following:
1. 2.

© CS-DEPT DPS MATHURA ROAD


42
XI
Solution

© CS-DEPT DPS MATHURA ROAD


1. age=12 2. a,b,c=12,13,14 or a=b=c=12
if age<=10: if (a>=b or a<=c and a+c<=b):
print(“Primary”) print(“A”) or print(a)
elif age<=13: else :
print(“middle”) print(a+b+c)
elif age<=15:
print(“secondary”)
elif age<=17:

43
XI
Find the output:
var=100

© CS-DEPT DPS MATHURA ROAD


if var<200:
print(“Expression value is less than 200”)
if var==150:
print(“Which is 150”)
elif var==100:
print(“Which is 100”)
elif var==50:
print(“Which is 50”)
elif var<50:
print(“Expression value is less than 50”)
else:
print(“Could not find true expression”)
print(“Good Bye”)
44
XI
Solution:

© CS-DEPT DPS MATHURA ROAD


Expression is less than 200
Which is 100
Good bye

45
XI
Find output of the following:
What will the program print if the

© CS-DEPT DPS MATHURA ROAD


user provides the following as input :

(a) 3 (a) wow 3


(b) 21 (b) Whoa 21
(c) 6
(c) 5
(d) 27
(d) 17 (e) Wow -5
(e) -5

46
XI
Solution
(a) Wow 3

© CS-DEPT DPS MATHURA ROAD


(b) Whoa 21
(c) 6
(d) 27
(e) Wow -5

47
XI
Find output of the following:
x=0 x=0

© CS-DEPT DPS MATHURA ROAD


1. 2.
a=0 a=1
b = -5 b = -5
if a > 0: if a > 0:
if b < 0: if b < 0:
x=x+5 x=x+5
elif b > 5: elif b > 5:
x=x+4 x=x+4
else: else:
x=x+3 OUTPUT: x=x+3 OUTPUT:
else: 2 else: 5
x=x+2 x=x+2
print(x) print(x)

48
XI

You might also like