0% found this document useful (0 votes)
6 views7 pages

Python Programs

The document contains a series of Python programs demonstrating the use of for and while loops to generate various number series and patterns. Each program is followed by a dry run explanation detailing how the output is derived. The examples cover basic arithmetic, countdowns, cumulative sums, and more complex patterns like Fibonacci-style addition.

Uploaded by

shreeshuisntok
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Python Programs

The document contains a series of Python programs demonstrating the use of for and while loops to generate various number series and patterns. Each program is followed by a dry run explanation detailing how the output is derived. The examples cover basic arithmetic, countdowns, cumulative sums, and more complex patterns like Fibonacci-style addition.

Uploaded by

shreeshuisntok
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Section 1: For Loops (Number Series)

Program 1: Basic Arithmetic


Python
x = 0
for i in range(5):
x = x + 2
print(x, end=" ")
Program 2: The Countdown
Python
for i in range(10, 0, -2):
print(i, end=" ")
Program 3: Powers of Two
Python
n = 1
for i in range(4):
print(n, end=" ")
n = n * 2
Program 4: Cumulative Sum
Python
total = 0
for i in range(1, 5):
total = total + i
print(total, end=" ")
Program 5: Square Series
Python
for i in range(1, 6):
print(i * i, end=" ")
Program 6: The Alternator
Python
val = 10
for i in range(4):
if i % 2 == 0:
val = val + 5
else:
val = val - 2
print(val, end=" ")
Program 7: Decrementing Multiples
Python
m = 50
for i in range(3):
print(m, end=" ")
m = m - 10
Program 8: Nested Counting
Python
for i in range(1, 3):
for j in range(1, 3):
print(i + j, end=" ")
Program 9: Triple Step
Python
for i in range(1, 12, 3):
print(i, end=" ")
Program 10: Simple Factorial Pattern
Python
f = 1
for i in range(1, 5):
f = f * i
print(f, end=" ")
Section 2: While Loops (Number Series)
Program 11: Simple Increment
Python
i = 5
while i <= 25:
print(i, end=" ")
i = i + 5
Program 12: Halving Series
Python
n = 16
while n > 1:
print(n, end=" ")
n = n // 2
Program 13: Conditional Skip
Python
x = 1
while x < 6:
if x != 3:
print(x, end=" ")
x = x + 1
Program 14: Difference Pattern
Python
a = 1
diff = 1
while a < 10:
print(a, end=" ")
a = a + diff
diff = diff + 1
Program 15: Backward Accumulator
Python
s = 20
while s > 10:
print(s, end=" ")
s = s - 3
Program 16: Two Variable Interaction
Python
a = 1
b = 10
while a < b:
print(a, end=" ")
a = a + 2
b = b - 1
Program 17: Multiplier Loop
Python
k = 3
while k < 30:
print(k, end=" ")
k = k * 2
Program 18: Remainder Logic
Python
count = 1
while count < 10:
if count % 3 == 0:
print(count, end=" ")
count = count + 1
Program 19: Fibonacci-style Addition
Python
x = 1
y = 2
while x < 10:
print(x, end=" ")
temp = x + y
x = y
y = temp
Program 20: Floor Division Countdown
Python
num = 100
while num > 5:
print(num, end=" ")
num = num // 2
Answer Key & Dry Run Explanations

No. Output Dry Run Explanation


2 4 6 8 i goes 0-4. x adds 2 each time:
1
10 0+2, 2+2, 4+2, 6+2, 8+2.
10 8 6 4 Starts at 10, stops before 0, steps
2
2 by -2.
n starts at 1. Iterations: 1, 12,
3 1 2 4 8
22, 4*2.
total is sum of i: 1, 1+2, 3+3,
4 1 3 6 10
6+4.
1 4 9 16
5 i is 1,2,3,4,5. Prints i*i.
25
15 13 18 i=0 (+5=15), i=1 (-2=13), i=2
6
16 (+5=18), i=3 (-2=16).
m starts 50, loops 3 times,
7 50 40 30
decreasing by 10 each time.
8 2 3 3 4 Inner: (1+1, 1+2), then (2+1, 2+2).
Starts 1, adds 3 until it reaches
9 1 4 7 10
11.
10 1 2 6 24 f multiplies by i: 11, 12, 23, 64.
5 10 15 i starts at 5, adds 5 until it
11
20 25 exceeds 25.
Integer division by 2: 16//2=8,
12 16 8 4 2
8//2=4, 4//2=2. Next is 1 (stops).
13 1 2 4 5 Prints x unless x is 3.
a adds diff. Iter 1: 1+1=2, Iter 2:
14 1 2 4 7
2+2=4, Iter 3: 4+3=7.
20 17 14 Subtracts 3. Next is 8, which is
15
11 not > 10.
a increases by 2, b decreases by 1.
16 1 3 5 Iter 1: (1,10), Iter 2: (3,9), Iter
3: (5,8).
No. Output Dry Run Explanation
3 6 12 Doubling k starting from 3. Next is
17
24 48 (stops).
Loops 1-9. Prints only numbers
18 3 6 9
divisible by 3.
1 2 3 5 x becomes y, y becomes sum of old x
19
8 and y.
100 50 Floor division by 2: 100, 50, 25,
20
25 12 6 12, 6. Next is 3 (stops).

You might also like