-
Notifications
You must be signed in to change notification settings - Fork 84
advanced python: labs
Welcome to the Advanced Python lab! Work through this sheet in pairs, making sure that you understand each bit of code before you move on.
If you get something wrong, let us know! Other people may have done the same thing; maybe you can help them.
Hit ctrl
+alt
+t
to bring up a terminal. Using the pip
command:
- Show a list of all the packages installed on your system. Using only terminal commands, find out how many there are.
- Use pip to install the
pandas
library. Pandas is a popular numerical library for Python. You can read about it here - Using terminal commands, find out if
beautifulsoup4
is installed. If it isn't, install it.
Write a function (using def
!) that takes a number and returns True if it's even. Let's call the function is_even
Use your is_even
function to return only the even numbers from the following list:
numbers = [1,56,234,87,4,76,24,69,90,135]
Now, rewrite your code to use a lambda: write the is_even code in-line, rather than defining and then using your function. Look at the slides for a hint.
Next, re-write your code to return all the odd numbers in the numbers
collection.
Investigate the function called not
. What does it do?
Using the built-in function not
and your function is_even
, find another way to return all the odd numbers in the numbers
collection from the previous section.
Remember that a folding operation (sometimes called reduce) takes a collection of data and returns one piece of data, by applying a function to all the items in order. An example might be to add up all the numbers in a list:
total = fold(lambda item, running_total: item + running_total, [1, 2, 3, 4, 5])
hint: from functools import fold
Write a function called join_strings
that takes a list of words and uses +
to join them all together. For example:
words = ["hello", "world"]
helloworld = join_strings(words) # "hello world"
Python has a very powerful way to perform the map
and filter
operations in one go: List comprehensions. The following code splits a sentence into its constituent words, we've seen this kind of code before:
sentence = "the quick brown fox jumps over the lazy dog"
words = sentence.split()
The following comprehension returns a list of the lengths of each word. Enter the following code in the python
REPL:
[len(word) for word in words]
Now, let's do the same for words that aren't "the":
[len(word) for word in words if word != "the"]
- Create a list out of only the positive numbers from this list:
numbers = [34.6, -203.4, 44.9, 68.3, -12.2, 44.6, 12.7]
- Create a list containing only the even numbers from this list:
numbers = [12, 54, 33, 67, 24, 89, 11, 24, 47]
- Create a list containing tuples of the uppercase version and the length of the following words:
words = ["hello", "my", "name", "is", "Sam"]
for example, the first element in your list would be
('HELLO', 5)
Investigate the python module system. Learn how to call out to functions in code that you've already written (e.g. Web APIs, GPIO labs) from another file.
What's a class? You should know this from your studies.
Write a Python class for a Person. The person should inherit from Object and have the following methods:
- A
constructor
which takes the persons name and date of birth - speak() - prints "hello"
- walk() - prints "walking away"
- get_name() - returns the person's name
- get_age() - returns the person's age
Write another class called Student. Student inherits from Person and has the following methods:
- The
constructor
also takes a String List of course names - get_courses() - returns the course list
- speak() - in the subclass, prints "I'm so tired!"
Create a github repo called glblcd_MP3
, initialise a local repo at ~/glblcd_MP3, and setup your github repo as a remote.
The aim of this challenge is to implement a Class in Python to maintain an MP3 Playlist. This class could be used when developing the software for a music App on a smartphone or an mp3 player.
This MP3 playlist will be stored as a queue of MP3 tracks. When new tracks are being added to the playlist, they are enqueued at the end of the playlist.
The key features that we will implement within this class are the ability to:
- Load a playlist from a text file
- Display all the tracks from the playlist
- Enqueue (Add) an MP3 to the playlist
Make your repository public so that we can look it up on GitHub.
Additionally implement the following features within the Playlist class:
- Remove an MP3 from the playlist
- Save a playlist on a text file
- Shuffle all the songs in the playlist
- Count the number of tracks in the playlist
- Calculate the total duration of the playlist
- Clear/Reset the playlist
- Check if the playlist is empty