0% found this document useful (0 votes)
20 views3 pages

2 INTRO Number

The document discusses various numeric operations in Python including integers, floats, and underscores in numbers. It covers addition, subtraction, multiplication, division, exponents, floor division, modulus, and order of operations. It also discusses type conversion between integers and floats and using multiple assignment and constants.

Uploaded by

小郭
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

2 INTRO Number

The document discusses various numeric operations in Python including integers, floats, and underscores in numbers. It covers addition, subtraction, multiplication, division, exponents, floor division, modulus, and order of operations. It also discusses type conversion between integers and floats and using multiple assignment and constants.

Uploaded by

小郭
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction Number

March 1, 2024

1 Integer
• Add (+), Subtract (-), Multiply (*), and Divide (/)

[ ]: print(3 + 2)
print(3 - 2)
print(3 * 2)
print(3 / 2)

5
1
6
1.5
• Two multiplication symbols (**) to represent “Exponentiation”

[ ]: print(3 ** 2)
print(5 ** 2)
print(5 ** 3)

9
25
125
• Double slash (//) to represent “Floor division”

[ ]: print(3 // 2)
print(5 // 2)
print(5 // 3)

1
2
1

1
• One percent symbol (%) to represent “Modulus”

[ ]: print(3 % 2)
print(5 % 2)
print(5 % 3)

1
1
2
• Use parentheses to modify the order of operations

[ ]: print(2 + 3 * 4)
print((2 + 3) * 4)

14
20

2 Floats
Any number with a decimal point is “Float”.

[ ]: print(0.2 + 0.1) # [Floating Point Approximation]


# Result = 0.30000000000000004 != 0.3
print(0.2 - 0.1)
print(0.2 * 0.1)
print(0.2 / 0.1)

0.30000000000000004
0.1
0.020000000000000004
2.0
How to solve “Floating Point Approximation - 0.1 + 0.2 != 0.3”?
• Further reading - Floating Point Math

[ ]: from decimal import Decimal


x = Decimal("0.1")
y = Decimal("0.2")
print(x + y)
print(Decimal("0.2") + Decimal("0.1"))

print(round(2.675, 2))
print(round(Decimal("2.675"), 2))

0.3
0.3
2.67
2.68

2
3 Integers and Floats
When you divide any two numbers, you’ll always get a float.

[ ]: print(4/2)
print(1 + 2.0)
print(2 * 3.0)
print(3.0 ** 2)

2.0
3.0
6.0
9.0

4 Underscores in Numbers
You can group digits using underscores to make large numbers more readable.

[ ]: universe_age = 14_000_000_000
print(universe_age)

14000000000

5 Multiple Assignment
Need to separate the variable names with commas, and do the same with the values.
As long as the number of values matches the number of variables, Python will match them up
correctly.

[ ]: x, y, z = 0, 1, 0
print(f"x = {x}, y = {y}, z = {z}")

x = 0, y = 1, z = 0

6 Constanst
When you want to treat a variable as a constant in your code, write the name of the variable in all
capital letters.

[ ]: CONSTANST = 5000

References:
• Eric Matthes - Python Crash Course-No Starch Press (2023)

You might also like