Built-ins and the math module
Compare floor, ceil, trunc and round for -2.5 on line 4.
Python follows a fixed order of operations (precedence): powers first, then multiplication and division, then addition and subtraction. Use parentheses to be clear, use += and friends to update a variable, and reach for the math module and built-ins like abs, min and max for more.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Compute and print the average of the marks 72, 88 and 95, rounded to 2 decimal places.
Start with count = 3, then use augmented assignment to double it and then add 10. Print count.
Print the longest side (the hypotenuse) of a right triangle with sides 6 and 8, using the math module.
Explain it without notes
State the order of operations for +, -, *, /, //, % and ** in Python.
Why is -3 ** 2 equal to -9, and what is 2 ** 3 ** 2?
What does x += 1 do, and when does it fail?
How do floor, ceil, trunc and round differ on -2.5?
Expected output
4.0 3.141592653589793 2.718281828459045
-3 -2 -2 -2
2 3
7 9 3 1024 0.5
6 120 5.0
10.0 3.0
1.4142135623730951 1.4142135623730951