-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path16_lists_functions_exercises.py
57 lines (47 loc) · 2.24 KB
/
16_lists_functions_exercises.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Write a function that adds the elements of a list:
def add_elements(list1):
pass # remove this line once you've written the function
# Test your function with the following:
print(add_elements([1, 2, 3])) # Should print 6
print(add_elements([4, 5, 6])) # Should print 15
print(add_elements([7, 8, 9, 10, 11])) # Should print 45
print(add_elements([])) # Should print 0
# Write a function that only adds the ODD INDEXED elements of a list:
def add_odd_indexed_elements(list1):
pass # remove this line once you've written the function
# Test your function with the following:
print(add_odd_indexed_elements([1, 2, 3])) # Should print 2
print(add_odd_indexed_elements([4, 5, 6])) # Should print 5
print(add_odd_indexed_elements([7, 8, 9, 10, 11])) # Should print 18
print(add_odd_indexed_elements([])) # Should print 0
print(add_odd_indexed_elements([1])) # Should print 0
print(add_odd_indexed_elements([1, 2])) # Should print 2
# Write a function that returns only the odd indexed elements of a list (in a new list):
def odd_indexed_elements(list1):
pass # remove this line once you've written
# Test your function with the following:
print(odd_indexed_elements([1, 2, 3])) # Should print [2]
print(odd_indexed_elements([4, 5, 6])) # Should print [5]
print(odd_indexed_elements([7, 8, 9, 10, 11])) # Should print [8, 10]
print(odd_indexed_elements([])) # Should print []
print(odd_indexed_elements([1])) # Should print []
print(odd_indexed_elements([1, 2])) # Should print [2]
# Write a function that prints the 3rd character of a string.
def print_third_char(string1):
pass # remove this line once you've written the function
# Test your function with the following:
print_third_char("Hello!")
print_third_char("Goodbye!")
print_third_char("How are you?")
print_third_char("I'm a function!")
# Write a function that changes the 5th character of a string to an exclamation point.
# If the string is less than 5 characters long, print "String too short."
def change_fifth_char(string1):
pass # remove this line once you've written the function
# Test your function with the following:
change_fifth_char("Hellooo")
change_fifth_char("Goodbye")
change_fifth_char("How are you")
change_fifth_char("I'm a function")
change_fifth_char("Hi")
change_fifth_char("Bye")