Kaggle — Learn Python Challenge: Day 1

Editor: Ishmael Njie

DataRegressed Team
DataRegressed
3 min readJun 11, 2018

--

Kaggle is the home of Data Science and Machine Learning, and this week they are providing a ‘7-day Python sprint’ where you can learn Python and/or brush up on the foundations of the language from small projects delivered in the notebooks daily.

Day 1 started with an introductory notebook on variable assignment as well as working with numbers and arithmetic in Python. After this introduction, the notebook leads onto Exercises for Day 1. A cool feature of the notebook is that hints and solutions are given within the notebook (I may have needed to take a peek at a few…).

So in this post, I’m going to give an outline of the Exercises in Day 1:

  • Exercise 1:

All that was needed for this exercise was to complete the code by creating variables called ‘radius’ and ‘area’ with the relevant equations.

pi = 3.14159 # approximate
diameter = 3

# Create a variable called 'radius' equal to half the diameter
radius = diameter/2 <- CODED THIS
# Create a variable called 'area', using the formula for the area of a circle: pi times the radius squared
area = pi * (radius**2) <- CODED THIS
q1.check()

In the introductory notebook, there were tips on the operators in Python like the ** operator for exponentiation.

  • Exercise 2:

The second exercise was to add code to the block that swapped variables a and b.

a = [1, 2, 3]
b = [3, 2, 1]
q2.store_original_ids()
####################################################################
######## MY CODE STARTS HERE #######
c = b
b = a
a = c
print(a)
print(b)
####################################################################q2.check()

A third variable is needed to store one of the variables so that one can update a and b accordingly.

  • Exercise 3:

This exercise focused on operators in Python and the idea of BIDMAS in Mathematics. The task was to add brackets to the expression so that it evaluates to a certain value.

Part a:

Add brackets to make sure the expression evaluates to 1.

5 - 3 // 2Added brackets
(5 - 3) // 2

The // operator for floor division grabs the integer value of the division, without any decimal parts.

Part b:

Add brackets to make sure the expression evaluates to 0.

8 - 3 * 2 - 1 + 1Added brackets
(8 - 3) * (2 - (1 + 1))
  • Exercise 4:

The aim of this task was to work out the remainder after dividing two numbers. In this case, Alice, Bob and Carol wanted to share candies equally between the three of them; while smashing the remaining candies that couldn’t be shared equally.

alice_candies = 121
bob_candies = 77
carol_candies = 109


######## MY CODE STARTS HERE #######
to_smash = (alice_candies + bob_candies + carol_candies) % 3
print(to_smash)
q4.check()

As you can probably guess, the % operator for modulus finds the remainder after dividing a by b.

  • Exercise 5:

The ‘-’ operator in Python is for subtraction, and used in succession will vary the answer given for a given expression, just like in Maths.

7------3

The answer to this expression is 10. Given an even number of subtraction signs, the operation turns to addition as a negative number multiplied by a negative number results in a positive number.

The variable ‘ninety_nine_dashes’ below has to be filled with the value of the above expression if there were 99 ‘-’ symbols instead of 6 symbols between the 7 and the 3.

ninety_nine_dashes = 4q5.check()

Given an odd number of subtraction signs, the operation will stays at subtraction.

Exercises 6 and 7 can be viewed here in my Kaggle Kernel as it will be easier to visualise and read!

Keep an eye out for Day 2!

--

--