-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_05_Math.py
More file actions
104 lines (86 loc) · 2.77 KB
/
_05_Math.py
File metadata and controls
104 lines (86 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Math Operators and Functions
# Operators: https://www.w3schools.com/python/gloss_python_arithmetic_operators.asp
# Functions: https://docs.python.org/3/library/math.html
# We must import the module math
import math
print("Math operations")
print("7 + 2 =", 7 + 2)
print("7 - 2 =", 7 - 2)
print("7 * 2 =", 7 * 2)
print("7 / 2 =", 7 / 2)
print("3 + 7 / 2 =", 3 + 7 / 2)
print("(3 + 7) / 2 =", (3 + 7) / 2)
print("(50 - 5 * 6) / 4 =", (50 - 5 * 6) / 4)
print("(3 + 4) / (1 - 5) =", (3 + 4) / (1 - 5))
print("10/3 =", 10 / 3)
# Integer division (WARNING: Explain the operation using Drawing)
# // = integer division
# % = mod
print("Integer division")
print("7 // 2 =", 7 // 2)
print("7 % 2 =", 7 % 2)
result = divmod(7,2)
print("divmod(7,2)", result)
print(result[0], result[1])
# Pi
print("math.pi =", math.pi)
# Power
print("Power")
print("2 ** 0 =", 2**0)
print("2 ** 1 =", 2**1)
print("2 ** 2 =", 2**2)
print("2 ** 3 =", 2**3)
print("2 ** 4 =", 2**4)
print("3.75 ** 2.5 =", 3.75**2.5)
# Minimum and Maximum
number1 = 5
number2 = 10
number3 = 15
print('min(number1, number2) =', min(number1, number2))
print('min(number1, number2, number3) =', min(number1, number2, number3))
print('max(number1, number2) =', max(number1, number2))
print('max(number1, number2, number3) =', max(number1, number2, number3))
# ceil
print("math.ceil(10.1) =", math.ceil(10.1))
print("math.ceil(10.7) =", math.ceil(10.7))
# floor
print("math.floor(10.1) =", math.floor(10.1))
print("math.floor(10.7) =", math.floor(10.7))
# trunc
print("math.trunc(10.1) =", math.trunc(10.1))
print("math.trunc(10.7) =", math.trunc(10.7))
# round: it is a built in not from math module
print("round(10.1) =", round(10.1))
print("round(10.5) =", round(10.5))
print("round(10.7) =", round(10.7))
print("round(10.7567124, 3) =", round(10.7567124, 3))
print("round(10.7567124, 0) =", round(10.7567124, 0))
# absolute
print("math.fabs(10) =", math.fabs(10))
print("math.fabs(-10) =", math.fabs(-10))
# Logarithm
print("math.log(16, 2) =", math.log(16, 2))
print("math.log(1024, 2) =", math.log(1024, 2))
print("math.log10(100) =", math.log10(100))
print("math.log10(1000000) =", math.log10(1000000))
# Trigonometric Functions
print(math.sin(90))
print(math.cos(180))
print(math.tan(1))
# Square Root
print("math.sqrt(9) =", math.sqrt(9))
print("math.sqrt(49) =", math.sqrt(49))
print("math.sqrt(121) =", math.sqrt(121))
# Power
print("math.pow(2, 4) =", math.pow(2, 4))
print("math.pow(10, 3) =", math.pow(10, 3))
# Random numbers
# https://docs.python.org/3/library/random.html
# Need to import random module
import random
rnd_number = random.randint(0, 10)
print("Random from 0 and 10 =", rnd_number)
rnd_number = random.randint(5, 10)
print("Random from 5 and 10 =", rnd_number)
rnd_number = random.randint(-5, 5)
print("Random from -5 and 5 =", rnd_number)