Write a function that takes two numbers as input and returns their sum.
Copy def add_numbers(num1, num2):
return num1 + num2
Write a function that takes a list of numbers as input and returns the maximum value in the list.
Copy def find_max(numbers):
return max(numbers)
Write a function that takes a string as input and returns the number of vowels in the string.
Copy def count_vowels(word):
vowels = "aeiou"
count = 0
for char in word:
if char.lower() in vowels:
count += 1
return count
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.
Copy def start_with_vowel(words):
vowels = "aeiou"
result = []
for word in words:
if word[0].lower() in vowels:
result.append(word)
return result
Write a function that takes a list of numbers as input and returns a new list that contains only the even numbers.
Copy def find_even(numbers):
result = []
for num in numbers:
if num % 2 == 0:
result.append(num)
return result
Write a function that takes a string as input and returns the string in reverse order.
Copy def reverse_string(string):
return string[::-1]
Write a function that takes a list of numbers as input and returns the average value of the numbers.
Copy def find_average(numbers):
return sum(numbers) / len(numbers)
Write a function that takes a string as input and returns a new string that has all the vowels removed.
Copy def remove_vowels(string):
vowels = "aeiou"
result = ""
for char in string:
if char.lower() not in vowels:
result += char
return result
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.
Copy def find_long_words(words):
result = []
for word in words:
if len(word) > 5:
result.append(word)
return result
Write a function that takes a list of strings as input and returns a new list that contains the length of each string.
Copy def find_length(strings):
result = []
for string in strings:
result.append(len(string))
return result
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.
Copy def sum_of_squares(numbers):
result = 0
for num in numbers:
result += num ** 2
return result
Write a function that takes a string as input and returns the number of words in the string.
Copy def count_words(string):
words = string.split()
return len(words)
Write a Python function to find the maximum of three numbers.
Copy def find_max(a, b, c):
return max(a, b, c)
print(find_max(5, 9, 3)) # Output: 9
Write a Python function to reverse a string.
Copy def reverse_string(s):
return s[::-1]
print(reverse_string("hello")) # Output: olleh
Write a Python function to check if a string is a palindrome.
Copy def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("racecar")) # Output: True
Write a Python function to calculate the factorial of a number.
Copy def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5)) # Output: 120
Write a Python function to check if a number is prime.
Copy 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
Write a Python function to find the greatest common divisor (GCD) of two numbers.
Copy def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
print(gcd(12, 8)) # Output: 4
Write a Python function to convert a string to title case.
Copy def to_title_case(s):
return s.title()
print(to_title_case("the quick brown fox")) # Output: The Quick Brown Fox
Write a Python function to calculate the area of a circle.
Copy def circle_area(radius):
return 3.14 * radius**2
print(circle_area(5)) # Output: 78.5
Write a Python function to generate a list of all prime numbers up to a given number.
Copy 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]
Write a Python function to calculate the Fibonacci sequence up to a given number of terms.
Copy 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]