Working with numeric expressions is pretty easy in Python there are not that many numeric types. For whole numbers you use int and the rest is a float.
#Number Types
varInt = 1 # int
varFloat = 2.8 # floatCalculations use similar operators like in Excel or SQL you can add (+), subtract (-), multiply (*) , divide (/) and use functions like round() to round the number. If you want to do more advanced things you need to import the math library and there you’ll find a lot of extra functions like sin(), cos(), tan(), floor(), ceil() etc. For an overview of all the math functions: Python Math (w3schools.com)
varUnitPrice = 12.55
varQuantity = 5
varLineTotal = varQuantity * varUnitPrice # 62.75
varRound = round(varLineTotal,0) # 63
import math
varRoundDown = math.floor(varLineTotal) # 62
varRoundUp = math.ceil(varLineTotal) # 63
varModulo = varRoundUp % 2 # 1
varFloorDivide = varRoundDown // 2 # 31Converting text into a number (parsing) can be tricky since different regions use different number formats. Like for example in the USA the dot (.) is used as a decimal separator while in the Netherlands we use a comma (,). We can solve this problem by using the module “locale” where we can set the region (locale). For a list of all the LCID ID’s: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid.
import decimal
import locale
# Parse String
varInt = int("2")
varFloat = float("2.8")
# Parse with Localization
locale.setlocale(locale.LC_ALL, 'nl-NL')
varDecimalStr = input('Enter in decimal nr:') # 1.234,567
varDecimal = locale.atof(varDecimalStr, decimal.Decimal)
print(varDecimal) # 1234.567 Formatting number into strings is pretty easy using f-strings. For a full list of all the available formatting codes go to: Python String Formatting (w3schools.com)
#Formatting
# Python 3.6+ f-strings
varFormat =f"€ { varDecimal :.2f}" # € 1234.57
varFormat =f"€ { varDecimal :.0f}" # € 1235
varFormat =f"€ { varDecimal :,.0f}" # € 1,235
varFormat =f"{ 9 :0>2d}" # 09
varFormat =f"{ 0.35 :.0%}" # 35%