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

Python-Loops

Uploaded by

Mohammed Fahad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python-Loops

Uploaded by

Mohammed Fahad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Python Notes Class XI Loop

while loop
Python scripts are given below to display numbers 1, 2, 3 4 and 5 on the screen.
k=1 k=1
print(k) print(k); k+=1
print(k+1) print(k); k+=1
print(k+2) print(k); k+=1
print(k+3) print(k); k+=1
print(k+4) print(k);
Using either of the scripts to display numbers 1, 2, 3, …, 99, 100 is not practical. There has to be a smarter
way of producing same output. In the script on the left, pair of statements print(k) and k+=1 are
getting repeated 4 times. Using a loop, it is possible to repeat statement(s) or a block for a specific number
of times. Python supports: while loop and for loop. Generally, while loop has three components:
a) Control Variable and its Initialization: while loop in Python has a control variable. Control variable
is generally an integer type or float type. But we can have Python while loop without a control variable.
If a while loop has a control variable, it is always initialized. A control variable is either an integer
variable or a floating point variable. It is called control variable because, it controls the iteration
(number times a loop gets executed) of while loop.
b) Terminating Condition: Generally, a while loop should terminate after certain number of iterations.
Terminating condition of a while loop in Python is a condition (logical expression) involving the
control variable and the stop value (final value). Condition (logical expression) plays a pivotal role in
while loop in Python while loop iteration depends on the condition.
c) Updating Control Variable: Python while loop repeats a block. Inside the block of a while loop,
control variable is updated (value of the control variable is either incremented or decremented), so that
the while loop terminates after certain number of iterations.

Rule: while Condition:


#Python Statements (while block)

The Condition is a logical expression involving control variable. First the Condition is evaluated,
if the Condition is True (nonzero), then the Block is executed. Inside the Block, control variable is
updated and Condition is tested once again. These two steps are repeated till the Condition changes
to False. When the condition is evaluated to False, the loop terminates.

k=1
while k<=4:
print(k)
k+=1 #OR, k=k+1

Running of the script produces following output: Working of the while loop:
1 k k<=4 print(k)
2 1 True 1
3 2 True 2
4 3 True 3
4 True 4
5 False
Write a Python script to generate Write a Python script to generate
and display following sequence on and display following sequence on
screen: 1, 3, 5, 7, …, 19 screen: 2, 4, 6, 8, …, 20
k=1 Control Variable Initialized k=2
while k<=19: Termination Condition while k<=20:
print(k) print(k)
k+=2 Updating Control Variable k+=2

FAIPS, DPS Kuwait Page 1 / 32


Python Notes Class XI Loop
k is the control variable. Control variable k is k is the control variable. Control variable k is
initialized to the first value in the series (k=1). In an initialized to the first value in the series (k=2). In an
ascending order series, the terminating condition is ascending order series, the terminating condition is
k<=19 (Stop value is 19) and updating control k<=20 (Stop value is 20) and updating control
variable is k+=2 (common difference is 2). variable is k+=2 (common difference is 2).

Write a Python script to generate Write a Python script to generate


and display following sequence on and display following sequence on
screen: 30, 27, 24, …, 6, 3 screen: 100, 90, 80, …, 20, 10
k=30 Control Variable Initialized k=100
while k>=3: Terminating while Condition while k>=10:
print(k) print(k)
k-=3 Updating Control Variable k-=10
k is the control variable. Control variable k is k is the control variable. Control variable k is
initialized to the first value in the series (k=3). In a initialized to the first value in the series (k=100). In
descending order series, the terminating condition a descending order series, the terminating condition
is k>=3 (Stop value is 3) and updating control is k>=10 (Stop value is 10) and updating control
variable is k-=3 (common difference is -3). variable is k-=10 (common difference is -10).

Using while loop to generate and display a sequence:


Ascending order Descending order:
ControlVar=StartVal Initialization ControlVar=StartVal
while ControlVar<=StopVal: while condition while ControlVar>=StopVal:
#Python Statements(s) while loop #Python Statements(s)
ControlVar+=CommonDiff Block ControlVar-=CommonDiff

Write a Python script to generate and display Write a Python script to generate and display
following sequence on screen: 4, 8, 12, …, 36, 40 following sequence on screen: 50, 45, 40, …, 10, 5
k=4 k=1 k=50 k=10
while k<=40: while k<=10: while k>=5: while k>=1:
print(k) print(4*k) print(k) print(5*k)
k+=4 k+=1 k-=5 k-=1
StartVal=4 StartVal=1 StartVal=50 StartVal=10
StopVal=40 StopVal=10 StopVal=5 StopVal=1
Displays ControlVar Displays 4*ControlVar Displays ControlVar Displays 5*ControlVar
CommonDiff=4 CommonDiff=1 CommonDiff=-5 CommonDiff=-1
Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 1, 2, 3, 4, …, n-1, n generate and display: n, n-1, n-2, …, 3, 2, 1
n=int(input('Input n? ')) n=int(input('Input n? '))
k=1 k=n
while k<=n: while k>=1:
print(k, end=' ') print(k, end=' ')
k+=1 k-=1
Running of the script: Running of the script:
Input n? 5 Input n? 5
1 2 3 4 5 5 4 3 2 1
Terminating condition is k<=n (n is 5). Value of k Terminating condition is k>=1. Value of k greater
less than 6 will satisfy the while condition. than 0 will satisfy the while condition.
Input n? -4 Input n? -4
OR, OR,
Input n? 0 Input n? 0
No output since while condition k<=n is false. No output since while condition k>=1 is false.
FAIPS, DPS Kuwait Page 2 / 32
Python Notes Class XI Loop
Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 1, 3, 5, …, 2n-3, 2n-1 generate and display: 2n-1, 2n-3, 2n-5, …, 3, 1
n=int(input('Input n? ')) n=int(input('Input n? '))
k=2 k=2*n-1
while k<=2*n-1: while k>=1:
print(k, end=' ') print(k, end=' ')
k+=2 k-=2
Running of the script: Running of the script:
Input n? 5 Input n? 5
1 3 5 7 9 9 7 5 3 1
Terminating condition is k<=2*n-1 (n is 5). Value Terminating condition is k>=1 (stop value is 1).
of k less than 11 will satisfy the while condition. Value of k>0 will satisfy the while condition.
Input n? -4 Input n? -4
OR, OR,
Input n? 0 Input n? 0
No output since while condition is False. No output since while condition is False.

OR, OR,

n=int(input('Input n? ')) n=int(input('Input n? '))


k=1 k=n
while k<=n: while k>=1:
print(2*k-1, end=' ') print(2*k-1, end=' ')
k+=1 k-=1
Running of the script: Running of the script:
Input n? 5 Input n? 5
1 3 5 7 9 9 7 5 3 1
Terminating condition is k<=n (n is 5). Value of k Terminating condition is k>=1 (stop value is 1).
less than 6 will satisfy the while condition. Value of k>0 will satisfy the while condition.
Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 2, 4, 6, …, 2n-2, 2n generate and display: 2n, 2n-2, 2n-4, …, 4, 2
n=int(input('Input n? ')) n=int(input('Input n? '))
k=2 k=2*n
while k<=2*n: while k>=2:
print(k, end=' ') print(k, end=' ')
k+=2 k-=2
Running of the script: Running of the script:
Input n? 5 Input n? 5
2 4 6 8 10 10 8 6 4 2
Terminating condition is k<=2*n (n is 5). Value of Terminating condition is k>=2 (stop value is 2).
k less than 12 will satisfy the while condition. Value of k>0 will satisfy the while condition.

OR, OR,

n=int(input('Input n? ')) n=int(input('Input n? '))


k=1 k=n
while k<=n: while k>=1:
print(2*k, end=' ') print(2*k, end=' ')
k+=1 k-=1
Running of the script: Running of the script:
Input n? 5 Input n? 5
2 4 6 8 10 10 8 6 4 2
Terminating condition is k<=n (n is 5). Value of k Terminating condition is k>=1 (stop value is 1).
less than 6 will satisfy the while condition. Value of k>0 will satisfy the while condition.
FAIPS, DPS Kuwait Page 3 / 32
Python Notes Class XI Loop
Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 7, 14, 21, …, 7n-14, 7n-7, 7n generate and display: 6n, 6n-6, 6n-12, …, 18, 12, 6
n=int(input('Input n? ')) n=int(input('Input n? '))
k=7 k=6*n
while k<=7*n: while k>=6:
print(k, end=' ') print(k, end=' ')
k+=7 k-=6
Running of the script: Running of the script:
Input n? 10 Input n? 10
7 14 21 28 35 42 49 56 63 70 60 54 48 42 36 30 24 18 12 6
Terminating condition is k<=7*n (n is 10). Value Terminating condition is k>=6 (stop value is 6).
of k less than 77 will satisfy the while condition. Value of k>0 will satisfy the while condition.

OR, OR,

n=int(input('Input n? ')) n=int(input('Input n? '))


