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

Advanced Python Exercises

The document contains advanced Python exercises with solutions, including finding prime numbers in a range, checking if a string is a palindrome, finding a missing number in a list, and generating Floyd's triangle. Each exercise is accompanied by code examples and expected outputs. These exercises are designed to enhance Python programming skills.

Uploaded by

Khäø ULä
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Advanced Python Exercises

The document contains advanced Python exercises with solutions, including finding prime numbers in a range, checking if a string is a palindrome, finding a missing number in a list, and generating Floyd's triangle. Each exercise is accompanied by code examples and expected outputs. These exercises are designed to enhance Python programming skills.

Uploaded by

Khäø ULä
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Advanced Python Exercises with Solutions

1. Find Prime Numbers in a Range


Code:
def find_primes(start, end):
primes = []
for num in range(start, end + 1):
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
break
else:
primes.append(num)
return primes

print(find_primes(10, 50))
Expected Output: [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

2. Check if a String is a Palindrome


Code:
def is_palindrome(s):
return s == s[::-1]

word = "madam"
print(f"Is '{word}' a palindrome? {is_palindrome(word)}")
Expected Output: Is 'madam' a palindrome? True

3. Find Missing Number in a List


Code:
def find_missing_number(lst):
n = len(lst) + 1
expected_sum = n * (n + 1) // 2
actual_sum = sum(lst)
return expected_sum - actual_sum

numbers = [1, 2, 3, 5, 6, 7, 8, 9, 10]


print(f"Missing number: {find_missing_number(numbers)}")
Expected Output: Missing number: 4

4. Generate Floyd's Triangle


Code:
def floyds_triangle(rows):
num = 1
for i in range(1, rows + 1):
for j in range(i):
print(num, end=" ")
num += 1
print()

floyds_triangle(5)
Expected Output: 12 34 5 67 8 9 1011 12 13 14 15

You might also like