Python Coding Practice Problems
0 190
Python Coding Practice Problems
Consistent practice with Python coding problems is a great way to sharpen your programming skills, prepare for interviews, and deepen your understanding of various Python concepts. At CodingTag, we provide a wide variety of problems organized by topic, ensuring a comprehensive learning journey.
Why Practice Python Problems?
Solving Python problems enhances your ability to write clean, efficient code, helps you learn problem-solving techniques, and improves your debugging skills. It also prepares you to handle real-world programming challenges.
Python Practice Problems with Answers
-
Find the factorial of a given number.
def factorial(n): result = 1 for i in range(2, n+1): result *= i return result print(factorial(5)) # Output: 120
-
Check if a string is a palindrome.
def is_palindrome(s): return s == s[::-1] print(is_palindrome("madam")) # Output: True print(is_palindrome("hello")) # Output: False
-
Generate Fibonacci series up to n terms.
def fibonacci(n): fib_series = [0, 1] for i in range(2, n): fib_series.append(fib_series[-1] + fib_series[-2]) return fib_series[:n] print(fibonacci(7)) # Output: [0, 1, 1, 2, 3, 5, 8]
-
Find the maximum and minimum elements in a list.
def find_max_min(lst): return max(lst), min(lst) print(find_max_min([3, 9, 1, 7])) # Output: (9, 1)
-
Count the number of vowels in a string.
def count_vowels(s): vowels = "aeiouAEIOU" return sum(1 for char in s if char in vowels) print(count_vowels("Hello World")) # Output: 3
-
Reverse a list without using built-in reverse functions.
def reverse_list(lst): reversed_lst = [] for i in range(len(lst)-1, -1, -1): reversed_lst.append(lst[i]) return reversed_lst print(reverse_list([1, 2, 3, 4])) # Output: [4, 3, 2, 1]
-
Merge two dictionaries into one.
def merge_dicts(d1, d2): merged = d1.copy() merged.update(d2) return merged print(merge_dicts({'a': 1, 'b': 2}, {'c': 3, 'd': 4})) # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
-
Check if a number is prime.
def is_prime(num): if num <= 1: return False for i in range(2, int(num ** 0.5) + 1): if num % i == 0: return False return True print(is_prime(17)) # Output: True print(is_prime(20)) # Output: False
-
Sort a list of tuples based on the second element.
def sort_tuples(tuples_list): return sorted(tuples_list, key=lambda x: x[1]) print(sort_tuples([(1, 3), (4, 1), (2, 2)])) # Output: [(4, 1), (2, 2), (1, 3)]
-
Read a text file and count the frequency of each word.
def word_frequency(filename): freq = {} with open(filename, 'r') as file: for line in file: words = line.strip().split() for word in words: freq[word] = freq.get(word, 0) + 1 return freq # Example usage: create a file 'sample.txt' with some text to test # print(word_frequency('sample.txt'))
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments