Check if a string is a palindrome using a function
def is_palindrome(str1): """ Palindrome is a string that reads the same forward and backward. """ str2 = str1[::-1] if str1 == str2: return "The string is a palindrome" else: return "The string is not a palindrome"print(is_palindrome("palindrome"))print(is_palindrome("malayalam"))
Output:The string is not a palindromeThe string is a palindrome
num1 = 10if num1 > 1: for i in range(2, num1): if num1 % i == 0: print("The number is not a prime number") break else: print("The number is a prime number")
def factorial(num1): """ Factorial is the product of all positive integers less than or equal to a given positive integer. """ if num1 == 0 or num1 == 1: return 1 else: return num1 * factorial(num1 - 1)print(factorial(8))
def fibonacci(num1): """ Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. """ if num1 == 0: return 0 elif num1 == 1: return 1 else: return fibonacci(num1 - 1) + fibonacci(num1 - 2)print(fibonacci(10))
def average(*args): """ Average is the sum of all numbers divided by the number of numbers. """ return sum(args) / len(args)print(average(1, 2, 3, 4, 5, 9))
def average_dict(**kwargs): """ Average is the sum of all numbers divided by the number of numbers. """ return sum(kwargs.values()) / len(kwargs)print(average_dict(a=1, b=2, c=3, d=4, e=5))
def count_vowels(str1): """ Vowels are the letters a, e, i, o, and u. """ vowels = "aeiouAEIOU" count = sum(1 for char in str1 if char in vowels) return countprint(count_vowels("programming"))
def is_even(num1): """ Even is a number that is divisible by 2. """ if num1 % 2 == 0: return "The number is even" else: return "The number is odd"print(is_even(10))
def is_positive(num1): """ Positive is a number that is greater than 0. """ if num1 > 0: return "The number is positive" elif num1 < 0: return "The number is negative" else: return "The number is zero"print(is_positive(10))
def is_anagram(str1, str2): """ Anagram is a string that has the same letters in a different order. """ return sorted(str1.lower()) == sorted(str2.lower())print(is_anagram("listen", "silent"))print(is_anagram("Grow", "Word"))