Skip to content

Core Python — Basic (50+ Coding Exercises)

  1. Write a program that reads two integers and prints their sum, difference, product, and quotient (handle division by zero).
  2. Write a program that reads a string and prints it reversed.
  3. Write a program that checks whether a number is even or odd.
  4. Write a program that finds the maximum of three numbers.
  5. Write a program that computes the factorial of (n) (iterative).
  6. Write a program that prints the first (n) Fibonacci numbers.
  7. Write a program that checks if a number is prime.
  8. Write a program that prints all primes up to (n).
  9. Write a program that counts vowels in a string.
  10. Write a program that checks if a string is a palindrome (ignore spaces).
  11. Write a program that sums all numbers in a list.
  12. Write a program that finds the second-largest number in a list.
  13. Write a program that removes duplicates from a list (preserve order).
  14. Write a program that merges two lists and sorts the result.
  15. Write a program that counts occurrences of each word in a sentence.
  16. Write a program that finds the most frequent character in a string.
  17. Write a program that converts Celsius to Fahrenheit and vice versa.
  18. Write a program that computes simple interest and compound interest.
  19. Write a program that prints a right triangle pattern of * of height (n).
  20. Write a program that prints a multiplication table for a given number up to 10.
  21. Write a program that computes the sum of digits of an integer.
  22. Write a program that computes the digital root of a number.
  23. Write a program that checks whether two strings are anagrams.
  24. Write a program that splits a list into chunks of size (k).
  25. Write a program that rotates a list right by (k).
  26. Write a program that finds common elements between two lists (unique output).
  27. Write a program that finds intersection and union of two sets.
  28. Write a program that safely reads an integer from input until valid.
  29. Write a program that uses a dict as a phonebook (add/search/delete).
  30. Write a program that sorts a dictionary by values.
  31. Write a program that counts how many times each letter appears (a–z only).
  32. Write a program that removes punctuation from a string.
  33. Write a program that replaces multiple spaces with a single space.
  34. Write a program that finds the longest word in a sentence.
  35. Write a program that generates a list of squares from 1 to (n).
  36. Write a program that filters out negative numbers from a list.
  37. Write a program that reads a file and counts the number of lines, words, and characters.
  38. Write a program that copies a text file to another file.
  39. Write a program that appends a new line to a file.
  40. Write a program that reads a CSV-like file (comma-separated) and prints each row as a list.
  41. Write a function is_leap_year(year) and test it with multiple inputs.
  42. Write a function gcd(a, b) using Euclid’s algorithm.
  43. Write a function lcm(a, b) using gcd.
  44. Write a function that returns (min, max) from a list (without using built-ins min/max).
  45. Write a function that flattens a 2D list into 1D.
  46. Write a function that counts how many times a substring appears in a string (overlapping allowed).
  47. Write a function that validates a password (min length, digit, uppercase, lowercase).
  48. Write a program that simulates a basic calculator loop until user types quit.
  49. Write a program that converts an integer to binary (without using bin()).
  50. Write a program that converts a binary string to integer (without using int(x, 2)).
  51. Write a program that finds the first non-repeating character in a string.
  52. Write a program that checks if a list is sorted (ascending).