k=1 k=n
while k<=n: while k>=1:
print(7*k, end=' ') print(6*k, end=' ')
k+=1 k-=1
Running of the script: Running of the script:
Input n? 10 Input n? 10
7 14 21 28 35 42 49 56 63 70 60 54 48 42 36 30 24 18 12 6
Terminating condition is k<=n (n is 10). Value of Terminating condition is k>=1 (stop value is 1).
k less than 11 will satisfy the while condition. Value of k>0 will satisfy the while condition.
Write a Python script to input two integers (say n Write a Python script to input two integers (say n
and m); then generate and display first n multiples and m); then generate and display first n multiples
of m in ascending order. of m in descending order.
n=int(input('Input n? ')) n=int(input('Input n? '))
m=int(input('Input n? ')) m=int(input('Input n? '))
k=m k=m*n
while k<=m*n: while k>=m:
print(k, end=' ') print(k, end=' ')
k+=m k-=m
Running of the script: Running of the script:
Input n? 10 Input n? 10
Input n? 3 Input n? 3
3 6 9 12 15 18 21 24 27 30 30 27 24 21 18 15 12 9 6 3
Terminating condition is k<=m*n (m*n is 30). Terminating condition is k>=m (stop value is 3).
Value of k<33 will satisfy the while condition. Value of k>0 will satisfy the while condition.
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
m=int(input('Input m? ')) m=int(input('Input m? '))
k=1 k=n
while k<=n: while k>=1:
print(m*k, end=' ') print(m*k, end=' ')
k+=1 k-=1
Running of the script: Running of the script:
Input n? 10 Input n? 10
Input n? 3 Input n? 3
3 6 9 12 15 18 21 24 27 30 30 27 24 21 18 15 12 9 6 3
Terminating condition is k<=n (n is 10). Value of Terminating condition is k>=1 (stop value is 1).
k less than 11 will satisfy the while condition. Value of k>0 will satisfy the while condition.
FAIPS, DPS Kuwait Page 4 / 32
Python Notes Class XI Loop
range()
Function range() generates a list of numbers, which is generally used to iterate over with for loop. Often
you will want to use this when you want to perform an action for fixed number of times. Function range()
has three syntaxes.
1. range(StopVal): generates sequence of integers 0, 1, 2, 3, …, StopVal-1
Start value of the sequence is always 0 (zero) when range() function has single parameter.
range(6) will generate sequence of integers 0, 1, 2, 3, 4, 5
range(11) will generate integers 0, 1, 2, 3, …, 9, 10
range(0) or range(-5) does not generate an any sequence because 0>StopVal

2. range(StartVal, StopVal): generates integers StartVal, StartVal +1, StartVal +2, , …, StopVal-1
range(1, 6) will generate sequence of integers 1, 2, 3, 4, 5
range(6, 11) will generate sequence of integers 6, 7, 8, 9, 10
range(-3, 4) will generate sequence of integers -3, -2, -1, 0, 1, 2, 3
range(3, 0) or range(3, -4) does not generate an any sequence of integer because StartVal>StopVal

3. range(StartVal, StopVal, Step)


a) Generates integers StartVal, StartVal + Step, StartVal + 2*Step, …, StopVal-1 when
StartVal<StopVal and Step>0
Generates a sequence in ascending order.
b) Generates integers StartVal, StartVal - Step, StartVal - 2*Step, …, StopVal+1 when
StartVal>StopVal and Step<0
Generates a sequence in descending order.
range(1, 6, 1) will generate sequence of integers 1, 2, 3, 4, 5
range(2, 12, 2) will generate sequence of integers 2, 4, 6, 8, 10
range(1, 12, 2) will also generate sequence of integers 1, 3, 5, 7, 9, 11
range(-1, -6, -1) will generate sequence of integers -1, -2, -3, -4, -5
range(15, 2, 3) will generate sequence of integers 15, 12, 9, 6, 3
range(-15, -2, 3) will generate sequence of integers -15, -12, -9, -6, -3
range(-5, 6, 2) will generate sequence of integers -5, -3, -1, 1, 3, 5
range(-9, 0, -2) does not generate an any sequence because StartVal<StopVal and Step is -2

StartVal: First value in the sequence


StopVal: Final value (StopVal-1) in the sequence
Step: Common difference between two successive numbers in the sequence
All parameters in the range() function must be integers. Using floating point value(s) as parameter(s)
in the range() function will trigger an error.

for loop
So far, we have used while loop to generate a sequence. Now will learn how to use for loop for the same.
Working of for loop is completely different as compared to working of while loop.

Rule: for ControlVar in range([StartVal], StopVal, [Step]):


#Python Statements (for block)

As we learned earlier, StartVal and Step are optional in function range(). In a for loop, range() function
plays the most pivotal role. Iteration of a for loop solely depends on parameters of range() function. In a
for loop there is control variable, like while loop. But unlike a while loop, in a for loop, initialization of
control variable, termination of the loop and updating control variable is done by the range() function.
First Value of the control variable is either StartVal or 0 (zero) if StartVal is missing from the range()
function. Final Value of the control variable is StopVal-1 if the sequence is in . Unlike while loop, there
is no terminating condition in a for loop. The control variable is updated by adding Step to the control
variable. Default value of Step is 1, when Step is missing from the range() function.
FAIPS, DPS Kuwait Page 5 / 32
Python Notes Class XI Loop
Write a Python script to generate and display: Write a Python script to generate and display:
1, 2, 3, 4, …, 9, 10 10, 9, 8, 7, …, 2, 1
for x in range(1, 11): for x in range(10, 0, -1):
print(x, end=' ') print(x, end=' ')
Running of the script: Running of the script:
1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1

First value of the control variable x is 1 (StartVal). First value of the control variable x is 10
Control variable x is incremented by 1 (default (StartVal). Control variable x is decremented by 1
Step value). Final value of the control variable x is (Step value). Final value of the control variable x is
10 (StopVal-1). The for loop Displays the sequence 1 (StopVal+1). The for loop Displays the sequence
in ascending order. in descending order.
Write a Python script to generate and display: Write a Python script to generate and display:
2, 4, 6, 8, …, 18, 20 19, 17, 15, 13, …, 3, 1
for x in range(2, 21, 2): for x in range(19, 0, -2):
print(x, end=' ') print(x, end=' ')
Running of the script: Running of the script:
2 4 6 8 10 12 14 16 18 20 19 17 15 13 11 9 7 5 3 1

First value of the control variable x is 2 (StartVal). First value of the control variable x is 19
Control variable x is incremented by 2 (Step (StartVal). Control variable x is decremented by 2
value). Final value of the control variable x is 20 (Step value). Final value of the control variable x is
(StopVal-1). The for loop Displays the sequence in 1 (StopVal+1). The for loop Displays the sequence
ascending order. in descending order.

OR, OR,

for x in range(1, 11): for x in range(10, 0, -1):


print(2*x, end=' ') print(2*x-1, end=' ')

Running of the script will produce the same output. Running of the script will produce the same output.
The for loop generates numbers 1, 2, 3, … 10 but The for loop generates numbers 10, 9, 8, … 1 but
print() function displays 2*x. print() function displays 2*x-1.
Write a Python script to generate and display: Write a Python script to generate and display:
4, 8, 12, 16, …, 36, 40 50, 45, 40, 35, …, 10, 5
for x in range(4, 41, 4): for x in range(50, 4, -5):
print(x, end=' ') print(x, end=' ')
Running of the script: Running of the script:
4 8 12 16 20 24 28 32 36 40 50 45 40 35 30 25 20 15 10 5

First value of the control variable x is 4 (StartVal). First value of the control variable x is 50
Control variable x is incremented by 4 (Step (StartVal). Control variable x is decremented by 5
value). Final value of the control variable x is 40 (Step value). Final value of the control variable x is
(StopVal-1). The for loop Displays the sequence in 5 (StopVal+1). The for loop Displays the sequence
ascending order. in descending order.

OR, OR,

for x in range(1, 11): for x in range(10, 0, -1):


print(4*x, end=' ') print(5*x, end=' ')

Running of the script will produce the same output. Running of the script will produce the same output.

FAIPS, DPS Kuwait Page 6 / 32


Python Notes Class XI Loop
Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 1, 2, 3, 4, …, n-1, n generate and display: n, n-1, n-2, …, 3, 2, 1
n=int(input('Input n? ')) n=int(input('Input n? '))
for x in range(1, n+1): for x in range(n, 0, -1):
print(x, end=' ') print(x, end=' ')
Running of the script: Running of the script:
Input n? 5 Input n? 5
1 2 3 4 5 5 4 3 2 1

Input n? -4 Input n? -4
OR, OR,
Input n? 0 Input n? 0
No output since range() function will not generate No output since range() function will not generate
any sequence. any sequence.
Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 1, 3, 5, …, 2n-3, 2n-1 generate and display: 2n-1, 2n-3, 2n-5, …, 3, 1
n=int(input('Input n? ')) n=int(input('Input n? '))
for x in range(1, 2*n, 2): for x in range(2*n-1, 0, -2):
print(x, end=' ') print(x, end=' ')
Running of the script: Running of the script:
Input n? 5 Input n? 5
1 3 5 7 9 9 7 5 3 1

Input n? -4 Input n? -4
OR, OR,
Input n? 0 Input n? 0
No output since range() function will not generate No output since range() function will not generate
any sequence. any sequence.
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
for x in range(1, n+1): for x in range(n, 0, -1):
print(2*x-1, end=' ') print(2*x-1, end=' ')
Running of the script will produce the same output. Running of the script will produce the same output.
Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 2, 4, 6, …, 2n-2, 2n generate and display: 2n, 2n-2, 2n-4, …, 4, 2
n=int(input('Input n? ')) n=int(input('Input n? '))
for x in range(2, 2*n+1, 2): for x in range(2*n, 1, -2):
print(x, end=' ') print(x, end=' ')
Running of the script: Running of the script:
Input n? 5 Input n? 5
2 4 6 8 10 10 8 6 4 2
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
for x in range(1, n+1): for x in range(n, 0, -1):
print(2*x, end=' ') print(2*x, end=' ')
Running of the script will produce the same output. Running of the script will produce the same output.
Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 7, 14, 21, …, 7n-14, 7n-7, 7n generate and display: 6n, 6n-6, 6n-12, …, 18, 12, 6
n=int(input('Input n? ')) n=int(input('Input n? '))
for x in range(7, 7*n+1, 7): for x in range(6*n, 5, -6):
print(x, end=' ') print(x, end=' ')
FAIPS, DPS Kuwait Page 7 / 32
Python Notes Class XI Loop
Running of the script: Running of the script:
Input n? 10 Input n? 10
7 14 21 28 35 42 49 56 63 70 60 54 48 42 36 30 24 18 12 6

OR, OR,

n=int(input('Input n? ')) n=int(input('Input n? '))


for x in range(1, n+1): for x in range(n, 0, -1):
print(7*x, end=' ') print(6*x, end=' ')
Running of the script will produce the same output. Running of the script will produce the same output.
Write a Python script to input two integers (say n Write a Python script to input two integers (say n
and m); then generate and display first n multiples and m); then generate and display first n multiples
of m in ascending order. of m in descending order.
n=int(input('Input n? ')) n=int(input('Input n? '))
m=int(input('Input n? ')) m=int(input('Input n? '))
for x in range(m, m*n+1, m) for x in range(m*n, m-1, -m)
print(x, end=' ') print(x, end=' ')
Running of the script: Running of the script:
Input n? 10 Input n? 10
Input n? 3 Input n? 3
3 6 9 12 15 18 21 24 27 30 30 27 24 21 18 15 12 9 6 3

