Python exercices notebook 1
Let's solve some problems using python¶
Problem 1¶
Write a Python function that takes a list of numbers as an argument and returns the largest number in the list.
def find_largest(nums):
largest = nums[0]
for num in nums:
if num > largest:
largest = num
return largest
numbers = [3, 5, 2, 8, 4]
print(find_largest(numbers)) # Output: 8
8
Problem 2¶
Write a Python function that takes a string as an argument and returns the number of vowels in the string.
def count_vowels(s):
vowels = 'aeiouAEIOU'
count = 0
for char in s:
if char in vowels:
count += 1
return count
sentence = "Hello, World!"
print(count_vowels(sentence)) # Output: 3
3
Problem 3¶
Write a Python function that takes a list of strings as an argument and returns a list of strings that contains only the even-length strings from the original list.
def even_length_strings(strings):
even_strings = []
for s in strings:
if len(s) % 2 == 0:
even_strings.append(s)
return even_strings
words = ['cat', 'dog', 'bird', 'monkey', 'elephant']
print(even_length_strings(words)) # Output: ['bird', 'monkey', 'elephant']
['bird', 'monkey', 'elephant']
Write a Python function that takes a list of integers as an argument and returns the sum of the squares of the integers in the list.
def sum_of_squares(nums):
return sum([x**2 for x in nums])
numbers = [1, 2, 3, 4, 5]
print(sum_of_squares(numbers)) # Output: 55
55
Problem 5¶
Palindrome checker: Write a function that takes in a string and returns a Boolean indicating whether the string is a palindrome (a word or phrase that reads the same backwards as forwards).
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("racecar")) # True
print(is_palindrome("hello")) # False
True False
:#### Problem 6 FizzBuzz: Write a program that prints the numbers from 1 to 25. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
for i in range(1, 26):
if i % 15 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz
Problem 7¶
Reverse a list: Write a function that takes in a list and returns a new list with the elements in reverse order.
def reverse_list(lst):
output = []
for l in range(len(lst)-1, -1, -1):
output.append(lst[l])
return output
print(reverse_list([1, 2, 3, 4, 5])) # [5, 4, 3, 2, 1]
[5, 4, 3, 2, 1]
Problem 8¶
Leap year checker: Write a function that takes in a year and returns a Boolean indicating whether the year is a leap year. A leap year is divisible by 4, but not divisible by 100 unless it is also divisible by 400.
def is_leap_year(year):
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return True
else:
return False
print(is_leap_year(2000)) # True
print(is_leap_year(1900)) # False
True False
Problem 9¶
Flatten a nested list: Write a function that takes in a nested list and returns a new list with all the elements flattened.
def flatten_list(lst):
flat_list = []
for i in lst:
if type(i) == list:
flat_list.extend(flatten_list(i))
else:
flat_list.append(i)
print(flat_list)
return flat_list
print(flatten_list([1, [2, 3]])) # [1, 2, 3]
[1] [2] [2, 3] [1, 2, 3] [1, 2, 3]
[1, [2, [3, 4], 5]] => flat_list = [1]
[1, [2, [3, 4], 5]] => flat_list = [2]
[1, [2, [3, 4], 5]] => flat_list = [3, 4]
[1, [2, [3, 4] ,5]] => flat_list = [2, 3, 4]
[1, [2, [3, 4] ,5]] => flat_list = [2, 3, 4, 5]
[1, [2, [3, 4] ,5]] => flat_list = [1, 2, 3, 4, 5]
Problem 10¶
Fibonacci Sequence Generator: Write a function that generates the Fibonacci sequence up to a given number of terms. The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.
def fibonacci(n):
fib_list = [0, 1]
for i in range(2, n):
fib_list.append(fib_list[i-1] + fib_list[i-2])
return fib_list
print(fibonacci(10)) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]