# 5.1 Functions
Run to view results
# A. Write a function that accepts one parameter, name
# The function should print the message "Hello, _____." filling in the blank with the name
def first_function(name):
print(f"Hello, {name}.")
first_function("Lou")
Run to view results
# B. Write a function that accepts one parameter, a number
# The function should return true or false depending on whether the number is even
def even_odd(number):
if number % 2 == 0:
print(f"{number} is an even number")
else:
print(f"{number} is an odd number")
even_odd(7)
Run to view results
l_1 = [1,2,3,4,5]
Run to view results
type(l_1)
Run to view results
# C. Write a function that accepts one parameter, a list
# The function should return the sum total of all numbers in the list, using a loop
def list_fun(list_1):
if isinstance(list_1,list):
sum_of_list = 0
for i in list_1:
sum_of_list += i
print(sum_of_list)
else:
print("need to enter a list")
list_fun(l_1)
Run to view results
# D. Write a function that accepts one parameter, a dictionary
# The function should print the message "Introducing ______ ______, ________!"
# Fill in the blanks with the "first_name", "last_name", and "title" properties of the dictionary
def introduce_person(person_info):
if all(key in person_info for key in ['first_name', 'last_name', 'title']):
print(f"Introducing {person_info['first_name']} {person_info['last_name']}, {person_info['title']}!")
else:
print("The dictionary is missing one or more of the required keys: 'first_name', 'last_name', or 'title'.")
person = {
'first_name': 'Jane',
'last_name': 'Doe',
'title': 'python expert'
}
introduce_person(person)
Run to view results
# 5.2 Problem-Solving Functions
Run to view results
string_list = ["hello", "world", "this is a long string", "short", "longer", "longest string in the list"]
Run to view results
# A. Find the longest string in a given list of stringsdef find_longest_string(string_list):
def find_longest_string(string_list):
longest_string = ""
for s in string_list:
if len(s) > len(longest_string):
longest_string = s
return longest_string
string_list = ["hello", "world", "this is a long string", "short", "longer", "longest string in the list"]
print("The longest string is:", find_longest_string(string_list))
Run to view results
# B. Determine whether a given list contains a given value
def contains_value(lst, value):
if value in lst:
return True
else:
return False
my_list = [1, 2, 3, 4, 5, "hello", "world"]
search_value = "hello"
print(f"Does the list contain '{search_value}'?", contains_value(my_list, search_value))
search_value = 7
print(f"Does the list contain '{search_value}'?", contains_value(my_list, search_value))
Run to view results
# C. Count how many times a given string contains a given letter
def count_letter_occurrences(input_string, letter):
count = 0
for char in input_string:
if char == letter:
count += 1
return count
my_string = "hello world"
search_letter = "l"
print(f"The letter '{search_letter}' appears {count_letter_occurrences(my_string, search_letter)} times in the string '{my_string}'.")
Run to view results
# D. Find the most common letter in a given string, and use the previous function in your answer
def count_letter_occurrences(input_string, letter):
count = 0
for char in input_string:
if char == letter:
count += 1
return count
def find_most_common_letter(input_string):
input_string = input_string.replace(" ", "").lower()
most_common_letter = ''
highest_count = 0
for letter in set(input_string):
count = count_letter_occurrences(input_string, letter)
if count > highest_count:
highest_count = count
most_common_letter = letter
return most_common_letter, highest_count
my_string = "Hello world"
most_common_letter, occurrence = find_most_common_letter(my_string)
print(f"The most common letter is '{most_common_letter}' and it appears {occurrence} times.")
Run to view results
# E. Find the area of a triangle given its length and height as parameters
def triangle_area(base, height):
area = 0.5 * base * height
return area
base_length = 10
height = 5
print(f"The area of the triangle is {triangle_area(base_length, height)} square units.")
Run to view results