OR, OR,

n=int(input('Input n? ')) n=int(input('Input n? '))


m=int(input('Input m? ')) m=int(input('Input m? '))
for x in range(1, n+1): for x in range(n, 0, -1):
print(m*x, end=' ') print(m*x, end=' ')
Running of the script will produce the same output. Running of the script will produce the same output.
Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 1, 2, 3, 4, …, n-1, n generate and display: 1, 2, 3, 4, …, n-1, n
n=int(input('Input n? ')) n=int(input('Input n? '))
k=1 for x in range(1, n+1):
while k<=n: print(x, end=' ')
print(k, end=' ') print('\nx=',x)
k+=1 Running of the script:
print('\nk=',k) Input n? 5
Running of the script: 1 2 3 4 5
Input n? 5 x= 5
1 2 3 4 5 Displaying the control variable x, after the end of
k= 6 the for loop will display the final value generated by
Displaying the control variable k, after the end of the range() function.
the while loop will display the value exceeding the
final value. Input n? 0

Input n? 0 NameError: name 'x' is not defined


k= 1 Since the inputted value of n is 0 (zero), for loop
does not ger executed. No output is generated
Since the inputted value of n is 0 (zero), while loop through the for loop. Also control variable x is not
does not ger executed because while condition is created since the for loop does not get executed.
False. No output is generated through the while After the end of the for loop, the print() function is
loop. But the print() function after the while loop displaying a variable x, which is not being created,
will display the initial value of the control variable and that's triggers an error. NameError – trying to
k. So, k= 1 is displayed. access a variable without creating the variable.
FAIPS, DPS Kuwait Page 8 / 32
Python Notes Class XI Loop
How should one go about generating and displaying a sequence where every term is either a square
or a cube, as given below?
12, 22, 32, 42, …, n2 OR, 12, 32, 52, 72, …, (2n-1)2 OR, 22, 42, 62, 82, …, (2n)2
1 , 2 , 3 , 4 , …, n
3 3 3 3 3
OR, 1 , 3 , 5 , 7 , …, (2n-1)
3 3 3 3 3
OR, 23, 43, 63, 83, …, (2n)3

Using while loop to generate sequence containing Using while loop to generate sequence containing
square of the numbers: cube of the numbers:
CVar=Start CVar=Start
while CVar<=Stop: while CVar<=Stop:
print(CVar**2) print(CVar**3)
CVar+=CDiff CVar+=CDiff
Generate the sequence by ignoring the power, but Generate the sequence by ignoring the power, but
display the square of the number. display the cube of the number.
Using for loop to generate sequence containing Using for loop to generate sequence containing cube
square of the numbers: of the numbers:
for CVar in range(Start,Stop,Step): for CVar in range(Start,Stop,Step):
print(CVar**2) print(CVar**3)
Generate the sequence by ignoring the power, but Generate the sequence by ignoring the power, but
display the square of the number. display the cube of the number.
CVar – Control Variable, Start – Start Value, Stop – Stop Value, Cdiff – Common Difference,
Step – Step Value

Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 12, 22, 32, 42, …, (n-1)2, n2 generate and display: 13, 23, 33, 43, …, (n-1)3, n3
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=1 k=1
while k<=n: while k<=n:
print(k*k, end=' ') print(k*k*k, end=' ')
k+=1 k+=1
print('\nUsing "for" loop') print('\nUsing "for" loop')
for x in range(1, n+1): for x in range(1, n+1):
print(x**2, end=' ') print(x**3, end=' ')
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=1 k=1
while k<=n: while k<=n:
t2=k*k t3=k*k*k
print(t2, end=' ') print(t3, end=' ')
k+=1 k+=1
print('\nUsing "for" loop') print('\nUsing "for" loop')
for x in range(1, n+1): for x in range(1, n+1):
t2=x**2 t3=x**3
print(t2, end=' ') print(t3, end=' ')
Running of the script produces following output Running of the script produces following output
Input n? 10 Input n? 10
Using "while" loop Using "while" loop
1 4 9 16 25 36 49 64 81 100 1 8 27 64 125 216 343 512 729 1000
Using "for" loop Using "for" loop
1 4 9 16 25 36 49 64 81 100 1 8 27 64 125 216 343 512 729 1000
Both while loop and for loop are used so that one Both while loop and for loop are used so that one
can understand how to use both the loops. In the can understand how to use both the loops. In the
Exam you have to use either while loop or for loop. Exam you have to use either while loop or for loop.
FAIPS, DPS Kuwait Page 9 / 32
Python Notes Class XI Loop
Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 22, 42, 62, …, (2n-2)2, (2n)2 generate and display: 23, 43, 63, …, (2n-2)3, (2n)3
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=2 k=2
while k<=2*n: while k<=2*n:
print(k*k, end=' ') print(k*k*k, end=' ')
k+=2 k+=2
print('\nUsing "for" loop') print('\nUsing "for" loop')
for x in range(2, 2*n+1, 2): for x in range(2, 2*n+1, 2):
print(x**2, end=' ') print(x**3, end=' ')
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=1 k=1
while k<=n: while k<=n:
t=2*k; t=2*k;
t2=t*t t3=t*t*t
print(t2, end=' ') print(t3, end=' ')
k+=1 k+=1
print('\nUsing "for" loop') print('\nUsing "for" loop')
for x in range(1, n+1): for x in range(1, n+1):
t=2*x; t=2*x;
t2=t**2 t3=t**3
print(t2, end=' ') print(t3, end=' ')
Running of the script produces following output Running of the script produces following output
Input n? 8 Input n? 8
Using "while" loop Using "while" loop
4 16 36 64 100 144 196 256 8 64 216 512 1000 1728 2744 4096
Using "for" loop Using "for" loop
4 16 36 64 100 144 196 256 8 64 216 512 1000 1728 2744 4096
Both while loop and for loop are used so that one Both while loop and for loop are used so that one
can understand how to use both the loops. In the can understand how to use both the loops. In the
Exam you have to use either while loop or for loop. Exam you have to use either while loop or for loop.
Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 12, 32, 52, …, (2n-3)2, (2n-1)2 generate and display: 13, 33, 53, …, (2n-3)3, (2n-1)3
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=1 k=1
while k<=2*n-1: while k<=2*n-1:
print(k*k, end=' ') print(k*k*k, end=' ')
k+=2 k+=2
print('\nUsing "for" loop') print('\nUsing "for" loop')
for x in range(1, 2*n, 2): for x in range(1, 2*n, 2):
print(x**2, end=' ') print(x**3, end=' ')
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=1 k=1
while k<=n: while k<=n:
t=2*k-1; t=2*k-1;
t2=t*t t3=t*t*t
print(t2, end=' ') print(t3, end=' ')
k+=1 k+=1
FAIPS, DPS Kuwait Page 10 / 32
Python Notes Class XI Loop
print('\nUsing "for" loop') print('\nUsing "for" loop')
for x in range(1, n+1): for x in range(1, n+1):
t=2*x-1; t=2*x-1;
t2=t**2 t3=t**3
print(t2, end=' ') print(t3, end=' ')
Running of the script produces following output Running of the script produces following output
Input n? 8 Input n? 8
Using "while" loop Using "while" loop
1 9 25 49 81 121 169 225 1 27 125 343 729 1331 2197 3375
Using "for" loop Using "for" loop
1 9 25 49 81 121 169 225 1 27 125 343 729 1331 2197 3375
Write a Python script to input an integer (say n); then Write a Python script to input an integer (say n); then
generate and display: 42, 82, 122, …, (4n-4)2, (2n)2 generate and display: 33, 63, 93, …, (3n-3)3, (3n)3
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=4 k=3
while k<=4*n: while k<=3*n:
print(k*k, end=' ') print(k*k*k, end=' ')
k+=4 k+=3
print('\nUsing "for" loop') print('\nUsing "for" loop')
for x in range(4, 4*n+1, 4): for x in range(3, 3*n+1, 3):
print(x**2, end=' ') print(x**3, end=' ')
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=1 k=1
while k<=n: while k<=n:
t=4*k; t=3*k;
t2=t*t t3=t*t*t
print(t2, end=' ') print(t3, end=' ')
k+=1 k+=1
print('\nUsing "for" loop') print('\nUsing "for" loop')
for x in range(1, n+1): for x in range(1, n+1):
t=4*x; t=3*x;
t2=t**2 t3=t**3
print(t2, end=' ') print(t3, end=' ')
Running of the script produces following output Running of the script produces following output
Input n? 8 Input n? 7
Using "while" loop Using "while" loop
16 64 144 256 400 576 784 1024 27 216 729 1728 3375 5832 9261
Using "for" loop Using "for" loop
16 64 144 256 400 576 784 1024 27 216 729 1728 3375 5832 9261

