Skip to content

Commit 920483f

Browse files
committed
Add number.py to demonstrate various number operations and precision handling in Python
1 parent c89a724 commit 920483f

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

Numbers/number.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from decimal import Decimal, Context
2+
from fractions import Fraction
3+
import math
4+
import random
5+
6+
# Decimal precision example
7+
ctx = Context(prec=6)
8+
result = ctx.add(Decimal('1.23456'), Decimal('7.89012'))
9+
print("Decimal context result:", result) # 9.12468
10+
11+
# Big integer power
12+
x = 2 ** 1000
13+
print("2 ** 1000 =", x)
14+
15+
# Float division
16+
result = 1 / 3
17+
print("1 / 3 =", result)
18+
19+
# str vs repr vs print
20+
print("str('chai'):", str('chai')) # 'chai'
21+
print("repr('chai'):", repr('chai')) # "'chai'"
22+
print("print('chai'):", end=" "); print('chai') # chai
23+
24+
# Boolean in conditionals
25+
print("bool([]):", bool([])) # False
26+
print("bool(-1):", bool(-1)) # True
27+
print("bool({'name': 'Alice'}):", bool({'name': 'Alice'})) # True
28+
29+
# Comparison operators
30+
print("1 < 2:", 1 < 2) # True
31+
print("5.0 == 5.0:", 5.0 == 5.0) # True
32+
print("4.0 != 5.0:", 4.0 != 5.0) # True
33+
34+
# math.floor() and math.trunc()
35+
print("math.floor(3.5):", math.floor(3.5)) # 3
36+
print("math.floor(-3.5):", math.floor(-3.5)) # -4
37+
print("math.trunc(-2.8):", math.trunc(-2.8)) # -2
38+
print("math.trunc(2.8):", math.trunc(2.8)) # 2
39+
40+
# Large integer precision
41+
print("999999999999999999 + 1 =", 999999999999999999 + 1) # Works fine
42+
43+
# Complex numbers
44+
print("(2 + 1j) * 3 =", (2 + 1j) * 3) # (6+3j)
45+
46+
# Number bases
47+
print("0xFF =", 0xFF) # 255
48+
print("0b1000 =", 0b1000) # 8
49+
print("oct(64) =", oct(64)) # '0o100'
50+
print("hex(64) =", hex(64)) # '0x40'
51+
print("bin(64) =", bin(64)) # '0b1000000'
52+
print("int('64', 16) =", int('64', 16)) # 100
53+
print("int('1000', 12) =", int('1000', 12)) # 1728
54+
55+
# Random module
56+
print("random.random():", random.random()) # Random float 0–1
57+
print("random.randint(1, 100):", random.randint(1, 100)) # Random int
58+
59+
li = ['lemon', 'masala', 'ginger tea']
60+
print("random.choice(li):", random.choice(li)) # Random element
61+
random.shuffle(li)
62+
print("shuffled li:", li)
63+
64+
# Floating point precision issues
65+
print("0.1 + 0.1 + 0.4 =", 0.1 + 0.1 + 0.4) # May be 0.6000000000000001
66+
print("0.1 + 0.1 + 0.1 =", 0.1 + 0.1 + 0.1) # 0.3
67+
print("0.1 + 0.1 + 0.1 + 0.3 =", 0.1 + 0.1 + 0.1 + 0.3) # 0.6
68+
print("(0.1 + 0.1 + 0.1) + 0.3 =", (0.1 + 0.1 + 0.1) + 0.3) # 0.6
69+
70+
# Solving precision issue with Decimal
71+
print("Decimal('0.1') * 3 =", Decimal('0.1') + Decimal('0.1') + Decimal('0.1')) # 0.3
72+
73+
# Fractions
74+
myFr = Fraction(2, 7)
75+
print("Fraction(2,7) =", myFr) # 2/7
76+
77+
# Set operations
78+
setone = {1, 2, 3, 4}
79+
print("setone & {1,3} =", setone & {1, 3}) # Intersection
80+
print("setone | {1,3,7} =", setone | {1, 3, 7}) # Union
81+
82+
# Boolean types and comparisons
83+
print("type(True):", type(True)) # <class 'bool'>
84+
print("True == 1:", True == 1) # True
85+
print("False == 0:", False == 0) # True
86+
print("True is 1:", True is 1) # False (because they’re not the same object)
87+
print("True + 4 =", True + 4) # 5

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy