FREE LESSON PREVIEW
Values, types and calculations
Python stores whole numbers as integers, fractional values as floats and text as strings. Types change how operations behave: 2 + 3 is 5, while "2" + "3" is "23". Convert text deliberately with float("2.5") and validate input before calculating. A variable names a value.
revenue = 240
orders = 12
average_order = revenue / orders
print(average_order) # 20.0
Division by zero raises an error. Decide how to represent an unavailable average rather than inventing zero. Floating-point values are approximate; rounding a displayed result does not make its measurement more precise.
Try it yourself
Calculate revenue per order for revenue 315 and 15 orders. What if the order count is zero?
Worked answer
The average is 21.0. If the denominator is zero, return an unavailable result rather than performing division.
