forked from trekhleb/learn-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_numbers.py
120 lines (86 loc) · 3.25 KB
/
test_numbers.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Numbers.
@see: https://docs.python.org/3/tutorial/introduction.html
@see: https://www.w3schools.com/python/python_numbers.asp
There are three numeric types in Python:
- int (e.g. 2, 4, 20)
- bool (e.g. False and True, acting like 0 and 1)
- float (e.g. 5.0, 1.6)
- complex (e.g. 5+6j, 4-3j)
"""
def test_integer_numbers():
"""Integer type
Int, or integer, is a whole number, positive or negative,
without decimals, of unlimited length.
"""
positive_integer = 1
negative_integer = -3255522
big_integer = 35656222554887711
assert isinstance(positive_integer, int)
assert isinstance(negative_integer, int)
assert isinstance(big_integer, int)
def test_booleans():
"""Boolean
Booleans represent the truth values False and True. The two objects representing the values
False and True are the only Boolean objects. The Boolean type is a subtype of the integer type,
and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the
exception being that when converted to a string, the strings "False" or "True" are returned,
respectively.
"""
true_boolean = True
false_boolean = False
assert true_boolean
assert not false_boolean
assert isinstance(true_boolean, bool)
assert isinstance(false_boolean, bool)
# Let's try to cast boolean to string.
assert str(true_boolean) == "True"
assert str(false_boolean) == "False"
def test_float_numbers():
"""Float type
Float, or "floating point number" is a number, positive or negative,
containing one or more decimals.
"""
float_number = 7.0
# Another way of declaring float is using float() function.
float_number_via_function = float(7)
float_negative = -35.59
assert float_number == float_number_via_function
assert isinstance(float_number, float)
assert isinstance(float_number_via_function, float)
assert isinstance(float_negative, float)
# Float can also be scientific numbers with an "e" to indicate
# the power of 10.
float_with_small_e = 35e3
float_with_big_e = 12E4
assert float_with_small_e == 35000
assert float_with_big_e == 120000
assert isinstance(12E4, float)
assert isinstance(-87.7e100, float)
def test_complex_numbers():
"""Complex Type"""
complex_number_1 = 5 + 6j
complex_number_2 = 3 - 2j
assert isinstance(complex_number_1, complex)
assert isinstance(complex_number_2, complex)
assert complex_number_1 * complex_number_2 == 27 + 8j
def test_number_operators():
"""Basic operations"""
# Addition.
assert 2 + 4 == 6
# Multiplication.
assert 2 * 4 == 8
# Division always returns a floating point number.
assert 12 / 3 == 4.0
assert 12 / 5 == 2.4
assert 17 / 3 == 5.666666666666667
# Modulo operator returns the remainder of the division.
assert 12 % 3 == 0
assert 13 % 3 == 1
# Floor division discards the fractional part.
assert 17 // 3 == 5
# Raising the number to specific power.
assert 5 ** 2 == 25 # 5 squared
assert 2 ** 7 == 128 # 2 to the power of 7
# There is full support for floating point; operators with
# mixed type operands convert the integer operand to floating point.
assert 4 * 3.75 - 1 == 14.0