Kaggle — Learn Python Challenge: Day 4

Editor: Ishmael Njie

DataRegressed Team
DataRegressed
3 min readJun 15, 2018

--

Onto Day 4!

Hope you have been keeping up with the daily posts!

Here are the links to the previous exercises:

Day 1 — Hello Python!(Variable assignment etc.)

Day 2 — Functions and Getting Help

Day 3 — Booleans and Conditionals

Day 4 was on lists. In Python, lists represent sequences of values. They can be defined with comma-separated values between square brackets:

Let’s get into the exercises; follow with the notebook for this set of exercises:

  • Exercise 1:

The first task was to create a function that returned the second element of a given list.

Here, the index 1 refers to the 2nd element as Python uses zero-based indexing; where the first element is index 0.

  • Exercise 2:

Here we had to define a function that took a list of many lists as an argument and then returned the 2nd element from the last list, in the list of lists… hope that made sense (refer to the notebook for a better understanding).

  • Exercise 3:

This exercise introduced a new item in Mario Kart! A purple shell, which swaps the racer in first place and the racer in last place (a pretty evil item!)

So we had to define a function that set the first place racer to last place and vice versa.

We saw a technique like this in Day 1 of the exercises! Using a temporary variable to store one of the variables.

  • Exercise 4:

This exercise was testing our understanding of lists and slicing. We were to guess the lengths of the following lists:

a = 3

b = 2 as [2,3] counts as one element in the entire list.

c = 0 items (empty list)

d = 2 as the expression returns [2,3]

  • Exercise 5:

This tasks involves using a list to record party attendees. The task is to define a function that details whether an attendee is fashionably late (arriving after at least half of the guests but not last).

  • Exercise 6:

This was a tough ‘riddle’ to complete. A very interesting problem; to count the number of negative numbers in a given list.

To solve this problem, it would be great to sort the list. If we first append the list with the number ‘0’, we can search for the 0 with the .index method. If the list is in order after we append the list with a 0, we get the position of the 0, and all the items below that place will be negative so we can infer the number of negative numbers.

If we run the function:

This will return the number 2, which is the position of the 0 after it is sorted, as well as the number of negative numbers in the list. Check the notebook for a full explanation!

Keep up with the challenges and stay tuned for Day 5!

--

--