1.
Check a number is Prime Number or not in a range
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
def prime_in_range(start, end):
for num in range(start, end + 1):
if is_prime(num):
print(num, "is Prime")
else:
print(num, "is Not Prime")
# Example: Check numbers between 10 and 20
prime_in_range(10, 20)
2. Fibonacci Series Generator
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
fibonacci(7)
3. Check Palindrome String
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("madam")) # True
print(is_palindrome("hello")) # False
4. Count Vowels in a String
def count_vowels(text):
vowels = "aeiouAEIOU"
return sum(1 for ch in text if ch in vowels)
print(count_vowels("Hello World"))
5. Find Maximum in a List
def find_max(numbers):
return max(numbers)
print(find_max([5, 12, 7, 3, 19]))
6. Word Frequency Counter
def word_count(sentence):
words = [Link]()
freq = {}
for word in words:
freq[word] = [Link](word, 0) + 1
return freq
print(word_count("python is an OOP and python is powerful"))
7. Temperature Converter
def celsius_to_fahrenheit(c):
return (c * 9/5) + 32
print("25°C =", celsius_to_fahrenheit(25), "°F")
8. Simple Calculator Using Functions
def add(a, b): return a + b
def sub(a, b): return a - b
def mul(a, b): return a * b
def div(a, b): return a / b if b != 0 else "Infinity"
print(add(10, 5))
print(sub(10, 5))
print(mul(10, 5))
print(div(10, 0))
9. Find Even and Odd Numbers from List
def separate_even_odd(numbers):
even = [n for n in numbers if n % 2 == 0]
odd = [n for n in numbers if n % 2 != 0]
return even, odd
even, odd = separate_even_odd([1, 2, 3, 4, 5, 6])
print("Even:", even)
print("Odd:", odd)
10. Anagram Checker
def is_anagram(s1, s2):
Two words (or phrases) are said to
return sorted(s1) == sorted(s2) be anagrams if they contain the
same characters in the same
print(is_anagram("listen", "silent")) # True frequency, but possibly in a
different order.
print(is_anagram("hello", "world")) # False
11. Reverse a Number
def reverse_number(n):
return int(str(n)[::-1])
print(reverse_number(12345)) # 54321
12. Check Armstrong Number A number is called an Armstrong number if the sum
of its own digits each raised to the power of the
def is_armstrong(n):
number of digits is equal to the number itself.
power = len(str(n))
Examples:
return n == sum(int(d)**power for d in str(n))
print(is_armstrong(153)) # True
print(is_armstrong(123)) # False
13. Greatest Common Divisor (GCD)
def gcd(a, b):
while b:
a, b = b, a % b
return a
print(gcd(48, 18)) # 6
14. LCM
def lcm(a, b):
greater = max(a, b)
while True:
if greater % a == 0 and greater % b == 0:
return greater
greater += 1
# Example
print(lcm(12, 15)) # 60
print(lcm(4, 5)) # 20
Explanation:
1. def lcm(a, b): defines a function taking two integers.
2. greater = max(a, b) initializes a candidate to start from the larger input value.
3. while True: starts an infinite loop that will keep testing candidates.
4. if greater % a == 0 and greater % b == 0: checks whether the current candidate is divisible by
both a and b.
o If true, return greater outputs the LCM and exits the function.
5. If not divisible by both, greater += 1 increments the candidate and repeats the loop.
15. Check Leap Year
Algorithm:
• Determines whether a given year is a leap year.
• Uses the standard leap year rules:
o A year is a leap year if it is divisible by 4,
o except years divisible by 100 are not leap years,
o except years divisible by 400 are leap years.
def is_leap_year(year):
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
print(is_leap_year(2024)) # True
print(is_leap_year(2100)) # False