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

Chapter 4

The document discusses loops in Python programming. It covers while loops and for loops. Examples are provided to illustrate how to use while and for loops to iterate through ranges of numbers and calculate numerical series. Practice problems are given at the end for readers to write programs using while and for loops to calculate different number series expressions.

Uploaded by

riaz
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)
45 views

Chapter 4

The document discusses loops in Python programming. It covers while loops and for loops. Examples are provided to illustrate how to use while and for loops to iterate through ranges of numbers and calculate numerical series. Practice problems are given at the end for readers to write programs using while and for loops to calculate different number series expressions.

Uploaded by

riaz
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/ 39

EECP1290

Computer Programming for Engineering


Chapter 4
Loops
Topics
● Loops
○ While Loop
■ Introduction
■ Syntax
■ Examples
○ For Loop
■ Introduction
■ Syntax
■ Examples
while loop
● For while loop we need to 3 things
1. Starting Value, Example: i=10
2. Ending Value (Condition), example: i<=10
3. Increment / decrement value, example: i=i+1, i=i-1
while loop - syntax
while condition:
statements

Example:
i=1
While i<=10:
print(“Python”)
i=i+1

Note: Loop will stop if the condition is false only


while loop – Example 1
● Write a python program to print all numbers from 1 to 5

i=1
while i<=5:
print(i)
i=i+1
while loop – Example 2
● Write a python program to print odd numbers from 1 to 10.

i=1
while i<=10:
print(i)
i=i+2
while loop – Example 3
● Write a python program to print even numbers from 1 to 10.

i=2
while i<=10:
print(i)
i=i+2
while loop – Example 4
● Write a python program to print all numbers from 10 to 1.

i=10
while i>=1:
print(i)
i=i-1
while loop – Example 5
● Write a python program to print even numbers from 10 to 1.

i=10
while i>=1:
print(i)
i=i-2
while loop – Example 6
● Write a python program to print all numbers from 1 to n.

n = int(input("Enter the n value"))


i=1
while i<=n:
print(i)
i=i+1
while loop – Example 7
● Write a python program to print even numbers from 1 to n.

n = int(input("Enter the n value"))


i=2
while i<=n:
print(i)
i=i+2
while loop – Example 8
● Write a python program to print odd numbers from 1 to n.

n = int(input("Enter the n value"))


i=1
while i<=n:
print(i)
i=i+2
While Loop - Number series Problems
● Identify the starting and ending value
● Identify the difference between the first and next series
● Condition
● Increment / decrement value
● Declare the sum variable
while loop number series – Example 1
● Write a python program using while loop to calculate S=11+22+33+……+nn

n = int(input("Enter n value"))
i=1
sum=0
while i<=n:
sum = sum+pow(i,i)
i=i+1
print(sum)
while loop number series – Example 2
● Write a python program using while loop to calculate S=12+23+34+…+nn+1

n = int(input("Enter n value"))
i=1
sum=0
while i<=n:
sum = sum+pow(i,i+1)
i=i+1
print(sum)
while loop number series – Example 3
● Write a python program using while loop to calculate S=35+37+39+……+3n

n = int(input("Enter n value"))
i=5
sum=0
while i<=n:
sum = sum+pow(3,i)
i=i+2
print(sum)
while loop number series – Example 4
● Write a python program using while loop to calculate S=1x+2x+3x+……+nx

n = int(input("Enter n value"))
x = int(input("Enter x value"))
i=1
sum=0
while i<=n:
sum = sum+pow(i,x)
i=i+1
print(sum)
while loop number series – Example 5
● Write a python program using while loop to calculate S=1/2 + 2/3 + 3/4
+….+ n/n+1

n = int(input("Enter n value"))
i=1
sum=0
while i<=n:
sum = sum+i/(i+1)
i=i+1
print(sum)
while loop number series – Example 6
● Write a python program using while loop to calculate S=1x /2y + 3x /4y +
……+ nx/(n+1)y

n = int(input("Enter n value"))
x = int(input("Enter x value"))
y = int(input("Enter y value"))
i=1
sum=0
while i<=n:
sum = sum+pow(i,x)/pow(i+1,y)
i=i+2
print(sum)
while loop number series – Example 7
● Write a python program using while loop to calculate Z=4 * 8 * 12 *…*n

