Sharpen your Python skills with our Python Cheat Sheet Challenges. Whether you’re a beginner or looking to refine your expertise, these problems will help you build confidence and master the fundamentals of Python programming.
Easy
1. Challenge: Write a Python program to print “Hello, World!”.
Answer:
print("Hello, World!")
2. Challenge: Write a Python program to find the sum of two numbers.
Answer:
num1 = 10
num2 = 20
sum = num1 + num2
print("The sum is:", sum)
3. Challenge: Write a Python program to check if a number is even or odd.
Answer:
number = int(input("Enter a number: "))
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
4. Challenge: Write a Python program to reverse a string.
Answer:
my_string = "Hello, World!"
reversed_string = my_string[::-1]
print("Reversed string:", reversed_string)
5. Challenge: Write a Python function to find the maximum of three numbers..
Answer:
def find_max(a, b, c):
return max(a, b, c)
print(find_max(3, 6, 2))
Medium
6. Challenge: Write a Python program to count the number of vowels in a given string.
Answer:
def count_vowels(s):
vowels = 'aeiouAEIOU'
count = 0
for char in s:
if char in vowels:
count += 1
return count
print(count_vowels("Hello World"))
7. Challenge: Write a Python program to find the factorial of a number using recursion.
Answer:
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
8. Challenge: Write a Python program to check if a string is a palindrome.
Answer:
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("racecar"))
9. Challenge: Write a Python function to find the GCD (Greatest Common Divisor) of two numbers.
Answer:
def gcd(a, b):
while b:
a, b = b, a % b
return a
print(gcd(48, 18))
10. Challenge: Write a Python program to find the second largest element in a list.
Answer:
def find_second_largest(numbers):
unique_numbers = list(set(numbers))
unique_numbers.sort()
return unique_numbers[-2]
print(find_second_largest([1, 3, 4, 4, 5, 6, 6, 7]))
Hard
11. Challenge: Write a Python program to solve the Fibonacci sequence using dynamic programming.
Answer:
def fibonacci(n):
fib = [0, 1]
for i in range(2, n+1):
fib.append(fib[i-1] + fib[i-2])
return fib[n]
print(fibonacci(10))
12. Challenge: Write a Python program to find all prime numbers up to a given number using the Sieve of Eratosthenes.
Answer:
def sieve_of_eratosthenes(n):
primes = [True] * (n + 1)
p = 2
while p * p <= n:
if primes[p]:
for i in range(p * p, n + 1, p):
primes[i] = False
p += 1
return [p for p in range(2, n+1) if primes[p]]
print(sieve_of_eratosthenes(30))
13. Challenge: Write a Python function to implement a binary search algorithm.
Answer:
def binary_search(arr, x):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1
print(binary_search([1, 2, 3, 4, 5, 6, 7], 4))
14. Challenge: Write a Python program to solve the “N-Queens” problem using backtracking.
Answer:
def is_safe(board, row, col, n):
for i in range(col):
if board[row][i] == 1:
return False
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if board[i][j] == 1:
return False
for i, j in zip(range(row, n, 1), range(col, -1, -1)):
if board[i][j] == 1:
return False
return True
def solve_n_queens(board, col, n):
if col >= n:
return True
for i in range(n):
if is_safe(board, i, col, n):
board[i][col] = 1
if solve_n_queens(board, col + 1, n):
return True
board[i][col] = 0
return False
n = 4
board = [[0] * n for _ in range(n)]
solve_n_queens(board, 0, n)
for row in board:
print(row)
15. Challenge: Write a Python function to perform Merge Sort.
Answer:
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
merge_sort(left_half)
merge_sort(right_half)
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
arr[k] = right_half[j]
j += 1
k += 1
array = [12, 11, 13, 5, 6, 7]
merge_sort(array)
print("Sorted array:", array)
Are you interested in more coding challenges? Check out additional challenges on our website to enhance your skills and knowledge.