Author: SUNNY BHAVEEN CHANDRA
For more information -
[1] Python notes- https://c17hawke.github.io/Python/
[2] Python YouTube Playlist- https://youtube.com/playlist?list=PLrdaCCBhU_hnxIzB7EJlY-pfYOMGRycAK
Numbers in Python 🔢¶
Python supports several types of numbers, including integers, floating point numbers, and complex numbers.
Integers 🔢¶
Integers are whole numbers that can be positive, negative, or zero. For example:
x = 4
y = 0
z = -3
print(f"Value: x={x} | type: {type(x)}")
print(f"Value: y={y} | type: {type(y)}")
print(f"Value: z={z} | type: {type(z)}")
Value: x=4 | type: <class 'int'> Value: y=0 | type: <class 'int'> Value: z=-3 | type: <class 'int'>
Large integers 🔍-¶
eg currency 10,000,000 10Mn dollars
x = 10000000
y = 10_000_000 # 10,000,000
z = 10,000,000 # avoid this
print(f"Value: x={x} | type: {type(x)}")
print(f"Value: y={y} | type: {type(y)}")
print(f"Value: z={z} | type: {type(z)}")
Value: x=10000000 | type: <class 'int'> Value: y=10000000 | type: <class 'int'> Value: z=(10, 0, 0) | type: <class 'tuple'>
Floats 🔟¶
Floats are numbers with a decimal point.
x = 4.0
y = 0.0
z = -3.3
print(f"Value: x={x} | type: {type(x)}")
print(f"Value: y={y} | type: {type(y)}")
print(f"Value: z={z} | type: {type(z)}")
Value: x=4.0 | type: <class 'float'> Value: y=0.0 | type: <class 'float'> Value: z=-3.3 | type: <class 'float'>
Large float values 🔍¶
x = 10000000.00
y = 10_000_000.00
z = 10,000,000.00 # avoid this as it returns tuple
print(f"Value: x={x} | type: {type(x)}")
print(f"Value: y={y} | type: {type(y)}")
print(f"Value: z={z} | type: {type(z)}")
Value: x=10000000.0 | type: <class 'float'> Value: y=10000000.0 | type: <class 'float'> Value: z=(10, 0, 0.0) | type: <class 'tuple'>
They can also be written in scientific notation
Scientific Notation 🔬¶
Numbers can also be represented in scientific notation using the e
or E
character to indicate the power of 10. For example, 1e3
represents 1 x 10^3
, which is equivalent to 1000
. Here's an example of using scientific notation in Python:
# - scientific notation x >> 1 and x >= 0 (positive)
# eg: distance between earth and sun
x = 12_000.00
y = 1.2 * 10 ** 4
z = 1.2e4
print(f"Value: x={x} | type: {type(x)}")
print(f"Value: y={y} | type: {type(y)}")
print(f"Value: z={z} | type: {type(z)}")
Value: x=12000.0 | type: <class 'float'> Value: y=12000.0 | type: <class 'float'> Value: z=12000.0 | type: <class 'float'>
x = 12_000
y = int(1.2 * 10 ** 4)
z = int(1.2e4)
print(f"Value: x={x} | type: {type(x)}")
print(f"Value: y={y} | type: {type(y)}")
print(f"Value: z={z} | type: {type(z)}")
Value: x=12000 | type: <class 'int'> Value: y=12000 | type: <class 'int'> Value: z=12000 | type: <class 'int'>
# - scientific notation x << 1 and x >= 0 (positive)
# eg: radius of an Hydrogen atom
x = 0.00012
y = 1.2 * 10 ** -4
z = 1.2e-4
print(f"Value: x={x} | type: {type(x)}")
print(f"Value: y={y} | type: {type(y)}")
print(f"Value: z={z} | type: {type(z)}")
Value: x=0.00012 | type: <class 'float'> Value: y=0.00012 | type: <class 'float'> Value: z=0.00012 | type: <class 'float'>
x = 0.00012
y = 1.2 * 10 ** -4
z = 1.2E-4
print(f"Value: x={x} | type: {type(x)}")
print(f"Value: y={y} | type: {type(y)}")
print(f"Value: z={z} | type: {type(z)}")
Value: x=0.00012 | type: <class 'float'> Value: y=0.00012 | type: <class 'float'> Value: z=0.00012 | type: <class 'float'>
x = 1e3 # equivalent to 1000
y = 1.5e2 # equivalent to 150
z = 3E-2 # equivalent to 0.03
print(f"Value: x={x} | type: {type(x)}")
print(f"Value: y={y} | type: {type(y)}")
print(f"Value: z={z} | type: {type(z)}")
Value: x=1000.0 | type: <class 'float'> Value: y=150.0 | type: <class 'float'> Value: z=0.03 | type: <class 'float'>
Scientific notation is often used when working with very large or very small numbers, as it provides a more concise way to represent these values.
Complex Numbers 🔢¶
Complex numbers are numbers with a real and imaginary part, written as x + yj
, where x
is the real part and y
is the imaginary part. For example:
x = 3 + 4j
y = -2 - 3j
z = 1j # equivalent to 0 + 1j, aka perfect complex numbers
a = 0j
b = 1+0j # complex number with real part only
print(f"Value: x={x} | type: {type(x)}")
print(f"Value: y={y} | type: {type(y)}")
print(f"Value: z={z} | type: {type(z)}")
print(f"Value: a={a} | type: {type(a)}")
print(f"Value: b={b} | type: {type(b)}")
Value: x=(3+4j) | type: <class 'complex'> Value: y=(-2-3j) | type: <class 'complex'> Value: z=1j | type: <class 'complex'> Value: a=0j | type: <class 'complex'> Value: b=(1+0j) | type: <class 'complex'>
x = -5
result = abs(x)
print(f"BEFORE: {x}, AFTER: {result}")
BEFORE: -5, AFTER: 5
x = 5
result = abs(x)
print(f"BEFORE: {x}, AFTER: {result}")
BEFORE: 5, AFTER: 5
division and modulus - divmod(x, y)
¶
Returns the quotient and remainder when dividing x
by y
. For example:
x = 10
y = 3
quotient, remainder = divmod(x, y) # q is now 3, r is now 1
print(f"{x}/{y}, quotient: {quotient}, reminder: {remainder}")
10/3, quotient: 3, reminder: 1
x = 10
y = 5
quotient, reminder = divmod(x, y)
print(f"{x}/{y}, quotient: {quotient}, reminder: {reminder}")
10/5, quotient: 2, reminder: 0
# even number test, remainder = 0
x = 10
y = 2
quotient, reminder = divmod(x, y)
print(f"{x}/{y}, quotient: {quotient}, reminder: {reminder}")
10/2, quotient: 5, reminder: 0
# odd number test, remainder not equal 0
x = 11
y = 2
quotient, reminder = divmod(x, y)
print(f"{x}/{y}, quotient: {quotient}, reminder: {reminder}")
11/2, quotient: 5, reminder: 1
power - pow(x, y)
¶
Returns x
raised to the power of y
. For example:
x = 2
y = 3
z = pow(x, y)
print(f"{x} to the power of {y} = {z}")
2 to the power of 3 = 8
round - round(x[, n])
¶
Rounds a floating point number to the nearest integer. If n
is provided, rounds to n
digits after the decimal point. For example:
without specifying upto what decimal point you want to round off
x = 3.14159
result = round(x) # y is now 3
print(f"BEFORE: {x}, AFTER: {result}")
BEFORE: 3.14159, AFTER: 3
x = 3.94159
result = round(x)
print(f"BEFORE: {x}, AFTER: {result}")
BEFORE: 3.94159, AFTER: 4
x = 3.9
result = round(x) # y is now 3
print(f"BEFORE: {x}, AFTER: {result}")
BEFORE: 3.9, AFTER: 4
x = 3.4
result = round(x)
print(f"BEFORE: {x}, AFTER: {result}")
x = 3.5
result = round(x)
print(f"BEFORE: {x}, AFTER: {result}")
BEFORE: 3.4, AFTER: 3 BEFORE: 3.5, AFTER: 4
# round upto one decimal places
x = 3.45
result = round(x, 1)
print(f"BEFORE: {x}, AFTER: {result}")
# round upto one decimal places
x = 3.43
result = round(x, 1)
print(f"BEFORE: {x}, AFTER: {result}")
BEFORE: 3.45, AFTER: 3.5 BEFORE: 3.43, AFTER: 3.4
# round upto two decimal places
x = 3.44567
result = round(x, 2)
print(f"BEFORE: {x}, AFTER: {result}")
BEFORE: 3.44567, AFTER: 3.45
# round upto three decimal places
x = 3.44567
result = round(x, 3)
print(f"BEFORE: {x}, AFTER: {result}")
BEFORE: 3.44567, AFTER: 3.446
# avoid using it not a proper round up
x = 3.6
result = int(x) # another way but wrong way to round up
print(f"using 'int' BEFORE: {x}, AFTER: {result}")
result = round(x) # another way but correct way to round up
print(f"using 'round' BEFORE: {x}, AFTER: {result}")
using 'int' BEFORE: 3.6, AFTER: 3 using 'round' BEFORE: 3.6, AFTER: 4