This notebook is an exercise in the Python course. You can reference the tutorial at this link.


Exercises

Welcome to your first set of Python coding problems!

If this is your first time using Kaggle Notebooks, welcome!

Notebooks are composed of blocks (called "cells") of text and code. Each of these is editable, though you'll mainly be editing the code cells to answer some questions.

To get started, try running the code cell below (by pressing the ► button, or clicking on the cell and pressing ctrl+enter on your keyboard).

Try adding another line of code in the cell above and re-running it.

Now let's get a little fancier: Add a new code cell by clicking on an existing code cell, hitting the escape key, and then hitting the a or b key. The a key will add a cell above the current cell, and b adds a cell below.

Great! Now you know how to use Notebooks.

Each hands-on exercise starts by setting up our feedback and code checking mechanism. Run the code cell below to do that. Then you'll be ready to move on to question 0.

0.

This is a silly question intended as an introduction to the format we use for hands-on exercises throughout all Kaggle courses.

What is your favorite color?

To complete this question, create a variable called color in the cell below with an appropriate value. The function call q0.check() (which we've already provided in the cell below) will check your answer.

Didn't get the right answer? How do you not even know your own favorite color?!

Delete the # in the line below to make one of the lines run. You can choose between getting a hint or the full answer by choosing which line to remove the # from.

Removing the # is called uncommenting, because it changes that line from a "comment" which Python doesn't run to code, which Python does run.

The upcoming questions work the same way. The only thing that will change are the question numbers. For the next question, you'll call q1.check(), q1.hint(), q1.solution(), for question 2, you'll call q2.check(), and so on.


1.

Complete the code below. In case it's helpful, here is the table of available arithmetic operations:

Operator Name Description
a + b Addition Sum of a and b
a - b Subtraction Difference of a and b
a * b Multiplication Product of a and b
a / b True division Quotient of a and b
a // b Floor division Quotient of a and b, removing fractional parts
a % b Modulus Integer remainder after division of a by b
a ** b Exponentiation a raised to the power of b
-a Negation The negative of a


2.

Add code to the following cell to swap variables a and b (so that a refers to the object previously referred to by b and vice versa).


3.

a) Add parentheses to the following expression so that it evaluates to 1.

Questions, like this one, marked a spicy pepper are a bit harder.

b) 🌶️ Add parentheses to the following expression so that it evaluates to 0


4.

Alice, Bob and Carol have agreed to pool their Halloween candy and split it evenly among themselves. For the sake of their friendship, any candies left over will be smashed. For example, if they collectively bring home 91 candies, they'll take 30 each and smash 1.

Write an arithmetic expression below to calculate how many candies they must smash for a given haul.

Keep Going

Next up, you'll learn to write new functions and understand functions others write. This will make you at least 10 times more productive as a Python programmer.


Have questions or comments? Visit the Learn Discussion forum to chat with other Learners.