So far, we have used loop for generating and displaying a sequence. Another application of loop is to find
the sum or the product of a sequence. To find the sum or the product of a sequence we need to introduce
a new concept called accumulator. Initially concept of accumulator will be explained to find the sum of a
sequence. But later on, we will use an accumulator to find the product of a sequence. To find the sum of
a sequence, one needs to generate the sequence. We have already learned how to generate a sequence
using while loop and for loop. Therefore, what we have learned earlier regarding sequence is also relevant
for sum of a sequence. Unless and until, we are able to generate the sequence, one cannot find the sum of
the sequence. We will start with very simple example, Python script to find the sum of first n natural
numbers (1+2+3+…n-1+n). One can question, why write a script, when a Mathematical formula is
available to find the sum. The answer is, Mathematical cannot be used to find the sum of every sequence.
How to find the sum of complicated sequence will be discussed and explained later.
FAIPS, DPS Kuwait Page 11 / 32
Python Notes Class XI Loop
Write a Python script to input an integer n and find Every accumulator has to be initialized to 0 (zero)
the sum of the sequence: 1+2+3+4+…+(n-1)+n or some other value (depending on the need of the
n=int(input('Input n? ')) script). In the example on the left, the accumulator
print('Using "while" loop') asum is initialized to 0 (zero) for both the loops.
asum=0 asum=0
k=1
The while loop and the for loop is used to generate
while k<=n:
the sequence. Both loops are used, so than one
print(k, end=' ')
knows how to use both the loops. Accumulator
asum+=k
k+=1 works exactly the same way for both the loops.
print('\nSum =',asum) Inside the block of the loops, the accumulator asum
print('-'*20) is updated as:
print('Using "for" loop') • asum+=k in while loop
asum=0 • asum+=x in for loop
for x in range(1, n+1): Note: in while loop, accumulator (asum) is first
print(x, end=' ') updated and then control variable (k) is updated. No
asum+=x such issue in for loop, since range() function
print('\nSum =',asum) updates the control variable (x).
Whether to display the sequence, is your discretion.
Running of the script produces following output If you don't want display the sequence, then remove:
Input n? 5 • print(k, end=' ') from the while loop
Using "while" loop
• print(x, end=' ') from the for loop
1 2 3 4 5
Sum = 15 The accumulator (asum) is displayed after the end
-------------------- of the loop, print('\nSum=',asum)
Using "for" loop Displaying accumulator inside the loop, will display
1 2 3 4 5 value of the accumulator for every value of the
Sum = 15 control variable.
Working of the accumulator with while loop: Working of the accumulator with for loop:
k k<=n asum x asum
1 True 1 1 1
2 True 3 2 3
3 True 6 3 6
4 True 10 4 10
5 True 15 5 15
6 False Sum = 15
Sum = 15
n=int(input('Input n? ')) n=int(input('Input n? '))
k, asum=1, 0 k, asum=1, 0
while k<=n: while k<=n:
asum+=k; asum+=k; k+=1
k+=1 print(asum, end=' ')
print('Sum =',asum) print()
asum=0 asum=0
for x in range(1, n+1): asum+=x for x in range(1, n+1):
print('Sum =',asum) asum+=x
print(asum, end=' ')
Running of the script produces following output Running of the script produces following output
Input n? 10 Input n? 10
Sum = 55 1 3 6 10 15 21 28 36 45 55
Sum = 55 1 3 6 10 15 21 28 36 45 55
Loops are used to find the sum of sequence only. Accumulator is displayed inside the loops. Displays
Sequence is not displayed on the screen. Output is value of the accumulator for every value of the
the sum of the sequence at the end of the loops. control variable.
FAIPS, DPS Kuwait Page 12 / 32
Python Notes Class XI Loop
n=int(input('Input n? ')) n=int(input('Input n? '))
k, asum=1, 0 k, asum=1, 0
while k<=n: while k<=n:
k+=1 asum+=k;
asum+=k; print(k, asum, sep='\t')
print('Sum =',asum) k+=1

Running of the script produces following output Running of the script produces following output
Input n? 10 Input n? 10
Sum = 65 1 1
Since the control variable (k) is updated before 2 3
updating the accumulator (asum), control variable's 3 6
initial value (k=1) is not added to the accumulator 4 10
but 11 is added to accumulator. Code is syntactically 5 15
correct but it has a logic error. How to remove the 6 21
logical error without swapping the two statements 7 28
inside the loop? 8 36
n=int(input('Input n? ')) 9 45
k=asum=0 10 55
while k<n: Value of the control variable and the accumulator is
k+=1 displayed inside the loop. The first column displays
asum+=k; the value of the control variable and the second
print('Sum =',asum) column displays the value of the accumulator.
Corrections are highlighted. Instead of while loop, you could have used for loop.
Write a Python script to input an integer n and find Write a Python script to input an integer n and find
the sum of the sequence: 2+4+6+8…+(2n-2)+2n the sum of the sequence:1+3+5+7+…+(2n-3)+2n-1
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k, asum=2, 0 k, asum=1, 0
while k<=2*n: while k<=2*n-1:
print(k, end=' ') print(k, end=' ')
asum+=k asum+=k
k+=2 k+=2
print('\nSum =',asum) print('\nSum =',asum)
print('Using "for" loop') print('Using "for" loop')
asum=0 asum=0
for x in range(2, 2*n+1, 2): for x in range(1, 2*n, 2):
print(x, end=' ') print(x, end=' ')
asum+=x asum+=x
print('\nSum =',asum) print('\nSum =',asum)
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k, asum=1, 0 k, asum=1, 0
while k<=n: while k<=n:
t=2*k t=2*k-1
print(t, end=' ') print(t, end=' ')
asum+=t asum+=t
k+=1 k+=1
print('\nSum =',asum) print('\nSum =',asum)
print('Using "for" loop') print('Using "for" loop')
asum=0 asum=0
for x in range(1, n+1): for x in range(1, n+1):
t=2*x t=2*x-1
FAIPS, DPS Kuwait Page 13 / 32
Python Notes Class XI Loop
print(t, end=' ') print(t, end=' ')
asum+=t asum+=t
print('\nSum =',asum) print('\nSum =',asum)

Running of the script produces following output Running of the script produces following output
Input n? 10 Input n? 10
Using "while" loop Using "while" loop
2 4 6 8 10 12 14 16 18 20 1 3 5 7 9 11 13 15 17 19
Sum = 110 Sum = 100
Using "for" loop Using "for" loop
2 4 6 8 10 12 14 16 18 20 1 3 5 7 9 11 13 15 17 19
Sum = 110 Sum = 100
Working of while loop: Working of for loop: Working of while loop: Working of for loop:
k k<=n asum x asum k k<=n asum x asum
2 True 2 2 2 1 True 1 1 1
4 True 6 4 6 3 True 4 3 4
6 True 12 6 12 5 True 9 5 9
8 True 20 8 20 7 True 16 7 16
10 True 30 10 30 9 True 25 9 25
12 True 42 12 42 11 True 36 11 36
14 True 56 14 56 13 True 49 13 49
16 True 72 16 72 15 True 64 15 64
18 True 90 18 90 17 True 81 17 81
20 True 110 20 110 19 True 100 19 100
22 False Sum = 110 21 False Sum = 100
Sum = 110 Sum = 100
Write a Python script to input an integer n and find Write a Python script to input an integer n and find
the sum of the sequence: 4+8+12+…+(4n-4)+4n the sum of the sequence: 5+10+15+…+(5n-5)+5n
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=4 k=5
asum=0 asum=0
while k<=4*n: while k<=5*n:
print(k, end=' ') print(k, end=' ')
asum+=k asum+=k
k+=4 k+=5
print('\nSum =',asum) print('\nSum =',asum)
print('Using "for" loop') print('Using "for" loop')
asum=0 asum=0
for x in range(4, 4*n+1, 4): for x in range(5, 5*n+1, 5):
print(x, end=' ') print(x, end=' ')
asum+=x asum+=x
print('\nSum =',asum) print('\nSum =',asum)
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=1 k=1
asum=0 asum=0
while k<=n: while k<=n:
t=4*k t=5*k
print(t, end=' ') print(t, end=' ')
asum+=t asum+=t
k+=1 k+=1
print('\nSum =',asum) print('\nSum =',asum)
FAIPS, DPS Kuwait Page 14 / 32
Python Notes Class XI Loop
print('Using "for" loop') print('Using "for" loop')
asum=0 asum=0
for x in range(1, n+1): for x in range(1, n+1):
t=4*x t=5*x
print(t, end=' ') print(t, end=' ')
asum+=t asum+=t
print('\nSum =',asum) print('\nSum =',asum)

Running of the script produces following output Running of the script produces following output
Input n? 10 Input n? 10
Using "while" loop Using "while" loop
4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50
Sum = 220 Sum = 275
Using "for" loop Using "for" loop
4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50
Sum = 220 Sum = 275
Write a Python script to input an integer n and find Write a Python script to input an integer n and find
the sum of the sequence: 12+22+32+…+(n-1)2+n2 the sum of the sequence: 13+23+33+…+(n-1)3+n3
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k, asum=1, 0 k, asum=1, 0
while k<=n: while k<=n:
print(k*k, end=' ') print(k*k*k, end=' ')
asum+=k*k asum+=k*k*k
k+=1 k+=1
print('\nUsing "for" loop') print('\nUsing "for" loop')
asum=0 asum=0
for x in range(1, n+1): for x in range(1, n+1):
print(x**2, end=' ') print(x**3, end=' ')
asum+=x**2 asum+=x**3
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k, asum=1, 0 k, asum=1, 0
while k<=n: while k<=n:
t2=k*k t3=k*k*k
print(t2, end=' ') print(t3, end=' ')
asum+=t2 asum+=t3
k+=1 k+=1
print('\nUsing "for" loop') print('\nUsing "for" loop')
asum=0 asum=0
for x in range(1, n+1): for x in range(1, n+1):
t2=x**2 t3=x**3
print(t2, end=' ') print(t3, end=' ')
asum+=t2 asum+=t3

