๐Ÿ”ข 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.

Free ยท US Letter ยท no sign-up
Google Classroom Save to Pinterest Email me

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 round function rounds a number: round(3.7) gives 4.
  • To check whether a division comes out even, you use the remainder: 12 % 4 is 0, so 4 goes into 12 exactly.

๐Ÿ“Œ Remember: int = whole. float = has a decimal point. / always gives a float.

Quick quiz

  1. Which of these is a float?

  2. What does 10 / 4 give in Python?

  3. What does 10 // 4 give?

๐Ÿ”Ž Ask Thinkapedia another question