Written by PIRATE KING
Let's explore how Python works with numbers. In this chapter, you'll learn about arithmetic and assignment operators, how to round numbers, find absolute values, and use powerful math functions from Python's math module.
Arithmetic operators are used for mathematical operations. Python has seven: addition +, subtraction -, an asterisk * to denote multiplication, division /, floor division // (quotient), modulus % (remainder), and exponentiation **. Run the code below to see how they work.
Addition, subtraction, and multiplication are straightforward. Let's focus on the rest. Python has two division types: single slash / for floating-point division and double slash // for floor division. Floor division drops the decimal, keeping only the whole number part. For example, 10 / 2 gives 5.0 (a float), while 10 // 2 gives 5 (an integer). The modulus operator % gives the remainder. Since 10 divided by 2 has no remainder, 10 % 2 returns 0. Exponentiation uses **, so 10 ** 2 or 10 to the power of 2 equals 100. Here are some more examples to help you understand better.
+ Addition- Subtraction* Multiplication/ Division (floating point)// Floor Division (integer)% Modulus (remainder)** ExponentiationAssignment operators set values to variables, like the equal sign =. Augmented assignment operators combine an arithmetic operation and assignment in one step; they let you change a variable's value by performing an operation and assigning the result back to the same variable. For example, price += 10 and price = price + 10 are equivalent. Let's execute this.
Both print 20, but the first combines assignment and arithmetic += to make the code shorter and clearer. Augmented assignment operators work with all arithmetic operators like -=, *=, /=, etc. But += is special because it can also concatenate and assign strings. For example, if you have a variable sport set to "foot", doing sport += "ball" results in football.
There you go. We have football.
= Assignment+= Add and assign-= Subtract and assign*= Multiply and assign/= Divide and assign//= Floor divide and assign%= Modulus and assign**= Exponentiate and assignround() is super handy when you want to control how many decimal places a number shows. By default, it rounds to the nearest integer, but you can pass a second argument for decimal precision. For example,
round(pi) yields 3, round(pi, 2) yields 3.14, and round(pi, 3) yields 3.142.
abs() gives you the absolute value — basically, it turns any negative number into positive.
This one's useful when you care about distance or magnitude of a value.
A module in Python is just a file full of pre-written code you can reuse. It's how Python keeps things organized and lets you tap into extra features without reinventing the wheel. You use the import keyword to bring in a module.
The math module gives you access to tons of mathematical functions — from square roots to trigonometry. Let's explore a few key ones.
First, use import math to access the math module. Then call its methods using math. followed by the function name. For example, math.ceil() rounds a number up, while math.floor() rounds it down.
math.ceil() rounded pi up to 4, and math.floor() rounded pi down to 3. They're especially useful when you're working with prices, coordinates, or ranges where you need clean integers.
math.pow(x, y) raises a number x to a power of y while math.sqrt(x) finds the square root.
For example, 2 to the power of 3 is 8 while taking the square root of 16 yields 4. Notice these return floats, even if the result is a whole number.
1. What is the result of the expression 19 % 4 in Python?
2. Which of the following are valid augmented assignment operators in Python? (Select all that apply)
3. What does math.ceil(4.2) return?
4. Write Python code to calculate and print:
Expected output:
6 81 12.5
5. Using the math module, write Python code to:
Expected output:
7.0 32.0 6