0% found this document useful (0 votes)
3 views5 pages

Python Basics 23 Problems

The document contains 23 basic Python programming problems along with their solutions. Each problem is presented with a brief description and a corresponding function that implements the solution. Topics covered include checking even or odd numbers, prime numbers, Fibonacci series, factorials, and various algorithms for searching and sorting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Python Basics 23 Problems

The document contains 23 basic Python programming problems along with their solutions. Each problem is presented with a brief description and a corresponding function that implements the solution. Topics covered include checking even or odd numbers, prime numbers, Fibonacci series, factorials, and various algorithms for searching and sorting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

23 Basic Python Programming Problems

with Solutions
1. Check Even or Odd
def is_even(num):
return "Even" if num % 2 == 0 else "Odd"

2. Check Prime Number


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

3. Fibonacci Series
def fibonacci(n):
seq = [0, 1]
for i in range(2, n):
seq.append(seq[-1] + seq[-2])
return seq[:n]

4. Factorial
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
5. Palindrome Checker
def is_palindrome(s):
return str(s) == str(s)[::-1]

6. Armstrong Number
def is_armstrong(num):
power = len(str(num))
return num == sum(int(d)**power for d in str(num))

7. Sum of Digits
def sum_of_digits(num):
return sum(int(d) for d in str(num))

8. Reverse a Number
def reverse_number(num):
return int(str(num)[::-1])

9. Swap Two Numbers


def swap(a, b):
return b, a

10. Multiplication Table


def multiplication_table(n):
return [n * i for i in range(1, 11)]

11. Count Vowels


def count_vowels(s):
return sum(1 for ch in s.lower() if ch in 'aeiou')
12. Check for Anagram
def are_anagrams(str1, str2):
return sorted(str1) == sorted(str2)

13. Largest Among Three


def largest_of_three(a, b, c):
return max(a, b, c)

14. Check Leap Year


def is_leap_year(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

15. Sum of N Natural Numbers


def sum_natural(n):
return n * (n + 1) // 2

16. GCD and LCM


def gcd(a, b):
while b:
a, b = b, a % b
return a

def lcm(a, b):


return a * b // gcd(a, b)

17. Linear Search


def linear_search(lst, target):
for i, val in enumerate(lst):
if val == target:
return i
return -1
18. Binary Search
def binary_search(lst, target):
left, right = 0, len(lst) - 1
while left <= right:
mid = (left + right) // 2
if lst[mid] == target:
return mid
elif lst[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1

19. Remove Duplicates from List


def remove_duplicates(lst):
return list(set(lst))

20. Sort List (Bubble Sort)


def bubble_sort(lst):
n = len(lst)
for i in range(n):
for j in range(0, n-i-1):
if lst[j] > lst[j+1]:
lst[j], lst[j+1] = lst[j+1], lst[j]
return lst

21. Frequency of Characters


def char_frequency(s):
freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1
return freq
22. Second Largest in List
def second_largest(lst):
unique = list(set(lst))
unique.sort()
return unique[-2] if len(unique) >= 2 else None

23. Simple Calculator


def calculator(a, b, op):
if op == '+': return a + b
if op == '-': return a - b
if op == '*': return a * b
if op == '/': return a / b if b != 0 else 'Error: Divide by 0'
return 'Invalid Operation'

You might also like