Lists#
For the exercises 1 - 7, use the following list. Use the Python functions and methods.
grades = [87.6, 92.3, 84.5, 90.0, 86.0, 98.6]
How many elements are in the list?
Provide two ways of getting the first element in the list.
Provide two ways of getting the last element in the list
Remove the first element from the list and return the value.
Remove the last element from the list and return the value
Concatenate this list to grades:
[76.4, 99.2, 88.8]Sort the list
1grades = [87.6, 92.3, 84.5, 90.0, 86.0, 98.6]
2
3#2. length
4len(grades)
5
6#3
7print(grades[0])
8print(grades[-len(grades)])
9
10#4
11print(grades[len(grades)-1])
12print(grades[-1])
13
14#5
15grades.pop(0)
16
17#6
18grades.pop
19
20#7
21grades = grades + [76.4, 99.2, 88.8]
22print(grades)
23
24#8 Remove the third to the last element
25grades.pop(-3)
26print(grades)
27
28#9
29grades.sort()
30print(grades)
87.6
87.6
98.6
98.6
[92.3, 84.5, 90.0, 86.0, 98.6, 76.4, 99.2, 88.8]
[92.3, 84.5, 90.0, 86.0, 98.6, 99.2, 88.8]
[84.5, 86.0, 88.8, 90.0, 92.3, 98.6, 99.2]
Exercises#
Write a short program that reads in a sentence from a user. Then create a list of words (punctuation can be included as part of the words) by separating the words/characters by whitespace. Then reverse the list. Finally, join the list back together into a single string with words separated by a colon
:and print the resulting string.
1# Answer to #1
2sentence = input("Enter a sentence: ")
3word_list = sentence.split()
4word_list.reverse()
5result = ":".join(word_list)
6print(result)
---------------------------------------------------------------------------
StdinNotImplementedError Traceback (most recent call last)
Cell In[2], line 2
1 # Answer to #1
----> 2 sentence = input("Enter a sentence: ")
3 word_list = sentence.split()
4 word_list.reverse()
5 result = ":".join(word_list)
File ~/Documents/GitHub/jupyternotebooks/venv/lib/python3.14/site-packages/ipykernel/kernelbase.py:1402, in Kernel.raw_input(self, prompt)
1400 if not self._allow_stdin:
1401 msg = "raw_input was called, but this frontend does not support input requests."
-> 1402 raise StdinNotImplementedError(msg)
1403 return self._input_request(
1404 str(prompt),
1405 self._get_shell_context_var(self._shell_parent_ident),
1406 self.get_parent("shell"),
1407 password=False,
1408 )
StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
Write a function, ascending, that returns true if each of the elements in list is greater than the previous element.
1def ascending(values):
2 for index in range(1, len(values)):
3 if values[index] <= values[index - 1]:
4 return False
5
6 return True
7
8
9print(ascending([1, 2, 3, 4])) # True
10print(ascending([1, 2, 2, 4])) # False
11print(ascending([5, 3, 7, 9])) # False
True
False
False
Write a function, count_odds, that counts the number of odd numbers in a list.
1def count_odds(values):
2 count = 0
3
4 for value in values:
5 if value % 2 == 1:
6 count = count + 1
7
8 return count
9
10
11print(count_odds([1, 2, 3, 4, 5])) # 3
12print(count_odds([2, 4, 6, 8])) # 0
13print(count_odds([7, 10, 13, 16, 19])) # 3
3
0
3
Write a function, iterate_lists, that iterates through two lists at the same time, print the content of an element from the first list, followed by printing an element from the second list. If one list is longer than the others, then all of those elements should be printed.
1def iterate_lists(first_list, second_list):
2 max_length = max(len(first_list), len(second_list))
3
4 for index in range(max_length):
5 if index < len(first_list):
6 print(first_list[index])
7
8 if index < len(second_list):
9 print(second_list[index])
10
11
12iterate_lists([1, 2, 3], ["a", "b", "c", "d", "e"])
1
a
2
b
3
c
d
e