n = int(input("Enter n value"))
i=4
sum=1
while i<=n:
sum = sum*i
i=i+4
print(sum)
for Loop Syntax
for variable in range(start value, End value, Step):
statements

Note: The range() function defaults to 0 as a starting value and step value
as 1
for loop - Example 1
● Write a python program using for loop to print all numbers from 1 to 5.

1. for i in range(1,6,1):
print(i)

2. for i in range(1,6):
print(i)
for loop - Example 2
● Write a python program using for loop to print odd numbers from 1 to 10.

for i in range(1,11,2):
print(i)
for loop - Example 3
● Write a python program using for loop to print even numbers from 1 to 10.

for i in range(2,11,2):
print(i)
for loop - Example 4
● Write a python program using for loop to print all numbers from 10 to 1.

for i in range(10,0,-1):
print(i)
for loop - Example 5
● Write a python program using for loop to print even numbers from 10 to 1.

for i in range(10,0,-2):
print(i)
for loop - Example 6
● Write a python program using for loop to print odd numbers from 10 to 1.

for i in range(9,0,-2):
print(i)
for loop - Example 7
● Write a python program using for loop to print all numbers from 1 to n.

n = int(input("Enter the n value"))


for i in range(1,n+1,1):
print(i)
for loop - Example 8
● Write a python program using for loop to print even numbers from 1 to n.

n = int(input("Enter the n value"))


for i in range(2,n+1,2):
print(i)
for loop - Example 9
● Write a python program using for loop to print odd numbers from 1 to n.

n = int(input("Enter the n value"))


for i in range(1,n+1,2):
print(i)
for Loop - Number series Problems
● Identify the starting and ending value
● Identify the difference between the first and next series
● Increment / decrement value
● Declare the sum variable
for Loop - Number series Problems – Example 1
● Write a python program using for loop to calculate S=11+22+33+……+nn

n = int(input("Enter the n value"))


sum=0
for i in range(1,n+1,1):
sum = sum+pow(i,i)
print(sum)
for Loop - Number series Problems – Example 2
● Write a python program using for loop to calculate S=12+23+34+…+nn+1

n = int(input("Enter the n value"))


sum=0
for i in range(1,n+1,1):
sum = sum+pow(i,i+1)
print(sum)
for Loop - Number series Problems – Example 3
● Write a python program using for loop to calculate S=35+37+39+……+3n

n = int(input("Enter the n value"))


sum=0
for i in range(5,n+1,2):
sum = sum+pow(3,i)
print(sum)
for Loop - Number series Problems – Example 4
● Write a python program using for loop to calculate S=1x+2x+3x+……+nx

n = int(input("Enter the n value"))


x = int(input("Enter x value"))
sum=0
for i in range(1,n+1,1):
sum = sum+pow(1,x)
print(sum)
for Loop - Number series Problems – Example 5
● Write a python program using for loop to calculate S=1/2 + 2/3 + 3/4 +….+
n/n+1

n = int(input("Enter the n value"))


sum=0
for i in range(1,n+1,1):
sum = sum+i/(i+1)
print(sum)
for Loop - Number series Problems – Example 6
● Write a python program using for loop to calculate S=1x /2y + 3x /4y + ……+
nx/(n+1)y

n = int(input("Enter the n value"))


x = int(input("Enter x value"))
y = int(input("Enter y value"))
sum=0
for i in range(1,n+1,2):

sum = sum+pow(i,x)/pow(i+1,y)
print(sum)
for Loop - Number series Problems – Example 7
● Write a python program using for loop to calculate Z=4 * 8 * 12 *…..*n

n = int(input("Enter the n value"))


sum=1
for i in range(4,n+1,4):
sum = sum*i
print(sum)
Practice Questions
● Write a python program using for loop and while loop

○ S=1/2 + 2/3 + 3/4 +……….+ n/n+1

○ S=2/4 + 4/6 + 6/8 +……….+ n/n+2

○ S=5/10 + 5/15 + 5/20+……….+ 5/n

○ S=12 /3 + 23 /4 + 34 /5 +……+ nn+1 /n+2

○ S=1 /23 + 2 /34 + 3 /45 +……+ n/(n+1)n+2

○ S=21 /32 + 43 /54 + 65 /76 +……+ nn-1 /(n+1)n

○ S=1x /2y + 3x /4y + 5x /6y +……+ nx/(n+1)y

○ S=12 /2x + 14 /4x + 16 /6x +……+ 1n/(n)x

You might also like