🐍 Number manipulation
Math
Basic Number Functions
min(2, 4, 6) # smallest value
max(2, 3, 7) # largest value
round(3.4) # rounds to nearest whole number
abs(-15) # absolute value → 15
pow(3, 2) # 3 to the power of 2 → 9
rest = 10 % 3 # modulo (remainder) → 1Range
# range(start(inklusiv), stop(exklusiv), steps)
for i in range(50, 100, 5): # jede 5. Zahl zwischen 50 und 99
print(i)Random number
import random
# Random whole number between 1 and 6 (like rolling a dice)
x = random.randint(1, 6)
print("Dice roll:", x)
# Random floating-point number between 0 and 1
y = random.random()
print("Random float:", y)With math Module
from math import floor, ceil, sqrt
floor(3.7) # always rounds down → 3
ceil(3.1) # always rounds up → 4
sqrt(144) # square root → 12Number Formatting x fStrings
⭐️ f-Strings are the cleanest and most modern way to format numbers in Python.
Basic Setup for following examples
number = 1000Decimal Places
f"The number is {number:.3f}"
# 1000.000 → round to 3 decimal placesThousand Separators
f"The number is {number:,}"
# 1,000 → adds commas (or locale separators)
f"The number is {number:_}"
# 1_000 → underscore separatorBinary / Octal / Hex
f"Binary: {number:b}" # 1111101000
f"Octal: {number:o}" # 1750
f"Hex: {number:X}" # 3E8 (uppercase)
f"hex: {number:x}" # 3e8 (lowercase)Scientific Notation
f"Scientific: {number:E}" # 1.000000E+03 (uppercase)
f"scientific: {number:e}" # 1.000000e+03 (lowercase)Percentage
value = 0.256
f"Value: {value:.1%}" # 25.6%Custom Numeric Formatting (advanced)
f"{number:,.2f}" # 1,000.00 → comma + 2 decimals
f"{number:0>8}" # 00001000 → pad with zeros to width 8
f"{number:=+8}" # +1000 → sign alignedPadding & Alignment
⭐️ fStrings are a great way to align your output: Alignment & Width