Miscellaneous Exercise

Test your Python Knowledge with this Miscellaneous Exercise
Author

Juma Shafara

Published

May 1, 2023

Keywords

python, python quiz, python data types, python functions, reverse string, palindrome checker, factorial calculator, fibonacci sequence, prime number checker, list reversal, anagram checker, finding unique elements

  1. Reverse a String: Write a function that takes a string as input and returns the reverse of that string.
#Qn.1 reverse a string
def reverseString (text):
    return text[::-1]
reverseString ('hello world')
'dlrow olleh'
  1. Palindrome Checker: Write a function that takes a string as input and returns True if it’s a palindrome (reads the same forwards and backwards), False otherwise.
#Qn.2 Palindrome Checker
def isPalindrome (text):
    return text == text[::-1]
isPalindrome('madam')
True
  1. Factorial Calculator: Write a function that calculates the factorial of a given number. The factorial of a non-negative integer is the product of all positive integers less than or equal to the integer.
#Qn.3 Factorial Calculator
def numberFactorial(number):
    if number==0 or number==1:
        print(1)
    else:
        factorial = 1
        for value in range (1, number+1):
            factorial *= value
        print(factorial)
numberFactorial(6)
720
  1. Fibonacci Sequence: Write a function to generate the Fibonacci sequence up to a certain number of terms. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1.
#Qn.4 Fibonacci Sequence
def fibonacciSequence (number):
    sequence = [0,1]
    while len (sequence)< number:
        sequence.append(sequence[-1] + sequence[-2])
    return sequence
fibonacciSequence (8)
[0, 1, 1, 2, 3, 5, 8, 13]
  1. Prime Number Checker: Write a function that checks if a given number is prime.
#Qn.5 Prime Number Checker
def isPrimeNumber (number):
    if number <= 1:
        return False
    for value in range(2,int(number**0.5)+1):
        if number % value == 0:
            return False
    return True
isPrimeNumber(4)
False
  1. List Reversal: Write a function that reverses a list.
#Qn.6 List Reversal
def reverseList(list_):
    return list_[::-1]
reverseList([1,3,5,6,8])
[8, 6, 5, 3, 1]
  1. List Sorting: Write a function that sorts a list of integers in ascending order without using Python’s built-in sorting functions.
#Qn.7 List Sorting
def sortList(list_):
    return sorted(list_)
sortList([3,7,1,9,2])
[1, 2, 3, 7, 9]
  1. Anagram Checker: Write a function that takes two strings as input and returns True if they are anagrams (contain the same letters with the same frequency), False otherwise.
#Qn.8 Anagram Checker
def areAnagram(string1, string2):
    return sorted(string1) == sorted(string2)
areAnagram('listen','silent')
True
  1. Count Words in a String: Write a function that takes a string as input and returns the count of words in that string.
#Qn.9 Count words in a String
def countWords(string):
    return len(string.split())
countWords('hello world')
2
  1. Unique Elements: Write a function that takes a list and returns a new list with only the unique elements from the original list.
#Qn.10 Unique Elements
def uniqueElements(list_):
    return list(set(list_))
uniqueElements([2,4,3,2,6,4,7,9,5,3,5,1,6])
[1, 2, 3, 4, 5, 6, 7, 9]

What’s on your mind? Put it in the comments!

Back to top