Python: Hello 你好
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 | spam_amount = 0 |
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 | # Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam) |
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 | if spam_amount > 0: |
1 | But I don't want ANY 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 | print(5 // 2) |
1 | 2 |
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 | print(min(1, 2, 3)) |
1 | 1 |
ABS
1 | print(abs(32)) |
1 | 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 | # create a variable called color with an appropriate value on the line below |
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 | # Variables representing the number of candies collected by alice, bob, and carol |
1 | # Variables representing the number of candies collected by alice, bob, and carol |
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.