10 Python Exercises to Test Your Programming Skills

10 Python Exercises

Here are 10 Python medium difficulty level code exercises with examples and explanation:

1. Write a program that calculates and prints the Fibonacci sequence up to the 100th term.

def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)

for i in range(100):
print(fibonacci(i))

2. Write a program that asks the user for a number and then prints out all the prime numbers less than that number.

def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True

number = int(input("Enter a number: "))
for i in range(2, number):
if is_prime(i):
print(i)

3. Write a program that asks the user for a word and then prints out all the anagrams of that word.

 def is_anagram(word1, word2):
if len(word1) != len(word2):
return False
sorted_word1 = sorted(word1)
sorted_word2 = sorted(word2)
return sorted_word1 == sorted_word2

word = input("Enter a word: ")
anagrams = []
for i in range(len(word)):
for j in range(i + 1, len(word) + 1):
new_word = word[i:j]
if new_word not in anagrams and is_anagram(new_word, word):
anagrams.append(new_word)

print(anagrams)

4. Write a program that asks the user for a list of numbers and then prints out the maximum and minimum numbers in the list.

def get_max_min(numbers):
max_number = numbers[0]
min_number = numbers[0]
for number in numbers:
if number > max_number:
max_number = number
elif number < min_number:
min_number = number
return max_number, min_number

numbers = input("Enter a list of numbers separated by spaces: ").split()
max_number, min_number = get_max_min(numbers)
print("The maximum number is", max_number)
print("The minimum number is", min_number)

5. Write a program that asks the user for a string and then prints out the number of vowels in the string.

def count_vowels(string):
vowels = "aeiouAEIOU"
count = 0
for character in string:
if character in vowels:
count += 1
return count

string = input("Enter a string: ")
number_of_vowels = count_vowels(string)
print("The number of vowels in the string is", number_of_vowels)

6. Write a program that asks the user for a string and then prints out the longest substring that doesn’t contain any repeated characters.

def longest_substring_without_repeating_characters(string):
longest_substring = ""
current_substring = ""
for character in string:
if character not in current_substring:
current_substring += character
else:
if len(current_substring) > len(longest_substring):
longest_substring = current_substring
current_substring = character
return longest_substring

string = input("Enter a string: ")
longest_substring = longest_substring_without_repeating_characters(string)
print("The longest substring without repeating characters is", longest_substring)

7. Write a program that asks the user for a list of words and then prints out the longest palindrome in the list.

def is_palindrome(word):
reversed_word = word[::-1]
return word == reversed_word

def longest_palindrome(words):
longest_palindrome = ""
for word in words:
if len(word) > len(longest_palindrome) and is_palindrome(word):
longest_palindrome = word
return longest_palindrome

words = input("Enter a list of words separated by spaces: ").split()
longest_palindrome = longest_palindrome(words)
print("The longest palindrome in the list is", longest_palindrome)

8. Write a program that asks the user for a number and then prints out the factorial of that number.

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

number = int(input("Enter a number: "))
factorial_of_number = factorial(number)
print("The factorial of the number is", factorial_of_number)

9. Write a program that asks the user for a number and then prints out the fibonacci sequence up to that number.

def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)

number = int(input("Enter a number: "))
for i in range(number):
print(fibonacci(i))

10. Write a program that asks the user for a number and then prints out all the perfect squares less than that number.

ef is_perfect_square(n):
for i in range(1, n):
if i * i == n:
return True
return False
number = int(input("Enter a number: "))for i in range(1, number):
if is_perfect_square(i):
print(i)

These are just a few examples of medium difficulty level Python code exercises. There are many other exercises that you can find online or in books. The best way to improve your Python skills is to practice solving these exercises.

Benefits of learning intermediate level Python code

Learning intermediate level Python code can help you to:

  • Write more complex and sophisticated programs.
  • Use more advanced Python features.
  • Solve more challenging problems.
  • Build more impressive projects.
  • Become more marketable to employers.
  • Have more fun programming.

Intermediate level Python code requires a deeper understanding of the language and its features. This can be challenging, but it is also rewarding. By learning intermediate level Python code, you will be able to build more powerful and complex applications. You will also be able to solve more challenging problems and work on more interesting projects.

In addition to the technical benefits, learning intermediate level Python code can also be a lot of fun. The challenge of solving complex problems and the satisfaction of seeing your code work can be very rewarding. If you are interested in programming and want to take your skills to the next level, then learning intermediate level Python code is a great way to do it.

3 thoughts on “10 Python Exercises to Test Your Programming Skills”

Leave a Comment