Python-Loops
Python-Loops
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.
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
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,
OR, OR,
OR, OR,
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
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.
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,
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,
Running of the script will produce the same output. Running of the script will produce the same output.
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,
OR, OR,
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
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')
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.
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.
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):
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)