Running of the script produces following output Running of the script produces following output
Input n? 10 Input n? 10
Using "while" loop Using "while" loop
1 4 9 16 25 36 49 64 81 100 1 8 27 64 125 216 343 512 729 1000
Sum = 385 Sum = 3025
Using "for" loop Using "for" loop
1 4 9 16 25 36 49 64 81 100 1 8 27 64 125 216 343 512 729 1000
Sum = 385 Sum = 3025
FAIPS, DPS Kuwait Page 15 / 32
Python Notes Class XI Loop
Working of while loop: Working of for loop: Working of for loop: Working of for loop:
k Cond k**2 asum x x**2 asum k Cond k**3 asum x x**3 asum
1 True 1 1 1 1 1 1 True 1 1 1 1 1
2 True 4 5 2 4 5 2 True 8 9 2 8 9
3 True 9 14 3 9 14 3 True 27 36 3 27 36
4 True 16 30 4 16 30 4 True 64 100 4 64 100
5 True 25 55 5 25 55 5 True 125 225 5 125 225
6 True 36 91 6 36 91 6 True 216 441 6 216 441
7 True 49 140 7 49 140 7 True 343 784 7 343 784
8 True 64 204 8 64 204 8 True 512 1296 8 512 1296
9 True 81 285 9 81 285 9 True 729 2025 9 729 2025
10 True 100 385 10 100 385 10 True 1000 3025 10 1000 3025
11 False 11 False
Write a Python script to input an integer n and find Write a Python script to input an integer n and find
the sum of the sequence: 22+42+62+82+…+(2n)2 the sum of the sequence: 23+43+63+83+…+(2n)3
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=2 k=2
asum=0 asum=0
while k<=2*n: while k<=2*n:
print(k*k, end=' ') print(k*k*k, end=' ')
asum+=k*k asum+=k*k*k
k+=2 k+=2
print('\nUsing "for" loop') print('\nUsing "for" loop')
asum=0 asum=0
for x in range(2, 2*n+1, 2): for x in range(2, 2*n+1, 2):
print(x**2, end=' ') print(x**3, end=' ')
asum+=x**2 asum+=x**3
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k, asum=1, 0 k, asum=1, 0
while k<=n: while k<=n:
t=2*k t=2*k
t2=t*t t3=t*t*t
print(t2, end=' ') print(t3, end=' ')
asum+=t2 asum+=t3
k+=1 k+=1
print('\nUsing "for" loop') print('\nUsing "for" loop')
asum=0 asum=0
for x in range(1, n+1): for x in range(1, n+1):
t=2*k t=2*x
t2=t**2 t3=t**3
print(t2, end=' ') print(t3, end=' ')
asum+=t2 asum+=t3

Running of the script produces following output Running of the script produces following output
Input n? 10 Input n? 8
Using "while" loop Using "while" loop
4 16 36 64 100 144 196 256 324 400 8 64 216 512 1000 1728 2744 4096
Sum = 1540 Sum = 10368
Using "for" loop Using "for" loop
4 16 36 64 100 144 196 256 324 400 8 64 216 512 1000 1728 2744 4096
Sum = 1540 Sum = 10368
FAIPS, DPS Kuwait Page 16 / 32
Python Notes Class XI Loop
Write a Python script to input an integer n and find Write a Python script to input an integer n and find
the sum of the sequence: 12+32+72+92+…+(2n-1)2 the sum of the sequence: 13+33+53+73+…+(2n-1)3
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k, sum=1, 0 k, sum=1, 0
while k<=2*n-1: while k<=2*n-1:
print(k*k, end=' ') print(k*k*k, end=' ')
asum+=k*k asum+=k*k*k
k+=2 k+=2
print('\nUsing "for" loop') print('\nUsing "for" loop')
asum=0 asum=0
for x in range(1, 2*n, 2): for x in range(1, 2*n, 2):
print(x**2, end=' ') print(x**3, end=' ')
asum+=x**2 asum+=x**3
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k, asum=1, 0 k, asum=1, 0
while k<=n: while k<=n:
t=2*k-1; t2=t*t t=2*k-1; t3=t*t*t
print(t2, end=' ') print(t3, end=' ')
asum+=t2 asum+=t3
k+=1 k+=1
print('\nUsing "for" loop') print('\nUsing "for" loop')
asum=0 asum=0
for x in range(1, n+1): for x in range(1, n+1):
t=2*x-1; t2=t**2 t=2*x-1; t3=t**3
print(t2, end=' ') print(t3, end=' ')
asum+=t2 asum+=t3

Running of the script produces following output Running of the script produces following output
Input n? 10 Input n? 8
Using "while" loop Using "while" loop
1 9 25 49 81 121 169 225 289 361 1 27 125 343 729 1331 2197 3375
Sum = 1330 Sum = 8128
Using "for" loop Using "for" loop
1 9 25 49 81 121 169 225 289 361 1 27 125 343 729 1331 2197 3375
Sum = 1330 Sum = 8128
Write a Python script to input an integer n and find Write a Python script to input an integer n and find
the sum of the sequence: 42+82+122+162+…+(4n)2 the sum of the sequence: 53+103+153+203+…+(5n)3
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=4 k=5
asum=0 asum=0
while k<=4*n: while k<=5*n:
print(k*k, end=' ') print(k*k*k, end=' ')
asum+=k*k asum+=k*k*k
k+=4 k+=5
print('\nUsing "for" loop') print('\nUsing "for" loop')
asum=0 asum=0
for x in range(4, 4*n+1, 4): for x in range(5, 5*n+1, 5):
print(x**2, end=' ') print(x**3, end=' ')
asum+=x**2 asum+=x**3
OR, OR,
FAIPS, DPS Kuwait Page 17 / 32
Python Notes Class XI Loop
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k, asum=1, 0 k, asum=1, 0
while k<=n: while k<=n:
t=4*k t=5*k
t2=t*t t3=t*t*t
print(t2, end=' ') print(t3, end=' ')
asum+=t2 asum+=t3
k+=1 k+=1
print('\nUsing "for" loop') print('\nUsing "for" loop')
asum=0 asum=0
for x in range(1, n+1): for x in range(1, n+1):
t=4*k t=5*x
t2=t**2 t3=t**3
print(t2, end=' ') print(t3, end=' ')
asum+=t2 asum+=t3

How to find the sum of the sequence: (1) + (1+2) + (1+2+3) + (1+2+3+4) +…+ (1+2+3+…+n)
This type of sequence requires two accumulators, one for every group – every group is a term in this
sequence and the other for the sequence within a group. The sequence has n terms (groups). So a loop
has to be used to generate first n natural number.
(1) 1st Group (Term) Sum of First 1 natural number
Sum (1+2) 2nd Group (Term) Sum of First 2 natural numbers
Of Sum
(1+2+3) 3rd Group (Term) Sum of First 3 natural numbers
The Within
(1+2+3+4) 4th Group (Term) Sum of First 4 natural numbers
Terms The
:
Or Group
:
Groups (1+2+3+4+…+n) nth Group (Term) Sum of First n natural numbers

n=int(input('Input n? ')) asum=0 Accumulator for sum within the group


print('Using "while" loop') gsum=0 Accumulator for sum of the group
asum=0
gsum=0 The while loop and the for loop is used to generate
k=1 the sequence. Both loops are used, so you know how
while k<=n: to use while and for loops. Inside while loop block:
asum+=k asum+=k Sum within the group
gsum+=asum gsum+=asum Sum of the group
k+=1 k+=1 Updating the counter
print('Sum =',gsum) Inside for loop block:
print('-'*20) asum+=x Sum within the group
print('Using "for" loop') gsum+=asum Sum of the group
asum=gsum=0
for x in range(1, n+1): Working of the loop:
asum+=x k(x) asum gsum
gsum+=asum 1 1 1
print('Sum =',gsum) 2 3 4
3 6 10
Running of the script produces following output 4 10 20
Input n? 10 5 15 35
Using "while" loop 6 21 56
Sum = 220 7 28 84
-------------------- 8 36 120
Using "for" loop 9 45 165
Sum = 220 10 55 220
FAIPS, DPS Kuwait Page 18 / 32
Python Notes Class XI Loop
Write a Python script to input an integer n and find Write a Python script to input an integer n and find
the sum of the sequence: the sum of the sequence:
(2) + (2+4) + (2+4+6) + … + (2+4+6+…+2n) (1) + (1+3) + (1+3+5) + … + (1+3+5+…+2n-1)
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
asum=gsum=0 asum=gsum=0
k=2 k=1
while k<=2*n: while k<=2*n-1:
asum+=k asum+=k
gsum+=asum gsum+=asum
k+=2 k+=2
print('Sum =',gsum) print('Sum =',gsum)
print('Using "for" loop') print('Using "for" loop')
asum=gsum=0 asum=gsum=0
for x in range(2, 2*n+1, 2): for x in range(1, 2*n, 2):
asum+=x asum+=x
gsum+=asum gsum+=asum
print('Sum =',gsum) print('Sum =',gsum)
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
asum=gsum=0 asum=gsum=0
k=1 k=1
while k<=n: while k<=n:
t=2*k t=2*k-1
asum+=t asum+=t
gsum+=asum gsum+=asum
k+=1 k+=1
print('Sum =',gsum) print('Sum =',gsum)
print('Using "for" loop') print('Using "for" loop')
asum=gsum=0 asum=gsum=0
for x in range(1, n+1): for x in range(1, n+1):
t=2*x t=2*x-1
asum+=x asum+=x
gsum+=asum gsum+=asum
print('Sum =',gsum) print('Sum =',gsum)

Running of the script produces following output Running of the script produces following output
Input n? 10 Input n? 10
Using "while" loop Using "while" loop
Sum = 440 Sum = 385
Using "for" loop Using "for" loop
Sum = 440 Sum = 385
Write a Python script to input an integer n and find Write a Python script to input an integer n and find
the sum of the sequence: the sum of the sequence:
(12)+(12+22)+(12+22+32)+…+(12+22+32+…+n2) (13)+(13+23)+(13+23+33)+…+(13+23+33+…+n3)
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
asum=gsum=0 asum=gsum=0
k=1 k=1
while k<=n: while k<=n:
asum+=k*k asum+=k*k*k
gsum+=asum gsum+=asum
k+=1 k+=1
FAIPS, DPS Kuwait Page 19 / 32
Python Notes Class XI Loop
print('Sum =',gsum) print('Sum =',gsum)
print('Using "for" loop') print('Using "for" loop')
asum=gsum=0 asum=gsum=0
for x in range(1, n+1): for x in range(1, n+1):
asum+=x**2 asum+=x**3
gsum+=asum gsum+=asum
print('Sum =',gsum) print('Sum =',gsum)

