Open In Colab

Exercises

Welcome to your first set of Python coding problems!

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).

print("You've successfully run some Python code")
print("Congratulations!")
You've successfully run some Python code
Congratulations!

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 *

**What is your favorite color? **

To complete this question, create a variable called color in the cell below with an appropriate value.

# create a variable called color with an appropriate value on the line below
# (Remember, strings in Python must be enclosed in 'single' or "double" quotes)
____
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-66c2cb3ff345> in <module>
      1 # create a variable called color with an appropriate value on the line below
      2 # (Remember, strings in Python must be enclosed in 'single' or "double" quotes)
----> 3 ____

NameError: name '____' is not defined

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

pi = 3.14159 # approximate
diameter = 3

# Create a variable called 'radius' equal to half the diameter
____

# Create a variable called 'area', using the formula for the area of a circle: pi times the radius squared
____

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).

########### Setup code - don't touch this part ######################
# If you're curious, these are examples of lists. We'll talk about 
# them in depth a few lessons from now. For now, just know that they're
# yet another type of Python object, like int or float.
a = [1, 2, 3]
b = [3, 2, 1]

######################################################################

# Your code goes here. Swap the values to which a and b refer.


######################################################################

3.

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

5 - 3 // 2

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

8 - 3 * 2 - 1 + 1

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.

# Variables representing the number of candies collected by alice, bob, and carol
alice_candies = 121
bob_candies = 77
carol_candies = 109

# Your code goes here! Replace the right-hand side of this assignment with an expression
# involving alice_candies, bob_candies, and carol_candies
to_smash = (replace this)

#That is all folks.

Be sure to submit this assignment by clicking share then copy link. Link should be pasted on your assignment page in classroom.