This first exercise is a reminder of how list indexing and slicing works in Python. The key detail is that indexing starts from zero instead of one in Python. If you are curious, you can read about why Python uses zero-based indexing in this blog post.
(a) Print the third element in the list below.
(b) Print elements 3 to 6.
(c) Print the last element.
a=[1,2,3,4,5,6,7,8,9,10] print(type(a)) print(a) print(len(a))
<class 'list'> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10
(a) What is the difference between a function and a method in Python? Try to remember the discussion from DataCamp.
(b) The next cell will create an empty list and print a list of available methods for that list object (ignore the ones with double underscores). Use the append method a few times to add elements to the stocks list. Then, use the appropriate method to remove one of the elements from the list.
Write a function called middle that takes a list and returns a new list that contains all but the first and last elements (Think Python 10-3). You can test it on the list below.
Write a function called chop that takes a list and modifies it by removing the first and last elements (Think Python 10-4). Think about the difference between this exercise and the previous one. This is something that you saw on DataCamp, try to remember it.
Write a function called is_sorted that takes a list as a parameter and returns True if the list is sorted in ascending order and false otherwise (Think Python 10-5). Use the internet to discover how to sort a Python list.
Write a function called k_largest that takes a list of numbers and a parameter k and returns the k largest numbers in the list.
Two words are anagrams if you can rearrange the letters from one to spell the other. Write a function called is_anagram that takes two strings and returns True if they are anagrams (Think Python 10-6). The exercise below is a reminder of how to evaluate logical statements in Python (which you saw on DataCamp when they discussed Boolean indexing of arrays)
a=10 b=15 a<b
Write a function that takes three numbers as separate inputs and returns the largest of the three. Do this by building a list.
Create a function first_k_digits that takes a number and returns its first k digits as a list. Test it by obtaining the first 5 digits of $\pi$.
Write a function median that takes a list of numbers and returns the median. You can assume that the size of the list is an odd number.