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

Python Nested Loops Assignment

The document outlines a Python assignment focused on nested loops with ten problems. These problems include generating multiplication tables, adding 2D lists, counting even and odd numbers, finding prime numbers, transposing matrices, generating unique pair combinations, identifying common characters in strings, counting vowels in words, finding common elements in lists, and summing rows in a 2D list. Each problem is accompanied by example code demonstrating the solution.

Uploaded by

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

Python Nested Loops Assignment

The document outlines a Python assignment focused on nested loops with ten problems. These problems include generating multiplication tables, adding 2D lists, counting even and odd numbers, finding prime numbers, transposing matrices, generating unique pair combinations, identifying common characters in strings, counting vowels in words, finding common elements in lists, and summing rows in a 2D list. Each problem is accompanied by example code demonstrating the solution.

Uploaded by

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

Python Assignment: Nested Loops

Problems (Date: 06/11/25)

1. Multiplication Tables (1 to 10)


for i in range(1, 11):
for j in range(1, 11):
print(i, "x", j, "=", i*j)
print()

2. Add Two 2D Lists


A = [[1,2],[3,4]]
B = [[5,6],[7,8]]
result = [[0,0],[0,0]]

for i in range(len(A)):
for j in range(len(A[0])):
result[i][j] = A[i][j] + B[i][j]

print(result)

3. Count Even and Odd in Nested List


lst = [[1,2,3],[4,5,6]]
even = odd = 0

for row in lst:


for num in row:
if num % 2 == 0:
even += 1
else:
odd += 1

print("Even:", even, "Odd:", odd)

4. Prime Numbers (2 to 50)


for num in range(2, 51):
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num)

5. Transpose of Matrix
matrix = [[1,2,3],[4,5,6]]
transpose = [[0,0],[0,0],[0,0]]

for i in range(len(matrix)):
for j in range(len(matrix[0])):
transpose[j][i] = matrix[i][j]

print(transpose)

6. Unique Pair Combinations


lst = [1,2,3,4]

for i in range(len(lst)):
for j in range(i+1, len(lst)):
print((lst[i], lst[j]))

7. Common Characters in Two Strings


s1 = "hello"
s2 = "world"

for ch1 in s1:


for ch2 in s2:
if ch1 == ch2:
print(ch1)

8. Count Vowels in Each Word


words = ["apple", "banana", "cherry"]
vowels = "aeiou"

for word in words:


count = 0
for ch in word:
if ch in vowels:
count += 1
print(word, ":", count)

9. Common Elements Between Two Lists


list1 = [1,2,3,4]
list2 = [3,4,5,6]

for i in list1:
for j in list2:
if i == j:
print(i)

10. Sum of Each Row in 2D List


matrix = [[1,2,3],[4,5,6]]

for row in matrix:


total = 0
for num in row:
total += num
print(total)

You might also like