Essential Knowledge 1.7.1 1.7.A.1.
Apps use mathematical expressions to calculate values that help users make decisions, such as prices, time estimates, or progress toward a goal.
width*height below. 
score = 0, but the right side of an assignment statement can be more than just a simple value like 0. We can use expressions which combine numbers, variables, and operators to calculate a value and assign it to the variable in an assignment statement like below.

score = score + 1) as shown below. The formula would look strange in math class, but it makes sense in coding because it is assigning a new value to the variable on the left that comes from evaluating the arithmetic expression on the right. So, the score variable is set to the previous value of score plus 1.
score = 5 are automatically type int for integer.
+, subtraction -, multiplication *, division / and modulo %.
| Operator | Meaning |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| Operator | Meaning |
|---|---|
| // | Integer Division (everything after the decimal point is removed) |
| % | Modulo (integer remainder after integer division) |
| ** | Exponent (raises a number to a power) |
% evaluates to the remainder when one integer is divided by another: a % b is the remainder when a is divided by b. Modulo can be used to identify patterns in numbers, such as whether a number is even or odd, and to help format values in user-friendly ways, such as converting total minutes into hours and minutes.
5 % 2 is equal to 1, because 5 divided by 2 is equal to 2 with a remainder of 1 (since 2 * 2 = 4).
2 % 6?
6 % 2.*, division /, and modulo % operations are evaluated from left to right. Then, addition + and subtraction - operations are evaluated from left to right.
1 + 3 * (2 + 1)?
+=, -=, *=, /=, and %= are shortcuts that do a math operation and assignment in one step. For example, x += 1 adds 1 to the current value of x and assigns the result back to x. It is the same as x = x + 1. This pattern is possible with any operator put in front of the = sign.
| Operator | Example | Equivalent to |
|---|---|---|
| += | x += 1 | x = x + 1 |
| -= | x -= 1 | x = x - 1 |
| *= | x *= 2 | x = x * 2 |
| /= | x /= 2 | x = x / 2 |
| %= | x %= 2 | x = x % 2 |
print statements to show the variables or click on the "Show CodeLens" button on any Active Code exercise to turn on the interactive tracing tool called CodeLens as seen below. This is a tool called Python Tutor that is built into Runestone.
a = 2
b = 10
a = b
a = a + 1
b = b * 3
input() command to get input from a user, the value is always treated as a string, even if the user types in a number. If you want to use that input in a mathematical expression, you need to convert it to a number first using the int() command for integers or float() command for decimal numbers. For example:
ageStr = input("How old are you? ")
age = int(ageStr)
# Now we can do math with the age variable since it is an int instead of a string
print("Next year you will be", age + 1, "years old.")
# Get the age from the user and convert it to an int in one line
age = int(input("How old are you? "))
round(num, 2) function. This function rounds the number to 2 decimal places, for example 2.555555 will be rounded to 2.56. Another option is to use format(num, ".2f").
# rounding a variable to 2 float or decimal places
cost = 10.666666
print("Cost is $", round(cost, 2))
% can be used to calculate the remaining minutes after the hours are calculated. For example, if there are 150 minutes, then that is 2 hours and a remainder of 30 minutes.
// in Python is integer division that returns a whole number, / is float division, and % is the remainder operator. Drag the blocks from the left and put them in the correct order on the right. There will be 2 extra blocks that you will not use. Click the Check Me button to check your solution.
a to b, including a and b, is generated and returned. Each result is equally likely to occur. The possible results of random(a, b) are all integers from a through b.
random that can be imported and used to generate random values. Use random.randint(start, end) to generate a random integer from start to end, including both endpoints. For example:
import random
# pick a random number from 0 to 10
r = random.randint(0, 10)

x = random.randint(-200, 200) can be used to generate a random x-coordinate for a turtle, because it is initially placed at coordinates (0, 0) and can move 200 pixels to the right to 200 or left to -200. Which of the following numbers cannot be generated by this expression?