Arithmetic operators
Two operators divide, and they give different answers
/ gives a float. // rounds down. % is the remainder.
Python has seven arithmetic operators. Add, subtract and multiply hold no surprises. The rest do. A single slash is true division, and the result is always a float. A double slash is floor division: it rounds down. Percent gives the remainder. Rounding down a negative goes away from zero: minus seven, floor divided by two, is minus four. Two stars is power. And if either side is a float, so is the result.
Arithmetic operators · Going deeper
Result types, and strings
A float spreads. + joins strings, * repeats them.
Precedence
Power first, then multiply and divide, then add
**, then * / // %, then + -.
When an expression has several operators, Python does not go left to right. Take two plus three times four to the power two. The power has the highest rank, so four squared is done first: sixteen. Multiplication is next: three times sixteen, forty eight. Addition is last: fifty. Left to right would have given four hundred.
Precedence · Going deeper · 1 of 2
The full ranking
** binds right to left. not, and, or come last.
Precedence · Going deeper · 2 of 2
Augmented assignment
x += 3 means x = x + 3.
Check yourself
What does this print?
Comparison and Boolean operators
A comparison gives True or False. and, or, not combine.
and: both. or: at least one. not: the opposite.
A comparison such as age greater or equal to eighteen has a value: True or False. Three operators combine those values. Take age at least eighteen, and has I D. The comparison is worked out first: True and False. And needs both, so the result is False. Now the same with or. True or False. Or needs only one, so the result is True.
Comparison and Boolean operators · Going deeper
Comparisons and the truth table
== compares. A str never equals a number.
Console input
input() always returns a string
input() gives a str. Wrap it in int().
Input reads a line from the keyboard. Whatever the user types, it comes back as a string. So if they type twenty, age is the text two nought, and adding one to text is a type error. Wrap the input in int, and age is the number twenty. Now adding one gives twenty one.
Console input · Going deeper
What print() prints
A space between, a new line after.
Check yourself
The user types 5. What is printed?
Type conversion
int() cuts. It does not round.
int(), float(), str(). Bad text: ValueError.
Three functions convert between the basic types. Int of four point nine is four: the fraction is cut off, not rounded. Int also reads text, if the text is a whole number. Text with a decimal point is refused, with a value error. Float accepts both. And str turns a number into text, which can then be joined to other text.
Type conversion · Going deeper
What converts to what
What cannot convert raises ValueError.
Check yourself
What does this print?
Lesson complete
You finished “Operators, input and output”
Answer the questions along the way to see your score here.