Python Numbers

int, float, complex and conversions.

✍️ 3 exercises + code challenge in this lesson

Python has three numeric types:

  • int: whole numbers of unlimited size, e.g. 42, -7.
  • float: decimals, e.g. 3.14, 2.5e3.
  • complex: e.g. 3 + 5j.

Convert between them with int(), float() and complex(). The random module creates random numbers.

Key Takeaways: Python Numbers

Mastering Python Numbers helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Python Numbers Code Example

Python
import random

big = 2 ** 100
print("Big int:", big)
print("Float:", 7 / 2, "Integer division:", 7 // 2)
print(int(9.99), float(5), round(3.14159, 2))
print("Random 1-6:", random.randint(1, 6))
Free sign-in required · Google login available

Python Numbers Practice Exercises

Check what you learned in “Python Numbers”.

/ 3 solved
Fill in the blank

Use integer (floor) division.

print(7  2)   # 3
✓ Correct! Not quite. Try again.
Multiple choice

What does 2 ** 3 return?

✓ Correct! Not quite. Try again.
Code challenge

Print 17 divided by 4 rounded to 2 decimals using round().

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services