Use Python as a powerful calculator to perform arithmetic operations.
What if you could build a video game that automatically calculates a player's score, levels them up every 100 points, and determines if they have enough 'mana' for a spell—all in a fraction of a second?
In Python, math works almost exactly like it does in your notebook, but with a few special symbols. We use `+` for addition, `-` for subtraction, `*` for multiplication, and `/` for division. One important thing to remember: in Python, division always results in a float (a number with a decimal point), even if the numbers divide perfectly. For example, will give you . This ensures that Python stays precise when dealing with fractions!
Let's calculate the cost of some snacks: 1. Addition: 2. Subtraction: 3. Multiplication: 4. Division:
Quick Check
What is the result of in Python?
Answer
3.0
The modulo operator uses the `%` symbol. Instead of giving you the result of a division, it gives you the remainder—the 'leftover' part. Think of it like sharing cookies: if you have 7 cookies and 3 friends, everyone gets 2 cookies, and there is cookie left over. In Python, equals . This is incredibly useful for checking if a number is even or odd (if is , it's even) or for creating cycles in games.
You have 22 slices of pizza and 5 people. How many slices are left over if everyone eats the same amount? 1. Set up the expression: 2. Calculate how many times 5 goes into 22: 3. Find the difference: 4. The result is .
Quick Check
What is the result of ?
Answer
1
Python follows the standard mathematical Order of Operations, often remembered by the acronym PEMDAS. This stands for Parentheses, Exponents, Multiplication and Division (left to right), and Addition and Subtraction (left to right). In Python, we use `**` for exponents (like which is `2 3`). If you want to make sure an addition happens before a multiplication, you must wrap it in parentheses** `()`.
Solve the expression: 1. Parentheses first: 2. Exponents next: 3. Multiplication/Division (left to right): 4. Final Division:
Which operator would you use to find the remainder of ?
What is the result of ?
In Python, the expression results in the integer 8.
Review Tomorrow
In 24 hours, try to explain to a friend why is different from in Python.
Practice Activity
Open a Python editor and try to calculate how many minutes are in a week using only math operators!