Introduction

We’ll start with a brief overview of Python syntax, variable assignment, and arithmetic operators. If you have previous Python experience, you can skip straight to the hands-on exercise.

Hello, Python!

Python was named for the British comedy troupe Monty Python, so we’ll make our first Python program an homage to their skit about Spam?

Just for fun, try reading over the code below and predicting what it’s going to do when run. (If you have no idea, that’s fine!)

Then click the “output” button to see the results of our program.

1
2
3
4
5
6
7
8
9
10
11
spam_amount = 0
print(spam_amount)

# Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam)
spam_amount = spam_amount + 4

if spam_amount > 0:
print("But I don't want ANY spam!")

viking_song = "Spam " * spam_amount
print(viking_song)

0
But I don’t want ANY spam!
Spam Spam Spam Spam

Variable Assignment

Here we create a variable called spam_amount and assign it the value of 0 using =, which is called the assignment operator.

Aside: If you’ve programmed in certain other languages (like Java or C++), you might be noticing some things Python doesn’t require us to do here:

  • we don’t need to “declare” spam_amount before assigning to it
  • we don’t need to tell Python what type of value spam_amount is going to refer to. In fact, we can even go on to reassign spam_amount to refer to a different sort of thing like a string or a boolean.

Function Calls

print is a Python function that displays the value passed to it on the screen. We call functions by putting parentheses after their name, and putting the inputs (or arguments) to the function in those parentheses.

1
2
# Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam)
spam_amount = spam_amount + 4

The first line above is a comment. In Python, comments begin with the # symbol.

Next we see an example of reassignment. Reassigning the value of an existing variable looks just the same as creating a variable - it still uses the = assignment operator.

In this case, the value we’re assigning to spam_amount involves some simple arithmetic on its previous value. When it encounters this line, Python evaluates the expression on the right-hand-side of the = (0 + 4 = 4), and then assigns that value to the variable on the left-hand-side.

1
2
3
4
5
if spam_amount > 0:
print("But I don't want ANY spam!")

viking_song = "Spam Spam Spam"
print(viking_song)
1
2
But I don't want ANY spam!
Spam Spam Spam

Numbers and arithmetic in Python

1
spam_amount = 0

“Number” is a fine informal name for the kind of thing, but if we wanted to be more technical, we could ask Python how it would describe the type of thing that spam_amount is:

1
type(spam_amount)
1
int

It’s an int - short for integer. There’s another sort of number we commonly encounter in Python:

1
type(19.95)
1
float

A float is a number with a decimal place - very useful for representing things like weights or proportions.

type() is the second built-in function we’ve seen (after print()), and it’s another good one to remember. It’s very useful to be able to ask Python “what kind of thing is this?”.

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
1
2
print(5 // 2)
print(6 // 2)
1
2
2
3

Order of operations

The arithmetic we learned in primary school has conventions about the order in which operations are evaluated. Some remember these by a mnemonic such as PEMDAS - Parentheses, Exponents, Multiplication/Division, Addition/Subtraction.

Python follows similar rules about which calculations to perform first. They’re mostly pretty intuitive.

Built-in functions for working with numbers

Min & Max

min and max return the minimum and maximum of their arguments, respectively…

1
2
print(min(1, 2, 3))
print(max(1, 2, 3))
1
2
1
3

ABS

1
2
print(abs(32))
print(abs(-32))
1
2
32
32

Exercise: Syntax, Variables, Numbers

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.

1
2
3
4
5
6
7
# 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)
____
color = "black"

# Check your answer
q0.check()

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

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

1
(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.

1
2
3
4
5
6
7
8
9
10
11
12
# 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 = -1


# Check your answer
q4.check()
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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
total_candies = alice_candies + bob_candies + carol_candies
to_smash = total_candies % 3


# Check your answer
q4.check()

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.