-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
expression.py
36 lines (27 loc) · 1.13 KB
/
expression.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
"""
This module shows how to create new integers by applying math expressions
on existing integers.
"""
def main():
# This is a simple integer
x = 1
# Its value can be used as part of expressions
assert x + 1 == 2
# An expression can be chained indefinitely. This concept of chaining
# expressions is powerful because it allows us to compose simple pieces
# of code into larger pieces of code over time
assert x * 2 * 2 * 2 == 8
# Division is tricky because Python 3.x returns 0.5 of type `float`
# whereas Python 2.x returns 0 of type `int`. If this line fails, it
# is a sign that the wrong version of Python was used
assert x / 2 == 0.5
# If an integer division is desired, then an extra slash must be
# added to the expression. In Python 2.x and Python 3.x, the behavior
# is exactly the same
assert x // 2 == 0
# Powers of an integer can be leveraged too. If more features are
# needed, then leverage the builtin `math` library or a third-party
# library. Otherwise, we have to build our own math library
assert x * 2 ** 3 == 8
if __name__ == "__main__":
main()