Python Assignments
  • Python Assignments
  • Operators Assignment
  • User Input Assignments
  • String Assignments
  • Conditional Statements - Assignments
  • Loops - Assignments
  • List Assignments
  • Function Practice Questions
  • File Handling Assignments
  • Miscellaneous Python Assignments
Powered by GitBook
On this page

Function Practice Questions

  1. Write a function that takes two numbers as input and returns their sum.

def add_numbers(num1, num2):
    return num1 + num2
  1. Write a function that takes a list of numbers as input and returns the maximum value in the list.

def find_max(numbers):
    return max(numbers)
  1. Write a function that takes a string as input and returns the number of vowels in the string.

def count_vowels(word):
    vowels = "aeiou"
    count = 0
    for char in word:
        if char.lower() in vowels:
            count += 1
    return count
  1. Write a function that takes a list of words as input and returns a new list that contains only the words that start with a vowel.

def start_with_vowel(words):
    vowels = "aeiou"
    result = []
    for word in words:
        if word[0].lower() in vowels:
            result.append(word)
    return result
  1. Write a function that takes a list of numbers as input and returns a new list that contains only the even numbers.

def find_even(numbers):
    result = []
    for num in numbers:
        if num % 2 == 0:
            result.append(num)
    return result
  1. Write a function that takes a string as input and returns the string in reverse order.

def reverse_string(string):
    return string[::-1]
  1. Write a function that takes a list of numbers as input and returns the average value of the numbers.

def find_average(numbers):
    return sum(numbers) / len(numbers)
  1. Write a function that takes a string as input and returns a new string that has all the vowels removed.

def remove_vowels(string):
    vowels = "aeiou"
    result = ""
    for char in string:
        if char.lower() not in vowels:
            result += char
    return result
  1. Write a function that takes a list of words as input and returns a new list that contains only the words that have more than five characters.

def find_long_words(words):
    result = []
    for word in words:
        if len(word) > 5:
            result.append(word)
    return result
  1. Write a function that takes a list of strings as input and returns a new list that contains the length of each string.

def find_length(strings):
    result = []
    for string in strings:
        result.append(len(string))
    return result
  1. Write a function that takes a list of numbers as input and returns the sum of the squares of all the numbers in the list.

def sum_of_squares(numbers):
    result = 0
    for num in numbers:
        result += num ** 2
    return result
  1. Write a function that takes a string as input and returns the number of words in the string.

def count_words(string):
    words = string.split()
    return len(words)
  1. Write a Python function to find the maximum of three numbers.

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

print(find_max(5, 9, 3))  # Output: 9
  1. Write a Python function to reverse a string.

def reverse_string(s):
    return s[::-1]

print(reverse_string("hello"))  # Output: olleh
  1. Write a Python function to check if a string is a palindrome.

def is_palindrome(s):
    return s == s[::-1]

print(is_palindrome("racecar"))  # Output: True
  1. Write a Python function to calculate the factorial of a number.

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

print(factorial(5))  # Output: 120
  1. Write a Python function to check if a number is prime.

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

print(is_prime(17))  # Output: True
  1. Write a Python function to find the greatest common divisor (GCD) of two numbers.

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

print(gcd(12, 8))  # Output: 4
  1. Write a Python function to convert a string to title case.

def to_title_case(s):
    return s.title()

print(to_title_case("the quick brown fox"))  # Output: The Quick Brown Fox
  1. Write a Python function to calculate the area of a circle.

def circle_area(radius):
    return 3.14 * radius**2

print(circle_area(5))  # Output: 78.5
  1. Write a Python function to generate a list of all prime numbers up to a given number.

def primes_up_to(n):
    primes = []
    for num in range(2, n+1):
        if is_prime(num):
            primes.append(num)
    return primes

print(primes_up_to(20))  # Output: [2, 3, 5, 7, 11, 13, 17, 19]
  1. Write a Python function to calculate the Fibonacci sequence up to a given number of terms.

def fibonacci(n):
    fib = [0, 1]
    while len(fib) < n:
        fib.append(fib[-1] + fib[-2])
    return fib

print(fibonacci(10))  # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
PreviousList AssignmentsNextFile Handling Assignments

Last updated 2 years ago