Uploaded by Alfredo A

1-operators

advertisement
COMP 1012
The Basics
Robert Guderian
/
1
Operators
We know Python can do the basics
Addition and subtraction, multiplication
and division….
1
2
2
4
+
*
/
2
1
2
2
what datatypes are returned?
what about with 1.2 - 1?
/
2
Arithmetic return types
Math between ints → int
Except division
(there’s always an except)
If there’s a float in the calculation → float
/
3
Other operators
What about the weird ones?
// Integer division (floor divide)
What does 5//2 return?
% Modulus operator
Returns the remainder of the division
Think about old long division
5 / 2 → 2, remainder 1
this returns 1 (the remainder)
/
4
Integer arithmetic
\(2\overline{)5}\)
Think back to long division
5 // 2 = 2
5%2=1
Then, the inverse:
(denominator) * (quotient) + (mod) =
(enumerator)
2*2+1=5
/
5
Exponents
Easy syntax
5 ** 2
(Or pow; know both, use your favourite)
/
6
BEDMAS
Yep, Python does what you think it’d do
5 + 6/2 → 5 + (6/2) → 5 + 3
Use brackets liberally!
/
7
Summary
It’s math! It does math things!
Don’t be a hero, and use lots of brackets!
/
8
/
9
Download