Professional Documents
Culture Documents
But, in some situation we need to execute/skip some part of the code according to the
condition.
To create a good program, we must plan using program logic and development tools.
9.2 STATEMENTS AND ITS TYPES IN PYTHON
Statements are the instructions given to the computer to perform any kind of action.
Generally, In Python Statements are divided into three types. They are:
STATEMENTS
Example:
print(Age) Statement 2
A compound statements contains one or more other statements and controls their
execution. Which consists of a header followed by one or more indented line.
The body of compound statement cannot be empty; it must always contains at least
one statement.
Control statements are divided into three main categories. They are:
Control
Statement
s
In Python, there are Four types of selection/decision making statements are available
they are:
(i) if statement.
if condition is true , then it evaluates the true statements. Otherwise, it will execute
normal statements.
Note:
After executing true statements, the flow will continue with normal statements.
False
Syntax: if
Conditio
n
True
Statement(s)
EXAMPLES
1. Write a Python program to get a number from the user, check whether it is equal
Source code:
between 5 to 10.
Source code:
(or)
9.4.2 IF-ELSE-STATEMENT
If else statement is called Two way decision making statement. Which is used to test
the condition True or False.
if condition is true , then it evaluates the true statements. Otherwise, it will execute else
statements.
Note:
After executing if statements or else statements, the flow will continue with normal
statements.
True False
Statement(s) Statement(s)
Normal
statements
EXAMPLES USING IF-ELSE STATEMENTS
1. Write a python program to get age from the user and check whether he (or) she
Source code:
4. Write a Python program to take values of length and breadth of a rectangle and check
whether it is square or not.
APPLICATION BASED PROGRAM QUESTIONS
1. A Company decided to give bonus of 5% to their employees. If His/ Her year of
services are more than 5 years. Ask user for their salary and years of service and print
net bonus.
2. A student will not be allowed to sit in exam if his/her attendance is less than 75% .
Take the following input from the user: Number of class held, Number of classes
attended and print percentage of class attendance along with whether the student is
allowed to sit in exam hall or not.
9.4.3 IF-ELIF-ELSE STATEMENTS
If-elif statements are called multi way decision making statements.
Syntax:
FLOW CHART OF if elif statements
EXAMPLE - 1
Write a Program that accepts a number (say ‘n’) and we wish do the following:
(i) if n is 0 do nothing.
Source Code:
EXAMPLE - 2
Write a Python program to input week number(1-7) and print day of week name.
Source Code:
EXAMPLE - 3
Write a Menu driven program to get two numbers from the user and find addition,
subtraction, multiplication and division depends on the user’s choice.
EXAMPLE - 4
Write a Program that accepts a country name as per the following table and assign to
a variable, then finally display it’s capital.
Country Name Capital
India New Delhi
France Paris
UK London
USA Washington D.C
EXAMPLE - 5
Write a Python program to input electricity unit charge and calculate the total electricity 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.
PRACTICE QUESTION BASED ON if-elif-else
Q.No: 1
Mr. Rajesh provides the side of Measurements of a triangle. He asks the students to
check whether It is a Scalene triangle, Isosceles triangle or Equilateral triangle. Write a
suitable Python program to solve the above problem.
Note:
Write a Python program to check whether a given year is leap year or not.
Q.No: 3
Write a menu driven program to find the area of circle, rectangle and square. The menu is given
below:
1 – Area of Circle.
2- Area of Rectangle.
3- Area of Square
PRACTICE QUESTION BASED ON if-elif-else
Q.No: 3
Write a Python program to input marks of five subjects Physics, Chemistry, Math and CS,
If average is 80 – 89 : Grade B
If average is 40 – 59 : Grade E
WAY – 1: WAY – 2:
SYNTAX
WAY – 3: WAY – 4:
EXAMPLE - 1
Source Code:
EXAMPLE - 2
Write a Python Program to Find the Largest Among Three Numbers.
For example,
Source Code:
EXAMPLE - 3
Write a Python program to check whether a given year is leap year or not.
Source code:
HOME WORK
1. While purchasing certain items, a discount of 10% is offered if the quantity purchased is
more than 1000. If quantity and per item are input through the keyboard, write a Python
program to calculate the total expenses.
Looping is the process of repeating some set of statements again and again until
the condition becomes false or until it reaches the last element in sequence.
Types of Looping:
(iii) The Body of the loop. - The statements that are executed repeatedly form the
body of the loop.
(iv) Update expression(s) - The update expression change the value of a loop
variable.
SYNTAX:
Body of the
Loop.
At first the test expression is checked. If it is False the body of the loop will not
execute at all and control transfer to the next line of code after the while loop.
However, if the test expression is true, then the body of the loop is execute. After
executing the body of the loop, once again it will check the condition, if the
condition is true once again it will execute the body of the loop.
This process will repeat until the test expression becomes False.
EXAMPLES
Example -1:
Write a Python program to print "Welcome" five times using while loop.
Example -2:
Write a Python program to get five fruits name from the user using while loop.
Example -3:
Write a Python program to print numbers from 1 to 10 also finally print "Finished"
using while loop.
Example -4:
Write a Python program to print numbers from 1 to 10 in reverse order.
Example -5:
Write a Python program to print even numbers from 1 to 20 using while loop.
Example -6:
Write a Program to calculate and print the sums of even and odd integers of the first
‘n’ natural numbers.
Example -7:
Example -8:
I.e. (1+2+3+4+5+6+7+8+9+10=55)
2. Write a Python program to find and print square of numbers from 5 to 15.
3. Write a Python program to get four digit number from the user and print its reverse
form. (EX: I/P: Enter a Number: 1589 O/P: The reverse of given number is: 9851)
So, Whatever is in the loop gets executed forever unless the program is terminated.
Example:
i. range(stop)
Example:
Statement Values generated
>>>range(5) 0,1,2,3,4
>>>range(1,10) 1,2,3,4,5,6,7,8,9
>>>range(2,10,2) 2,4,6,8
>>>range(5,15,3) 5,8,11,14
>>>range(10,1,-2) 10,8,6,4,2
9.5.2 The for loop
In Python for loop is called "Counting loop" or "Definite loop“.
The for loop has the ability to iterate over the items of any sequence, such as a list or
a string.
Step 1: The iterate variable is assigned the first value in the sequence.
Step 2: All the statements in the body of for loop are executed with assigned value
of iterate variable.
Step 3: Once step 2 is over, the iterate variable is assigned the next value in
sequence and loop body is executed with the new value of iterate variable.
Step 4: This continues until all values in the sequences are processed.
Example – for loop using range()
Ex 1: Write a Python Program to print "Welcome" Ex 3: Write a Python program to print
even numbers from 1 to 10 using for
five times using for loop. loop.
Output:
Example - for loop using sequence.
Output: Output:
PRACTICE QUESTIONS
1. Write a Python program to obtain fruit name from user and print it five times using for
loop.
2. Write a Python program to print sum of even and sum of odd integer numbers from 1 to
20 using for loop.
3. Write a Python program to print the following series using for loop:
3 4 7 10 … 40.
4. Write a Python program to print the following series using for loop:
1 -4 7 -10 … -40.
5. Write a Python program to print sum of series: S= 1+ x+x2 +X3 + …. + xn
6. Write a Python program to input two numbers and print their LCM and GCD.
7. Write a Python program to print first 10 Prime numbers using for loop.
8. Write a Python program to get name from the user and print character by character using
for loop.
9.5.3 NESTED LOOPS
Nested Loop:
z
NESTED LOOP EXAMPLES
1. Write a Python Program to print multiplication table 1 to 5 using nested while loop
and nested for loop.
Python offers three types of jump statements: (i) break (ii) continue (iii) pass.
The break statement is used to terminate the loop in between the execution.
9.6.1 Break Statement
(i) Break Statement:
The break statement is used to terminate the loop which it is defined in between the
execution.
Syntax: break
How to use break in loops and nested loops
EXAMPLE PROGRAM USING break
Example :
Sample Output:
Sample Output:
Here, upto 5 number of
Here, upto 5 number of i is printed. When it i=6
i is printed. When it i=6 then it will break loop
then it will break loop and transfer control to
and transfer control to normal statement. I.e.
normal statement. I.e. outside of loop,
outside of loop,
9.6.2 Continue Statement
(ii) Continue Statement:
The continue statement is used to transfer the control to the beginning of the loop.
Whenever, continue keyword is encountered by Python. It will skip the rest of the
statements in the current loop block and continue next iteration of the loop.
Syntax: continue
How to use continue in loops and nested loops
EXAMPLE PROGRAM USING continue
Example :
The Pass statement in Python is used when a statement required syntactically but you
don’t want any command or code to execute.
Finished Finished
Sample Output:
Sample Output:
i value is: 9
i value is: 11
9.7 LOOP WITH ELSE STATEMENTS
Python allows the else keyword to be used with the for and while loops too.
i. If we use break in loop, then that time else will not work.
Loop with else statement
while condition: for variable in seq:
statement 1 statement 1
statement 2 statement 2
... ...
... ...
else: else:
statement(s) statement(s)
Sample Output:
Sample Output:
THE END