Control Statement Cs
Control Statement Cs
STATEMENT
PY T H O N
What are loops? While loops
What’s in it
for you
For loops.
What are loops?
Riya wants to print her name 10 times
Print(‘riya’
)
Print(‘riya’
)
Print(‘riya’
)
Print(‘riya’
)
OR Print(‘riya
’)
Print(‘riya’
)
Print(‘riya’
)
Print(‘riya’
)
What are loops?
A loop is an instruction that repeats multiple times as long as some condition is met
Loops in Python
While loop
While loop is used to repeat a section of code an unknown number of times
until a specific condition is met
Syntax:
while test_expression:
statements
for loop
For loop is used to iterate over a sequence, which could be a list ,tuple or string
Syntax
Val = 5
Sum1 =6+5 =11
Val=3
Sum1=11+3
=14
range() function
Programs
1. Write a program in python to print sum of natural numbers between 1 to 7 (for loop)
2. Write a program to find largest number of a list of numbers entered through keyboard.
3. Write a program to input some numbers repeatedly and print their sum.The program ends
when the users say no more to enter or program aborts when the number entered is less than
zero
4. Write a program to accept the age of n employees and out the number of persons in the
following age group: (i) 26-35 (ii) 36-45 (iii) 46-55
5. Write a python program to print every integer between 1 and n divisible by m. Also report
whether the number that is divisible by m is even or odd.
6. Write a program in python to accept transactions made in a day and items sold in a day for a
week and then print average sales per transaction (avgsales.py)
Nested for loops
Loop control statement
Loop control statement alter the regular flow of a loop
Continue
break
Skips the statements
Transfer control to the
following it and returns
statement right after the
control to the beginning
loop
of the loop
Practice work
1. Print First 10 natural numbers using while loop (first10.py)
3. Accept number from user and calculate the sum of all number between 1 and given
number (sumrange.py)
5. Given a list iterate it and display numbers which are divisible by 5 and if you find
number greater than 150 stop the loop iteration list1 = [12, 15, 32, 42, 55, 75, 122,
132, 150, 180, 200] (listdiv.py)