You are on page 1of 11

Conquer

Python

Cheat Sheet: Numbers


Numbers
Cheat Sheet

There are two main types of numbers in Python:

1. Integer
a. A whole number with no decimal places
2. Float
a. A number with decimal places
Numbers: Arithmetic Operators
Cheat Sheet

+ Addition

Integer + Integer:
>>> 1 + 1
2
>>> 3 + 4
7

Integer + Float:
>>> 1 + 1.0
2.0
>>> 9 + 2.7
11.7

Float + Float:
>>> 1.0 + 1.0
2.0
>>> 3.0 + 4.0
7.0

- Subtraction

Integer - Integer:
>>> 8 - 2
6
>>> 53 - 12
41

Integer - Float:
>>> 3 - 2.0
1.0
>>> 9 - 8.7
0.3

Float - Float:
>>> 8.0 - 2.0
6.0
>>> 53.0 - 12.0
41.0
Numbers: Arithmetic Operators
Cheat Sheet

* Multiplication

Integer * Integer:
>>> 10 * 10
100
>>> 8 * 4
32

Integer * Float:
>>> 2 * 3.0
6.0
>>> 7 * 3.7
25.9

Float * Float:
>>> 10.0 * 10.0
100.0
>>> 8.0 * 4.0
32.0
Numbers: Arithmetic Operators
Cheat Sheet

/ Division

Integer / Integer:
>>> 20 / 10
2.0
>>> 8 / 5
1.6

Integer * Float:
>>> 9 / 3.0
3.0
>>> 8 / 2.7
2.96296296

Float / Float:
>>> 20.0 / 10.0
2.0
>>> 8.0 / 5.0
1.6
Numbers: Arithmetic Operators
Cheat Sheet

// Integer Division

The // operator first divides the number on the left by the


number on the right and then rounds down to an integer. This
might not give the value you expect when one of the numbers
is negative.

Integer // Integer:
>>> 20 // 10
2
>>> 8 // 5
1
>>> -3 // 2
-2

Integer // Float:
>>> 9 // 3.0
3.0
>>> 8 // 2.7
2.0
>>> -12 // 4.0
-3.0

Float // Float:
>>> 20.0 // 10.0
2.0
>>> 8.0 // 5.0
1.0
Numbers: Arithmetic Operators
Cheat Sheet

** You can raise a number to an exponent with the ** operator.

Integer ** Integer:
>>> 2**2
4
>>> 2**3
8

Integer ** Float:
>>> 3**2.0
9.0
>>> 3**3.0
27.0

Float ** Integer:
>>> 3.0**2
9.0
>>> 3.0**3
27.0

Float ** Float:
>>> 4.0**2.0
16.0
>>> 4.0**3.0
64.0
Numbers: Arithmetic Operators
Cheat Sheet

% Modulus Operator

The % operator, or the modulus, returns the remainder of


dividing the left operand by the right operand

Integer % Integer:
>>> 5 % 2
1
>>> 7 % 3
1
>>> -5 % 2
1

Integer % Float:
>>> 11 % 3.0
2.0
>>> 3 % 3.0
0.0
>>> 11 % -2
-1

Float % Integer:
>>> 11.0 % 3
2.0
>>> 3.0 % 3
0.0
>>> -5.0 % -3.0
-2.0

Float % Float:
>>> 19.0 % 5.0
4.0
>>> 26.0**7.0
5.0
Numbers: Arithmetic Expressions
Cheat Sheet

PEMDAS Order of Operations

P - Parenthesis
E - Exponents
M - Multiplication
D - Division
A - Addition
S - Subtraction

>>> 5/2 * (3 + 2) + 2**2


16.5
>>> -1 + (-5.0 + 4)*7
-8.0
Numbers: Functions
Cheat Sheet

round() You can use round() to round a number to the nearest


integer. There are two different ways to use the round():
1. Single argument - this is where you simply enter an
integer into the function where, by default, it rounds the
number to the nearest integer.
2. Two argument - this is where you enter a second
argument that indicates the number of decimals that
you would like to round to.

Single Argument:

>>> round(2.4)
2
>>> round(5.6)
6

Two Argument:

>>> round(5.68434,3)
5.684
>>> round(20.183649,5)
20.18365

abs() You can use abs() to find the absolute value of a number;
integer or float, positive or negative.

>>> abs(-4)
4
>>> abs(-9.84)
9.84
Numbers: Functions
Cheat Sheet

pow() Earlier we covered the ** arithmetic operator. The pow()


function allows you to do the same thing. The pow() function
takes two arguments; the first is the base and the second is
the exponent.

>>> pow(2,2)
4
>>> pow(5,3)
125
>>> pow(2,-2)
0.25

int() Allows you to turn a non-integer data type such as a float or a


string to an integer.

>>> int('56')
56
>>> int(56.8)
56

float() Allows you to turn a non-float data type such as an integer or


a string to an float.

>>> float('44')
44.0
>>> float(83)
83.0

You might also like