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
Count the whitespace in a string
str1 = "Python is a programming language"count = str1.count(" ")print(count)
Output:4
Count a character in a string
str1 = "Python is a programming language"count = str1.upper().count("P")print(count)
Output:2
Check a number is prime or not
it is not handling the negative numbers and zero
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")
Find a factorial of a 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))
Output:40320
Find a fibonacci series
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))
Output:55
Find an average of a tuple
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))
Output:4.0
Find an average of a dictionary
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))
Output:3.0
Count vowels in a string
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"))
Output:3
Find a minimum value in a list
def find_min(list1): """ Minimum is the smallest number in a list. """ return min(list1)print(find_min([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
Output:1
Find a maximum value in a list
def find_max(list1): """ Maximum is the largest number in a list. """ return max(list1)print(find_max([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
Output:10
Find a sum of a list
def sum_list(list1): """ Sum is the sum of all numbers in a list. """ return sum(list1)print(sum_list([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
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))
Output:The number is even
Check if a number is positive or negative or zero
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))
Output:The number is positive
anagram of a string
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"))