Running of the script produces following output Running of the script produces following output
Input n? 10 Input n? 10
Using "while" loop Using "while" loop
Sum = 1210 Sum = 7942
Using "for" loop Using "for" loop
Sum = 1210 Sum = 7942
Write a Python script to input an integer n and find Write a Python script to input an integer n and find
the sum of the sequence: the sum of the sequence:
(22)+(22+42)+(22+42+62)+…+(22+42+62+…+(2n)2) (23)+(23+43)+(23+43+63)+…+(23+43+63+…+(2n)3)
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k, asum, gsum=2, 0, 0 k, asum, gsum=2, 0, 0
while k<=2*n: while k<=2*n:
asum+=k*k asum+=k*k*k
gsum+=asum gsum+=asum
k+=2 k+=2
print('Sum =',gsum) print('Sum =',gsum)
print('Using "for" loop') print('Using "for" loop')
asum=gsum=0 asum=gsum=0
for x in range(2, 2*n+1, 2): for x in range(2, 2*n+1, 2):
asum+=x**2 asum+=x**3
gsum+=asum gsum+=asum
print('Sum =',gsum) print('Sum =',gsum)
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k, asum, gsum=1, 0 , 0 k, asum, gsum=1, 0 , 0
while k<=n: while k<=n:
t=2*k t=2*k
asum+=t*t asum+=t*t*t
gsum+=asum gsum+=asum
k+=1 k+=1
print('Sum =',gsum) print('Sum =',gsum)
print('Using "for" loop') print('Using "for" loop')
asum=gsum=0 asum=gsum=0
for x in range(1, n+1): for x in range(1, n+1):
t=2*x t=2*x
asum+=t**2 asum+=t**3
gsum+=asum gsum+=asum
print('Sum =',gsum) print('Sum =',gsum)
Write a Python script to input an integer n and find Write a Python script to input an integer n and find
the sum of the sequence: the sum of the sequence:
(12)+(12+32)+(12+32+52)+…+(12+32+…+(2n-1)2) (13)+(13+33)+(13+33+53)+…+(13+33+…+(2n-1)3)
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
asum=gsum=0 asum=gsum=0
FAIPS, DPS Kuwait Page 20 / 32
Python Notes Class XI Loop
k=1 k=1
while k<=2*n-1: while k<=2*n-1:
asum+=k*k asum+=k*k*k
gsum+=asum gsum+=asum
k+=2 k+=2
print('Sum =',gsum) print('Sum =',gsum)
print('Using "for" loop') print('Using "for" loop')
asum=gsum=0 asum=gsum=0
for x in range(1, 2*n, 2): for x in range(1, 2*n, 2):
asum+=x**2 asum+=x**3
gsum+=asum gsum+=asum
print('Sum =',gsum) print('Sum =',gsum)
OR, OR,
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
asum=gsum=0 asum=gsum=0
k=1 k=1
while k<=n: while k<=n:
t=2*k-1 t=2*k
asum+=t*t asum+=t*t*t
gsum+=asum gsum+=asum
k+=1 k+=1
print('Sum =',gsum) print('Sum =',gsum)
print('Using "for" loop') print('Using "for" loop')
asum=gsum=0 asum=gsum=0
for x in range(1, n+1): for x in range(1, n+1):
t=2*x-1 t=2*x
asum+=t**2 asum+=t**3
gsum+=asum gsum+=asum
print('Sum =',gsum) print('Sum =',gsum)
Write a Python script to input an integer n and find Write a Python Script to input a floating point value
the product of the sequence: 1*2*3*4*…*(n-1)*n x and an integer n; calculate and display xn.
n=int(input('Input n? ')) x=float(input('Input x? '))
print('Using "while" loop') n=int(input('Input n? '))
k=fact=1 print('Using "while" loop')
while k<=n: k=power=1
print(k, end=' ') while k<=n:
fact*=k power*=x
k+=1 k+=1
print('\nFactorial =',fact) print(x,'**',n,'=',power,sep='')
print('Using "for" loop') print('-'*20)
fact=1 print('Using "for" loop')
for k in range(1, n+1): power=1
print(k, end=' ') for k in range(1, n+1):
fact*=k power*=x
print('\nFactorial =',fact) print(x,'**',n,'=',power,sep='')
Running of the script produces following output Running of the script produces following output
Input n? 5 Input x? 2
Using "while" loop Input n? 5
1 2 3 4 5 Using "while" loop
Factorial = 120 2**5=32
Using "for" loop --------------------
1 2 3 4 5 Using "for" loop
Factorial = 120 2**5=32
FAIPS, DPS Kuwait Page 21 / 32
Python Notes Class XI Loop
Working of the loop: Working of the loop:
k fact k x power
1 1 1 2 2 x
2 2 2 2 4 x2
3 6 3 2 8 x3
4 24 4 2 16 x4
5 120 5 2 32 x5
Write a Python script to input an integer n and find Write a Python Script to input a floating point value
the sum of the sequence: 1!+2!+3!+…+n! x and an integer n; find the sum of the sequence:
#(1)+(1*2)+(1*2*3)+…+(1*2*3*…+*n) 1+x+x2+x3+x4+…+xn-1+xn
n=int(input('Input n? ')) x=float(input('Input x? '))
print('Using "while" loop') n=int(input('Input n? '))
k=fact=1 print('Using "while" loop')
asum=0 k=power=asum=1
while k<=n: while k<=n:
fact*=k power*=x
asum+=fact asum+=power
k+=1 k+=1
print('Sum =',asum) print('Sum =',asum)
print('Using "for" loop') print('Using "for" loop')
fact, asum=1, 0 power=asum=1
for k in range(1, n+1): for k in range(1, n+1):
fact*=k power*=x
asum+=fact asum+=power
print('Sum =',asum) print('Sum =',asum)
Running of the script produces following output Running of the script produces following output
Input n? 5 Input x? 2
Using "while" loop Input n? 4
Sum = 153 Using "while" loop
Using "for" loop Sum = 31
Sum = 153 Using "for" loop
Working of the loop: Sum = 31
k fact asum Working of the loop:
1 1 1 k x power asum
2 2 3 1 2 2 3
3 6 9 2 2 4 7
4 24 33 3 2 8 15
5 120 153 4 2 16 31
Write a Python script to input an integer n and find Write a Python script to input an integer n and find
the sum of the sequence: 2!+4!+6!+…+(2n)! the sum of the sequence: 1!+3!+5!+…+(2n-1)!
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k, fact, asum=2, 1, 0 k, fact, asum=1, 1, 0
while k<=2*n: while k<=2*n-1:
fact*=(k-1)*k if k>1: fact*=(k-1)*k
asum+=fact asum+=fact
k+=2 k+=2
print('Sum =',asum) print('Sum =',asum)
print('Using "for" loop') print('Using "for" loop')
fact, asum=1, 0 fact, asum=1, 0
for k in range(2, 2*n+1, 2): for k in range(1, 2*n+1, 2):
fact*=(k-1)*k if k>1: fact*=(k-1)*k
asum+=fact asum+=fact
print('Sum =',asum) print('Sum =',asum)
FAIPS, DPS Kuwait Page 22 / 32
Python Notes Class XI Loop
OR, OR,
from math import factorial from math import factorial
n=int(input('Input n? ')) n=int(input('Input n? '))
asum=0 asum=0
for k in range(2, 2*n+1, 2): for k in range(1, 2*n, 2):
asum+=factoral(k) asum+=factoral(k)
print('Sum =',asum) print('Sum =',asum)
OR, OR,
from math import factorial from math import factorial
n=int(input('Input n? ')) n=int(input('Input n? '))
asum=0 asum=0
for k in range(1, n+1): for k in range(1, n+1):
asum+=factoral(2*k) asum+=factoral(2*k-1)
print('Sum =',asum) print('Sum =',asum)
Write a Python Script to input a floating point value Write a Python Script to input a floating point value
x and an integer n; find the sum of the sequence: x and an integer n; find the sum of the sequence:
1+x +x +x +x +…+x +x
2 4 6 8 2n-2 2n 1+x+x3+x5+x7+…+x2n-3+x2n-1
x=float(input('Input x? ')) x=float(input('Input x? '))
n=int(input('Input n? ')) n=int(input('Input n? '))
print('Using "while" loop') print('Using "while" loop')
k=power=asum=1 k, power, asum=1, 1/x, 1
while k<=n: while k<=n:
power*=x*x power*=x*x
asum+=power asum+=power
k+=1 k+=1
print('Sum =',asum) print('Sum =',asum)
print('Using "for" loop') print('Using "for" loop')
power=asum=1 power, asum=1, 1/x
for k in range(1, n+1): for k in range(1, n+1):
power*=x*x power*=x*x
asum+=power asum+=power
print('Sum =',asum) print('Sum =',asum)
OR, OR,
x=float(input('Input x? ')) x=float(input('Input x? '))
n=int(input('Input n? ')) n=int(input('Input n? '))
asum=1 asum=1
for k in range(1, n+1): for k in range(1, n+1):
asum+=x**(2*k) asum+=x**(2*k-1)
print('Sum =',asum) print('Sum =',asum)
Write a Python Script to input a floating point value Write a Python Script to input a floating point value
x and an integer n; find the sum of the sequence: x and an integer n; find the sum of the sequence:
𝑥 𝑥2 𝑥3 𝑥𝑛 𝑥 𝑥2 𝑥3 𝑥𝑛
1 + + + + ⋯+ 1 − + − + ⋯ + (−1)𝑛
1! 2! 3! 𝑛! 1! 2! 3! 𝑛!
x=float(input('Input x? ')) x=float(input('Input x? '))
n=int(input('Input n? ')) n=int(input('Input n? '))
power=fact=asum=1 power=fact=asum=1
for k in range(1, n+1): for k in range(1, n+1):
power*=x power*=-x
fact*=k fact*=k
asum+=power/fact asum+=power/fact
print('Sum =',asum) print('Sum =',asum)
OR, OR,
x=float(input('Input x? ')) x=float(input('Input x? '))
n=int(input('Input n? ')) n=int(input('Input n? '))
FAIPS, DPS Kuwait Page 23 / 32
Python Notes Class XI Loop
term=asum=1 term=asum=1
for k in range(1, n+1): for k in range(1, n+1):
term*=x/k term*=-x/k
asum+=term asum+=term
print('Sum =',asum) print('Sum =',asum)
Write a Python Script to input a floating point value Write a Python Script to input a floating point value
x and an integer n; find the sum of the sequence: x and an integer n; find the sum of the sequence:
𝑥2 𝑥4 𝑥6 𝑥 2𝑛 𝑥2 𝑥4 𝑥6 𝑥 2𝑛
1+ + + +⋯+ 1 − + − + ⋯ + (−1)𝑛
2! 4! 6! (2𝑛)! 2! 4! 6! (2𝑛)!
x=float(input('Input x? ')) x=float(input('Input x? '))
n=int(input('Input n? ')) n=int(input('Input n? '))
asum=fact=power=1 power=fact=asum=1
for k in range(1, n+1): for k in range(1, n+1):
power*=x*x power*=-x*x
fact*=(2*k-1)*2*k fact*=(2*k-1)*2*k
asum+=power/fact asum+=power/fact
print('Sum =',asum) print('Sum =',asum)
OR, OR,
from math import factorial from math import factorial
x=int(input('Input x? ')) x=int(input('Input x? '))
n=int(input('Input n? ')) n=int(input('Input n? '))
asum=1 asum=1
for k in range(1, n+1): for k in range(1, n+1):
deno=factorial(2*k) deno=factorial(2*k)
asum+=x**(2*k)/deno asum+=(-1)**k*x**(2*k)/deno
print('Sum =',asum) print('Sum =',asum)
Write a Python Script to input a floating point value Write a Python Script to input a floating point value
x and an integer n; find the sum of the sequence: x and an integer n; find the sum of the sequence:
3 5 2𝑛−1
𝑥 𝑥 𝑥 𝑥 𝑥 𝑥3 𝑥5 𝑥 2𝑛−1
1 + + + + ⋯+ 1 − + − + ⋯ + (−1)𝑛
1! 3! 5! (2𝑛 − 1)! 1! 3! 5! (2𝑛 − 1)!
x=float(input('Input x? ')) x=float(input('Input x? '))
n=int(input('Input n? ')) n=int(input('Input n? '))
power=1/x power=1/x
fact=asum=1 fact=asum=1
for k in range(1, n+1): for k in range(1, n+1):
power*=x*x power*=-x*x
if k>1: fact*=(2*k-1)*(2*k-2) if k>1: fact*=(2*k-1)*2*k
asum+=power/fact asum+=power/fact
print('Sum =',asum) print('Sum =',asum)
OR, OR,
from math import factorial from math import factorial
x=float(input('Input x? ')) x=float(input('Input x? '))
n=int(input('Input n? ')) n=int(input('Input n? '))
asum=1 asum=1
for k in range(1, n+1): for k in range(1, n+1):
power=x**(2*k-1) power=x**(2*k-1)
fact=factorial(2*k-1) fact=factorial(2*k-1)
asum+=power/fact sign=(-1)**n
print('Sum =',asum) asum+=sign*power/deno
print('Sum =',asum)
#count number of digits #sum of digits
n=int(input('Input n? ')) n=int(input('Input n? '))
d=0 s=0
FAIPS, DPS Kuwait Page 24 / 32
Python Notes Class XI Loop
while n>0: while n>0:
d+=1 s+=n%10
n//=10 n//=10
print('Digits =',d) print('Sum =',s)
#sum of square of digits #sum of cube of digits
n=int(input('Input n? ')) n=int(input('Input n? '))
s=0 s=0
while n>0: while n>0:
d=n%10 d=n%10
s+=d*d s+=d*d*d
n//=10 n//=10
print('Sum =',s) print('Sum =',s)
#products of digits #products of digits ignore 0
n=int(input('Input n? ')) n=int(input('Input n? '))
p=1 p=1
while n>0: while n>0:
d=n%10 d=n%10
p*=d if d>0: p*=d
n//=10 n//=10
print('Product =',p) print('Product =',p)
#sum of reciprocal of digits #reversing digits
n=int(input('Input n? ')) n=int(input('Input n? '))
s=0 m=0
while n>0: while n>0:
d=n%10 d=n%10
if d>0: m=10*m+d
s+=1/d n//=10
n//=10 print('Reversed =',m)
print('Sum =',s)
#check for Armstrong Number #check for Palindrome Number
n=int(input('Input n? ')) n=int(input('Input n? '))
t, s, power=n, 0, len(str(n)) t, m=n, 0
while n>0: while n>0:
d=n%10 d=n%10
s+=d**power m=10*m+d
n//=10 n//=10
if s==t: if m==t:
print('Armstrong Number') print(' Palindrome Number')
else: else:
print('Not Armstrong Number') print('Not Palindrome Number’)
#HCF of two numbers #LCM of two numbers
a=int(input('Input a? ')) a=int(input('Input a? '))
b=int(input('Input b? ')) b=int(input('Input b? '))
while True: p=a*b
r=a%b while True:
if r==0: r=a%b
break if r==0: break
a=b a=b
b=r b=r
print('HFC =',b) print('LCM =',p//b)
#check for Prime Number #check for Fibonacci numbers
n=int(input('Input n? ')) n=int(input('Input n? '))
prime=True f1, f2=0, 1
for k in range(2, n//2): while True:
FAIPS, DPS Kuwait Page 25 / 32
Python Notes Class XI Loop
if n%k==0: if f1+f2>n:
prime=False break
break f1, f2=f2, f1+f2
if prime: if f1==0 or f2==n:
print('Prime') print('Fibonacci Number')
else: else:
print('Composite') print('Not Fibonacci Number')
#First n Fibonacci numbers #check for Perfect Number
n=int(input('Input n? ')) n=int(input('Input n? '))
f1=0 s=0
f2=1 for k in range(1, n):
for k in range(n): if n%k==0: s+=k
print(f1) if s==n:
#print(f2) print('Perfect Number')
f1, f2=f2, f1+f2 else:
print('Not Perfect Number')

else with loop


With a while loop and with for loop we have an else part and else part with loop is executed when a loop
terminates normally.
while loop with else statement: for loop with else statement:
while Condition: for var in range(…):
Block Block
else: else:
Block2/Statement2 Block2/Statement2
k=1 for k in range(1,7):
while k<=4: print(k)
print(k) else:
k+=1 print('End of for loop')
else:
print('End of while loop') Running of the script produces following output:
1
Running of the script produces following output: 2
1 3
2 4
3 5
4 6
End of while loop End of for loop

The loop comes to an end when k is 5. When loop The loop comes to an end when k is 6. When loop
ends, else statement is executed. ends, else statement is executed.

k=1 for k in range(1,11):


while k<=10: print(k)
print(k) if k==6:
if k==4: break
break else:
k+=1 print('End of while loop')
else:
print('End of while loop') Running of the script produces following output:
1
Running of the script produces following output: 2
1 3

FAIPS, DPS Kuwait Page 26 / 32


Python Notes Class XI Loop
2 4
3 5
4 6

The loop is terminated abruptly using break, hence The loop is terminated abruptly using break, hence
else statement does not get executed. else statement does not get executed.

Nested loop
Till now we have learned how to use single while / for loop. The role of a loop is too repeat a block (group
of Python statements) or a single Python statement for a specific number of times. A block inside a loop
usually contained either a print() function or a assignment statement or a if statement or a combination of
print(), assignment and if statements. But now we will learn that a block or statement inside a loop may
contain another loop – this is known as nested loop (one loop contains at least one more loop). General
rule for nested loop is given below:
Nested while loop: Nested for loop:
while Condition1: Outer for var1 in range(…):
Statement1 Loop Statement1
Statement2 Statement2
: :
while Condition2: for var2 in range(…):
Block Block
Statement3 Inner Statement3
: Loop :
Write a Python script to draw a grid by inputting number of rows, number of columns and the character
to fill the grid.

#using nested while loop #using nested for loop


nor=int(input('Rows? ')) nor=int(input('Rows? '))
noc=int(input('Cols? ')) noc=int(input('Cols? '))
ch=input('Fill Char? ') ch=input('Fill Char? ')
k=1 for k in range(1, nor+1):
while k<=nor: for j in range(1, noc+1):
j=1 print(ch, end='')
while j<=noc: print()
print(ch, end='')
j+=1 Running of the script produces following output:
print() Rows? 9
k+=1 Cols? 7
Fill Char? @
Running of the script produces following output: @@@@@@@
Rows? 5 @@@@@@@
Cols? 7 @@@@@@@
Fill Char? # @@@@@@@
####### @@@@@@@
####### @@@@@@@
####### @@@@@@@
@@@@@@@
When k is 1, outer while loop’s condition is true, @@@@@@@
outer while loop is executed. When outer while @@@@@@@
loop is executed, inner while loop is executed noc
number of times. When inner while loop is Outer for loop is executed nor number of times.
executed, # is printed noc number of times. After Every time outer for loop is executed, inner for
the end of inner while loop, print() shifts the cursor loop is also executed. When inner for loop is

FAIPS, DPS Kuwait Page 27 / 32


Python Notes Class XI Loop
to beginning of the next line. Outer while loop’s executed, @ is displayed noc number of times.
control variable k is updated. Outer while loop’s Row of @ is displayed by the inner for loop. At the
condition is tested, if the condition is true, then end of inner for loop, print() shifts the cursor to the
next row is displayed. If the condition is false, beginning of the next line. Outer for loop controls
outer while loop terminates and the grid is the number of rows to be displayed. When outer
complete. for loop terminates and the grid is complete.

Write a Python script to draw a triangle by inputting number of rows.


#using nested while loop #using nested for loop
n=int(input('Rows? ')) n=int(input('Rows? '))
k=1 for k in range(1, n+1):
ch='$' for j in range(1, k+1):
while k<=n: print('*', end='')
j=1 print()
while j<=k:
print(ch, end='') Running of the script produces following output:
j+=1 Rows? 5
k+=1 *
print() **
***
Running of the script produces following output: ****
Rows? 3 *****
$ Outer for loop controls number of rows (n rows) in
$$ the triangle. Number of times inner for loop is
$$$ executed, depends on the value stored in the
$$$$ control variable of the outer loop. When k is 1,
$$$$$ inner loop is executed once, one * is displayed in
the first row. When k is 2, inner loop is executed
Outer while loop controls number of rows (n rows) twice, two * is displayed in the second row. When
in the triangle. Number of times inner while loop k is 3, inner loop is executed three times, three * is
is executed, depends on the value stored in the displayed in the third row. When k is 4, inner loop
control variable of the outer loop. When k is 1, is executed four times, four * is displayed in the
inner loop is executed once, one $ is displayed in fourth row. When k is 5, inner loop is executed five
the first row. When k is 2, inner loop is executed times, five * is displayed in the fifth row.
twice, two $ is displayed in the second row. When
k is 3, inner loop is executed three times, three $ is
displayed in the third row. When k is 4, inner loop
is executed four times, four $ is displayed in the
fourth row. When k is 5, inner loop is executed five
times, five $ is displayed in the fifth row.

Write a Python script to input an integer (say n) and find the sum of the following series using nested
loops:
a) (1)+(1+2)+(1+2+3)+ … +(1+2+3+…+n)
b) (2)+(2+4)+(2+4+6)+ … +(2+4+6+…+2n)
c) (1)+(1+3)+(1+3+5)+ … +(1+3+5+…+2n-1)
a) (1)+(1+2)+(1+2+3)+ … +(1+2+3+…+n) a) (1)+(1+2)+(1+2+3)+ … +(1+2+3+…+n)
#using nested while loop #using nested for loop
n=int(input('Input n? ')) n=int(input('Input n? '))
k, sg=1, 0 sg=0
while k<=n: for k in range(1, n+1):
j, s=1, 0 s=0
while j<=k: for j in range(1, k+1):

