forked from huangsam/ultimate-python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.py
More file actions
Latest commit
38 lines (28 loc) · 1.16 KB
/
Copy pathexpression.py
File metadata and controls
38 lines (28 loc) · 1.16 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
"""
This module shows how to create new integers by applying math expressions
on existing integers.
"""
importmath
defmain() ->None:
# This is a simple integer
x=1
# Its value can be used as part of expressions
assertx+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
assertx*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
assertmath.isclose(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
assertx//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
assertx*2**3==8
if__name__=="__main__":
main()