Numbers, Operators, and Type Conversion
Python handles numbers naturally. In this lesson you will learn the two main number types, all the math operators, and how to convert values from one type to another so your calculations behave the way you expect.
What You'll Learn
- Integers (
int) and floating-point numbers (float) - All the arithmetic operators, including floor division and modulo
- Order of operations and compound assignment
- Converting between numbers, text, and other types
Integers and Floats
An integer is a whole number. A float has a decimal point:
Arithmetic Operators
Two of these surprise beginners: / always gives a float (10 / 2 is 5.0), and // (floor division) throws away the remainder. The % operator (modulo) gives you that remainder, which is perfect for checking if a number is even.
Exercises
Order of Operations
Python follows normal math precedence: parentheses, then exponents, then multiply/divide, then add/subtract.
Compound Assignment
x += 5 is shorthand for x = x + 5. The same works for -=, *=, //=, and more. You will use += all the time to build up totals:
Type Conversion
Values have types, and sometimes you need to convert between them. Use int(), float(), and str():
This matters a lot: if you have the text '42' and try to add a number to it, Python raises an error. Convert it with int() first.
Rounding and Formatting
The :.2f format shows exactly two decimal places, which is exactly what money needs. You will use it in the tip calculator next.
Key Takeaways
intfor whole numbers,floatfor decimals/gives a float,//floors,%gives the remainder,**is power- Use
+=,*=, and friends as shorthand - Convert with
int(),float(),str() - Format money with an f-string like
f'${value:.2f}'

