0% found this document useful (0 votes)
26 views6 pages

FP Ta 04

The document is a tutorial on for loops and nested loops in programming, explaining their structure and providing examples. It includes various code snippets demonstrating the use of loops for tasks such as printing numbers, calculating sums, and identifying prime numbers. Additionally, it addresses common coding errors and provides corrected versions of the code.

Uploaded by

yalda.hajilito
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)
26 views6 pages

FP Ta 04

The document is a tutorial on for loops and nested loops in programming, explaining their structure and providing examples. It includes various code snippets demonstrating the use of loops for tasks such as printing numbers, calculating sums, and identifying prime numbers. Additionally, it addresses common coding errors and provides corrected versions of the code.

Uploaded by

yalda.hajilito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

TA_S04

December 6, 2024

1 TA Session 4
1.1 For Loops and Nested Loops
prepared by Mohsen Mirzaei
A for loop is used for executing a block of code a predetermined number of times for specific values.
Nested Loops are two or more loops written inside of each other.
for i in range(a, b, c):
The range function in the above example generates integers from a to b (including a but excluding
b) with an increment value of c.

[1]: for i in range(0, 10):


print(i)

# i = 10 # doesnt change anything since at the start pf every loop the␣


↪value of i is reassigned

# print(i) # prints the last value that i had in the loop

0
1
2
3
4
5
6
7
8
9

[2]: for i in range(0, 10, 2):


print(i)

0
2
4

1
6
8

[17]: # what does this code do?

num = int(input())
a = int(input())
b = int(input())

for i in range(a, b + 1, num):


print(i)

3
2
10
2
5
8

[4]: for i in range(1, 5):


for j in range(1, 5):
print(str(i), "*", str(j), "=", i * j)

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16

[5]: name = "mohzen"

for character in name:


print(character)

m
o

2
h
z
e
n

[6]: # for a given integer n, print the sum of integers 1 to j, for every 1 <= j <=␣
↪n.

# Is the code below correct? if not, whats the problem?

n = int(input())

s = 0
for i in range(1, n + 1):
for j in range(1, i + 1):
s += j

print(s)

5
1
4
10
20
35

[7]: # corrected code:

n = int(input())

for i in range(1, n + 1):


s = 0
for j in range(1, i + 1):
s += j

print(s)

5
1
3
6
10
15
print a triangle using a given string. (WITHOUT MULTIPLYING A STRING BY AN INTEGER)
hint: print(‘a’, end=””)

3
example:
input: abcd
output: a bb ccc dddd
[8]: input_string = input()

character_count = 1

for character in input_string:


for i in range(character_count):
print(character, end='')
print()
character_count += 1

abcd
a
bb
ccc
dddd

[11]: # Find the smallest integer whose sum of digits is n

n = int(input())

is_found = False
current_num = 1

while not is_found:


sum_of_digits = 0

for digit in str(current_num):


sum_of_digits += int(digit)

if sum_of_digits == n:
is_found = True
else:
current_num += 1

print(current_num)

23
599

[2]: # write a program to count the number of prime numbers in a given interval [a,␣
↪b]

4
a = int(input())
b = int(input())

count = 0

for i in range(a, b + 1):


is_prime = True
for j in range(2, i):
if i % j == 0:
is_prime = False

if is_prime and i != 1:
count += 1

print(count)

2
13
6
����� ����� ���� �� ����� �������� ��� ���� ��� ��� �� �� ���� ������ ��� ��� .����
[13]: # write a program to count the number of prime numbers whose sum of digits is␣
↪also prime in a given interval [a, b]

a = int(input())
b = int(input())

count = 0

for i in range(a, b + 1):


is_prime = True
is_sum_of_digits_prime = True

for j in range(2, i):


if i % j == 0:
is_prime = False

sum_of_digits = 0

for j in str(i):
sum_of_digits += int(j)

for j in range(2, sum_of_digits):


if sum_of_digits % j == 0:
is_sum_of_digits_prime = False

5
if is_prime and i != 1:
if is_sum_of_digits_prime and sum_of_digits != 1:
count += 1

print(count)

1
13
5

[14]: # given two integers n and d, find the smallest multiple of n whose largest␣
↪digit is less than or equal to d.

n = int(input())
d = int(input())

current_number = n

while True:
largest_digit = current_number % 10

for digit in str(current_number):


digit = int(digit)
if digit > largest_digit:
largest_digit = digit

if largest_digit <= d:
break

current_number += n

print(current_number)

25
1
100

You might also like