FAIPS, DPS Kuwait Page 28 / 32


Python Notes Class XI Loop
s+=j s+=j
j+=1 sg+=s
sg+=s print('Sum =',sg)
k+=1
print('Sum =',sg)
We will use two accumulators. The outer loop will be used to find the sum of the groups and the inner
loop will be used to find the sum within the group. Every time inner loop gets executed, sum within the
group will be calculated from the beginning for every group.
b) (2)+(2+4)+(2+4+6)+ … +(2+4+6+…+2n) b) (2)+(2+4)+(2+4+6)+ … +(2+4+6+…+2n)
#using nested while loop #using nested for loop
n=int(input('Input n? ')) n=int(input('Input n? '))
k, sg=2, 0 sg=0
while k<=2*n: for k in range(2,2*n+1,2):
j, s=2, 0 s=0
while j<=2*k: for j in range(2,2*k+1,2):
s+=j s+=j
j+=2 sg+=s
sg+=s print('Sum =',sg)
k+=2
print('Sum =',sg)
c) (1)+(1+3)+(1+3+5)+ … +(1+3+5+…+2n-1) c) (1)+(1+3)+(1+3+5)+ … +(1+3+5+…+2n-1)
#using nested while loop #using nested for loop
n=int(input('Input n? ')) n=int(input('Input n? '))
k, sg=1, 0 sg=0
while k<=2*n-1: for k in range(1,2*n,2):
j, s=1, 0 s=0
while j<=2*k-1: for j in range(1,2*k,2):
s+=j s+=j
j+=2 sg+=s
sg+=s print('Sum =',sg)
k+=2
print('Sum =',sg)

