๐ข Integer and Float
5th Grade ๐ป Coding
Programming has two main kinds of numbers: integers (`int`), like 7 or -3, and floating-point numbers (`float`), like 2.5 or 0.1.
In Python, int is short for integer โ a whole number โ and float is a number with a decimal point. When you divide with a single slash, the answer is always a float: 10 / 4 gives 2.5, and even 10 / 2 gives 5.0. If you want a whole number, you use //: 10 // 4 gives 2.
Decimal numbers are stored in the computer as close approximations, so sometimes you get slightly strange results, like 0.1 + 0.2 giving 0.30000000000000004. That isnโt a Python bug โ itโs how decimal numbers work on almost every computer. Thatโs why programmers are extra careful with things like money.
For example
- The number of students in a class is an int (32); the temperature outside is a float (75.5).
- The
roundfunction rounds a number:round(3.7)gives 4. - To check whether a division comes out even, you use the remainder:
12 % 4is 0, so 4 goes into 12 exactly.
๐ Remember: int = whole. float = has a decimal point. / always gives a float.
Quick quiz
Which of these is a float?
What does 10 / 4 give in Python?
What does 10 // 4 give?