-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_numbers_test.py
76 lines (67 loc) · 1.49 KB
/
02_numbers_test.py
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
# Demonstrates common functions used with numbers.
import math
import random
import statistics
import decimal
# Absolute value.
print(abs(-123))
print()
# Powers.
x = 5**2
print(x)
print(pow(5, 2))
print(math.sqrt(25))
print()
# Larger/smaller number.
print(max(5, 2))
print(min(5, 2))
print()
# Rounding.
print(round(1.5))
print(math.floor(1.7))
print(math.ceil(1.2))
# round to nearest 0.5 instead of 1.
factor = 0.5/1
print(round(1.4/factor)*factor)
print()
# Compound assingment operators.
y = 1
y += 1
y -= 1
y *= 10
y /= 2
print('start:', y)
# Use this if you want the result to be an integer.
y //= 2
print('divide and floor:', y)
y **= 2
print('power:', y)
y %= 2
print('remainder:', y)
print()
# Different randomizations.
print(random.choice(['one', 'two', 'three']))
# No repeats.
print(random.sample(range(100), 10))
# Set random seed.
random.seed(123)
# Similar to excel's rand()
print(random.random())
# Rand int from 1-10.
print(random.randrange(1, 11))
print()
# Stat functions.
data = [75, 25, 250, 50]
print(statistics.mean(data))
print(statistics.median(data))
print(statistics.variance(data))
print()
# Demonstrates use of the decimal module for calcuations involving fractions
# that give weird results in floating point. Alternatively, multiply all input
# by a factor of 10.
print(1.00 % 0.10)
print(decimal.Decimal('1.00') % decimal.Decimal('.10'))
print()
# Alternatively, use math.isclose() to prevent floating point precision errors.
print(0.1 + 0.7 == 0.8)
print(math.isclose(0.1 + 0.7, 0.8))