Python includes three numeric types to represent numbers: integers, floating point numbers, and complex numbers.
Sometimes we need to convert a number from one type to another to satisfy the requirements of an operator or a function.
In this section, we’ll take a look at how to do basic arithmetic operations.
We can do simple calculations with Python by entering a calculation directly into the Python console.
|x + y||sum of x and y|
|x - y||difference of x and y|
|x * y||product of x and y|
|x / y||quotient of x and y|
|x // y||floored quotient of x and y|
|x % y||remainder of x / y|
|pow(x, y)||x to the power y|
|x ** y||x to the power y|
Below are more examples.
We can use the + operator to add 2 numbers. The numbers don’t have to be the same type.
>>> 2 + 3.0 5.0
Adding two integers together always results in an int. If one of the operands is a float, the result is also a float.
To subtract two numbers, we use a - operator between them:
>>> 5 - 2 3 >>> 9.0 - 4 5.0
Simmilar to adding 2 numbers, substracting two integers together always results in an int. If one of the operands is a float, the result is also a float.
The minus sign indicates a negative number.
>>> -10 -10
We can multiply numbers in Python using an asterisk * operator.
Multiplying two integers will result in an int, and multiplying a number with a float results in a float.
The parentheses determine which operations are performed first.
>>> 2 * (3 + 4) 14
We use a forward slash / to indicate division. Division with the / operator always returns a float.
>>> 4 / 2 2.0
We can use int() to convert the result to interger:
>>> int(9 / 3) 3
If you try to divide by zero, Python produces an error.
>>> 10/0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: division by zero
Python has an integer division operator (//), also known as the floor division operator. It first divides the number and then rounds down to an integer.
If one of the operands is a float, the result returns a float.
>>> 4 // 2 2 >>> 3 // 2 1 >>> 5.0 // 2 2.0
Besides addition, subtraction, multiplication, and division, we can also use exponentiation in Python.
The ** operator in Python is used to raise the number on the left to the power of the exponent of the right.
>>> 2**3 8 >>> 2 ** -1 0.5
To calculate remainder, we use % operator
>>> 6 % 4 2
You can use the modulo function (%) with 10 to find the ones’ place of an integer.
>>> 2%10 2
In-place operators allow you to shorten code from
x = x + 5 to
x += 3. We can apply with other operators such as -, *, / and % as well.
These operators can be used on types other than numbers such as strings.
>>> x = "Hello" >>> print(x) Hello >>> x += " World" >>> print(x) Hello World
Python provides an extensive list of built-in functions that wecan use to work with numbers. Here are some common functions:
|abs(x)||Absolute value of x|
|ceil(x)||Smallest integer that is not less than x|
|floor(x)||Largest integer that is not more than x|
|log(x)||lnx, with x> 0|
|log10(x)||log10(x), với x> 0 .|
|max(x1, x2,…)||max number|
|min(x1, x2,…)||min number|
|round(x, n)||rounding numbers to some number of decimal places|
|sqrt(x)||square root of x, where x > 0|
|pow(x, y)||x to the power y|
|abs()||the absolute value of a number|
We use round() to round a number to the nearest integer:
>>> round(5.2) 5 >>> round(5.8) 6
We can also round a number to a number of decimal places by passing a second argument to round():
>>> round(5.23456, 3) 5.235 >>> round(8.82132, 2) 8.82
We use min() and max() to find min and max.
>>> min(2, 5) 2 max (4,5,6) 6
To find the absolute value of a number in Python, we use abs():
>>> abs(3) 3 >>> abs(-5.0) 5.0
abs() function always return a positive number of the same type as its argument. So a the absolute value of an integer is a positive, and float is a positive float.
random.randint(a,b) will return an integer between a and b (inclusive).
random.randint(5, 10) could return any integer between 5 and 10 including both 5 and 10. We need to
import random to use this method.
import random num = random.randint(5, 10) print(num)