Numbers and math
Numbers are different from text: they’re written without quotes. When you leave the quotes off, Python actually does the math:
Inputs (for input())
One value per line — read in order by input().
See that? 5 + 3 was calculated and came out as 8, while "5 + 3" was printed as plain text. Quotes = text, no quotes = number.
Math operations
Section titled “Math operations”| Sign | Operation | Example | Result |
|---|---|---|---|
+ | addition | 7 + 2 | 9 |
- | subtraction | 7 - 2 | 5 |
* | multiplication | 7 * 2 | 14 |
/ | division | 7 / 2 | 3.5 |
// | whole division | 7 // 2 | 3 |
% | remainder | 7 % 2 | 1 |
** | power | 7 ** 2 | 49 |
Here’s how to remember the // and % pair: share 7 candies among 2 kids — each one gets 7 // 2 = 3 candies, and 7 % 2 = 1 candy is left over. 🍬
Python — a super-calculator
Section titled “Python — a super-calculator”How many seconds are there in a day? Easy:
Inputs (for input())
One value per line — read in order by input().
Look at the second line: 2 to the power of 100 — an ordinary calculator can’t handle it, but Python doesn’t even blink. 😎
Missions
Section titled “Missions”How many hours in a year?
+10 XP ✓ CompletedHave Python calculate how many hours are in a year. (Don’t type the answer yourself — use multiplication!)
Inputs (for input())
One value per line — read in order by input().
Sharing apples
+15 XP ✓ Completed17 apples, 5 kids. Use // and % to work out: how many apples does each kid get, and how many are left over?
Inputs (for input())
One value per line — read in order by input().
The power of powers
+10 XP ✓ CompletedThere’s a famous number in the world of computers: 2 to the power of 10. Have Python calculate it and see the result.
Inputs (for input())
One value per line — read in order by input().
Next lesson: variables — boxes for storing information. One of the most important ideas in all of programming. Continue →