0% found this document useful (0 votes)
18 views

ProgFund Lect Week 5

Uploaded by

Mohi Uddin
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)
18 views

ProgFund Lect Week 5

Uploaded by

Mohi Uddin
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/ 19

Programming Fundamentals

Department of CS&IT

Loops
Introduction:
A loop can be used to tell a program to execute
statements repeatedly. Suppose that you need to
display a string (e.g., Programming is fun!) 100
times. It would be tedious to type the statement
100 times:
print("Programming is fun!")
print("Programming is fun!")
print("Programming is fun!")…….

2
Intro….
So, how do you solve this problem?
Python provides a powerful construct
called a loop, which controls how many
times in succession an operation (or a
sequence of operations) is performed. By
using a loop statement, you don’t have to
code the print statement a hundred
times; you simply tell the computer to
display a string that number of times.

19
Types of Loops.
Python provides two types of loop.
1. For loop
2. while loop

4
1. While Loop
A while loop executes statements repeatedly
as long as a condition remains true.
The syntax for the while loop is:

while loop-continuation-condition:
# Loop body
Statement(s)

5
The while loop repeatedly executes the statements in
the loop body as long as the loop-continuation-
condition evaluates to True.
Print numbers from 1 to 10
i=0
while i < 10:
i=i+1
print(i)
Print numbers from 1 to 10 with string:
i=0
while i < 10:
i=i+1
print(i,"Computer Programming")
Sum of numbers:

sum = 0
i=1
while i < 10:
sum = sum + i
i=i+1
print("sum is", sum) #
Activity
Guess the number:
Write a program where the user enters a
number. If the number matches the number,
acknowledge the user and stop the program. If
the number does not match, keep asking for
the next number.

Expected Output:
import random

number = random.randint(0, 100)


print("Guess a magic number between 0 and
100")
guess =-1
while guess != number:
guess = eval(input("Enter your guess: "))
if(guess == number):
print("Yes, the number is", number)
elif guess > number:
print("Your guess is too high")
else:
print("Your guess is too low")
2. For Loop

A Python for loop iterates through each


value in a sequence.
Comparision of For and While loop syntax:
while Loop:
i = initialValue # Initialize loop-control variable
while i < endValue:
# Loop body
...
i += 1 # Adjust loop-control variable

For Loop:
A for loop can be used to simplify the preceding loop:
for i in range(initialValue, endValue):
# Loop body

In general, the syntax of a for loop is:

for var in sequence:


Range Function:

To iterate over a sequence of numbers, the built-in


function range() comes in handy. It generates arithmetic
progressions. For example, if we want to print a whole
number from 0 to 10, it can be done using a range(11)
function in a for loop.
In a range() function we can also define an optional start
and the endpoint to the sequence. The start is always
included while the endpoint is always excluded.
Example:
for i in range(1, 6):
print("Hello World!")

Output: Prints Hello World! five times


The range() function also takes in another optional parameter i.e.
step size. Passing step size in a range() function is important
when you want to print the value uniformly with a certain
interval.
Step size is always passed as a third parameter where the first
parameter is the starting point and the second parameter is the
ending point. And if the step_size is not provided it will take 1 as
a default step_size.
Example:
for i in range(0,20, 2):
print(i)
Output: prints all the even numbers till 20
Loop through String:

for x in 'Python':
print(x)

Output:
P
Y
t
h
o
n
Program that print numb from 1 to 10:

for i in range(11):
print(i)
Program that print numb from 1 to 10
with string:

for i in range(11):
print(i,"programming")
Convert while loop into For loop:
i=1
sum = 0
while i<=10:
sum = sum + i
print(i,sum)
i += 1
Activity:

Write a program to find the sum of all the numbers less


than a number entered by the user. Display the sum.
Suppose the input is 2 3 4 5 0 (one number per
line). What is the output of the following code?

number = 0
sum = 0
for count in range(5):
number = eval(input("Enter an integer: "))
sum += number
print("sum is", sum)
print("count is", count)

You might also like