Write a Python script to input an integer (say n) and find the sum of the following series using nested
loops:
a) 1!+2!+3!+4!+…+n!
b) (2)+(2*4)+(2*4*6)+ … +(2*4*6*…*2n)
c) (1)+(1*3)+(1*3*5)+ … +(1*3*5*…*2n-1)
d) 2!+4!+6!+…+(2n)!
e) 1!+3!+5!+…+(2n-1)!
a) 1!+2!+3!+4!+…+n! a) 1!+2!+3!+4!+…+n!
#using nested while loop #using nested for loop
n=int(input('Input n? ')) n=int(input('Input n? '))
k, s=1, 0 s=0
while k<=n: for k in range(1, n+1):
j, f=1, 1 f=1
while j<=k: for j in range(1, k+1):
f*=j f*=j
j+=1 s+=f
s+=f print('Sum =',s)
k+=1
print('Sum =',s)

FAIPS, DPS Kuwait Page 29 / 32


Python Notes Class XI Loop
a) 1!+2!+3!+4!+…+n! a) 1!+2!+3!+4!+…+n!
#Outer loop for inner loop while #Outer loop while inner loop for
n=int(input('Input n? ')) n=int(input('Input n? '))
s=1 k, s=1, 0
for k in range(1, n+1): while k<=n:
j, f=1, 1 f=1
while j<=k: for j in range(1, k+1):
f*=j f*=j
j+=1 s+=f
s+=f k+=1
print('Sum =',s) print('Sum =',s)
b) (2)+(2*4)+(2*4*6)+ … +(2*4*6*…*2n) b) (2)+(2*4)+(2*4*6)+ … +(2*4*6*…*2n)
#using nested while loop #using nested for loop
n=int(input('Input n? ')) n=int(input('Input n? '))
k, s=1, 0 s=0
while k<=n: for k in range(1, n+1):
j, f=2, 1 f=1
while j<=2*k: for j in range(2, 2*k+1, 2):
f*=j f*=j
j+=2 s+=f
s+=f; k+=1 print('Sum =',s)
print('Sum =',s)
b) (2)+(2*4)+(2*4*6)+ … +(2*4*6*…*2n) b) (2)+(2*4)+(2*4*6)+ … +(2*4*6*…*2n)
#Outer loop for inner loop while #Outer loop while inner loop for
n=int(input('Input n? ')) n=int(input('Input n? '))
s=0 k, s=0, 1
for k in range(1, n+1): while k<=n:
j, f=2, 1 f=1
while j<=2*k: for j in range(2, 2*k+1, 2):
f*=j f*=j
j+=2 s+=f
s+=f k+=1
print('Sum =',s) print('Sum =',s)
c) (1)+(1*3)+(1*3*5)+ … +(1*3*5*…*2n-1) c) (1)+(1*3)+(1*3*5)+ … +(1*3*5*…*2n-1)
#using nested while loop #using nested for loop
n=int(input('Input n? ')) n=int(input('Input n? '))
k, s=1, 0 s=0
while k<=n: for k in range(1, n+1):
j, f=1, 1 f=1
while j<=2*k-1: for j in range(1, 2*k, 2):
f*=j; j+=2 f*=j
s+=f; k+=1 s+=f
print('Sum =',s) print('Sum =',sg)
c) (1)+(1*3)+(1*3*5)+ … +(1*3*5*…*2n-1) c) (2)+(2*4)+(2*4*6)+ … +(2*4*6*…*2n)
#Outer loop for inner loop while #Outer loop while inner loop for
n=int(input('Input n? ')) n=int(input('Input n? '))
s=0 k, s=0, 1
for k in range(1, n+1): while k<=n:
j, f=1, 1 f=1
while j<=2*k-1: for j in range(1, 2*k, 2):
f*=j f*=j
j+=2 s+=f
s+=f k+=1
print('Sum =',s) print('Sum =',s)
FAIPS, DPS Kuwait Page 30 / 32
Python Notes Class XI Loop
d) 2!+4!+6!+…+(2n)! d) 2!+4!+6!+…+(2n)!
#Using nested while loop #using nested for loop
n=int(input('Input n? ')) n=int(input('Input n? '))
k, s=2, 0 s=0
while k<=2*n: for k in range(2, 2*n+1, 2):
j, f=1, 1 f=1
while j<=k: for j in range(1,k+1):
f*=j f*=j
j+=1 s+=f
s+=f print('Sum =',s)
k+=2
print('Sum =',s)
d) 2!+4!+6!+…+(2n)! d) 2!+4!+6!+…+(2n)!
#Outer loop for inner loop while #Outer loop while inner loop for
n=int(input('Input n? ')) n=int(input('Input n? '))
s=0 k, s=0, 2
for k in range(2, 2*n+1, 2): while k<=2*n:
j, f=1, 1 f=1
while j<=k: for j in range(1, k+1):
f*=j f*=j
j+=1 s+=f
s+=f k+=2
print('Sum =',s) print('Sum =',s)
e) 1!+3!+5!+…+(2n-1)! e) 1!+3!+5!+…+(2n-1)!
#Using nested while loop #using nested for loop
n=int(input('Input n? ')) n=int(input('Input n? '))
k, s=1, 0 s=0
while k<=2*n-1: for k in range(1, 2*n, 2):
j, f=1, 1 f=1
while j<=k: for j in range(1,k+1):
f*=j f*=j
j+=1 s+=f
s+=f print('Sum =',s)
k+=2
print('Sum =',s)
e) 1!+3!+5!+…+(2n-1)! e) 1!+3!+5!+…+(2n-1)!
#Outer loop for inner loop while #Outer loop while inner loop for
n=int(input('Input n? ')) n=int(input('Input n? '))
s=0 k, s=0, 1
for k in range(1, 2*n, 2): while k<=2*n-1:
j, f=2, 1 f=1
while j<=k: for j in range(1, k+1):
f*=j f*=j
j+=1 s+=f
s+=f k+=2
print('Sum =',s) print('Sum =',s)

Write a menu driven Python script to do the following:


• Input an integer and check Sum-Product number
• Input an integer and check Factorian number
• Input an integer and check Harshad number
• Input an integer and check Happy number
• Exit from the menu

FAIPS, DPS Kuwait Page 31 / 32


Python Notes Class XI Loop
while True:
print('1. Check Sum-Product Number')
print('2. Check Harshad Number')
print('3. Check Factorian Number')
print('4. Check Happy Number')
print('0. Exit')
ch=input('Choice[0-4]? ')
if ch=='0': break
elif ch=='1':
t=n=int(input('Input an Integer? '))
s,p=0,1
while t:
d=t%10
s+=d
p*=d
t//=10
if n==s*p:
print(n, 'Sum-Product Number')
else:
print(n, 'Not Sum-Product Number')
elif ch=='2':
n=int(input('Input an Integer? '))
t, s=n, 0
while t:
s+=t%10
t//=10
if n%s==0:
print(n, 'Harshad Number')
else:
print(n, 'Not Harshad Number')
elif ch=='3':
t=n=int(input('Input an Integer? '))
s=0
while t:
f=1
for k in range(1, t%10+1): f*=k
s+=f
t//=10
if n==s:
print(n, 'Factorion Number')
else:
print(n, 'Not Factorion Number')
elif ch=='4':
t=n=int(input('Input an Integer? '))
while t!=1 and t!=4:
s=0
while t:
s+=(t%10)**2
t//=10
t=s
if t==1:
print(n, 'Happy Number')
else:
print(n, 'Not Happy Number')
FAIPS, DPS Kuwait Page 32 / 32

You might also like