Comments, Arithmetic, and String Concatenation
Comments
Comments are notes for people reading the code. Python ignores them. A comment
begins with #. It can be on its own line or on the same line as other code.
Either way, Python ignores everything from the # to the end of the line.
Write comments that explain what is not obvious, such as the purpose of a section, the units of a variable, or the reason for a design choice. You do not need to comment every line, but remember that code that is clear to you now may not be clear to other people, or to you in a few months.
Arithmetic
Addition, subtraction, multiplication and division are done with the +, -,
*, and / symbols.
Floor division uses the // symbol. It divides and rounds the result down to a
whole number.
Normal division always gives a float, even when the result is a whole number.
For example, 8 / 4 gives 2.0. If you want a whole-number result, use floor
division.
The mod operation uses the % symbol. It gives you the remainder of a
division.
Check your understanding. Predict the result. Think about the type each kind of division gives back.
x: float = 8 / 4
y: int = -9 // 2
Predict what each variable holds after the program finishes. The program is not run for you. Replace each None in the box below with your prediction, and give each variable a type hint, like name: type = value. Then press Run to find out if you are right. Write text in quotes, like "abc".
final_xis the value ofxat the endfinal_yis the value ofyat the end
Hint 1
Normal division gives a float even for whole results. Rounding down on a negative number goes away from zero.
Exponentiation is done with the ** symbol.
The order of operations is exponentiation first, then multiplication, division, floor division, and mod, then addition and subtraction. Parentheses let you choose a different order, since operations in parentheses happen first.
The += operator is a shorter way to add something to an existing value. The
following statements do exactly the same thing:
You can combine = with each of the operators above to get more shortcut
operators. Run the cells below and read the printed values.
The += operator is the most common of these, since it is handy for keeping a
running total.
You can get the absolute value of a number like this:
Here the variable answer is assigned the value 12.
You can round a number with round(). Give it the number and how many decimal
places to keep. If you leave out the second value, you get a whole number.
Concatenation
The + operator also works on strings. Concatenating strings joins them end to
end.
Explanation
The hint goes after the colon, once, where the variable first gets a value.
Quizzes
- Write a statement that prints out the remainder of 17 divided by 3.
- Write a statement that prints out 3 to the 4th power.
- Write a statement that finds the result of 17 divided by 6 and assigns it to a variable named
conversion_ratio.
Hint 1
Python has two kinds of division. Which one keeps the decimal part? The lesson shows both.
- Write code that first assigns 9 to a variable called
num_planets, then subtracts 1 from it, so thatnum_planetswill now equal 8.
- Write code that assigns
"Doctor"to a variable namedtitle, assigns"Strange"to a variable calledlast_name, and then uses string concatenation to print out"Doctor Strange".
- Predict the result. Follow x one line at a time, then work out y by order of operations.
x: float = 20
x //= 3
x %= 4
x **= 3
x /= 4
y: int = 2 + 3 * 4 ** 2 // 5
Predict what each variable holds after the program finishes. The program is not run for you. Change the values below to your predictions:
final_xis the value ofxat the endfinal_yis the value ofyat the end
Hint 1
Track x after each line. Only the last step uses /.