Python Numbers

ChatGPT said: A colourful cartoon-style landscape illustration shows two large labelled containers: one marked Integers filled with whole numbers like 7, 0, and -3, and another marked Floats filled with decimal numbers like 3.14, -0.5, and 2.0. A cheerful character is sorting the numbers into the correct containers, visually explaining the difference between integers and floating-point numbers in Python.

Numbers are one of the most common things you’ll use in programming. Just like in everyday life, we need numbers to count, measure, and calculate. In Python, numbers are treated as special data types, which means the computer knows how to work with them in a mathematical way.

For example, you might want to:

  • Keep track of a score in a game.
  • Work out the total price of shopping.
  • Measure a distance or a temperature.
  • Calculate someone’s age from their birth year.

Python makes all of this possible by giving us different types of numbers, each designed for a specific job. The two you’ll start with are:

  • Integer (int) – whole numbers (…−3, −2, −1, 0, 1, 2, 3…).
    Examples: 0, 7, -42, 2025
  • Floating-point (float) – numbers with a decimal point.
    Examples: 3.14, -0.5, 2.0, 1e3 (which means 1000.0)

Try it

  • Make one int and one float. Print both and use type() to check their types.

Basic arithmetic

Python supports the usual arithmetic operators:

OperatorMeaningExampleResult
+addition7 + 310
-subtraction7 - 34
*multiplication7 * 321
/true division7 / 23.5 (float)
//floor division7 // 23 (int)
%remainder (modulo)7 % 21
**power2 ** 38

Order of operations (BODMAS/PEMDAS) applies: brackets first, then powers, then multiply/divide, then add/subtract.

Try it

  • Work out 9 // 4, 9 % 4, and 9 / 4.
  • Predict 3 + 2 ** 3 * 2, then run it to check.

Mixing ints and floats

If an operation mixes int and float, the result is a float:

Converting types

  • int(x) converts to an integer (truncates towards zero).
  • float(x) converts to a float.
  • str(x) converts to text (string).

Try it

  • Convert "3.9" to a float and add 1.
  • Convert 5.9 to an int: what do you get, and why?

Input from a user (and converting it)

input() always returns a string, so you must convert it before doing maths.

Try it

  • Ask for two numbers with input(), convert them to float, and print their average.

Rounding and absolute values

Note: Python’s round() uses “banker’s rounding”: round(2.5)2, round(3.5)4.

Try it

  • Round 9.995 to 2 decimal places.
  • Use abs() to turn -123 into positive.

Floating-point gotchas (precision)

Floats are stored in binary, which can’t represent some decimals exactly. You may see tiny errors:

This is normal in many languages. If exact decimal money maths matters, use decimal.Decimal:

Try it

  • Print 0.3 == 0.1 + 0.2. Then print Decimal("0.3") == Decimal("0.1") + Decimal("0.2").

Comparisons and tests

Use comparison operators to ask true/false questions:

OperatorMeaningExampleResult
==equal to5 == 5True
!=not equal to5 != 3True
<less than2 < 7True
<=less or equal7 <= 7True
>greater than10 > 3True
>=greater or equal3 >= 4False

Try it

  • Write a program that asks for a test score (0–100) and prints Pass if it’s >= 50, else Fail.

A few handy maths tools

The built-in math module gives extra functions:

Try it

  • Compute the area of a circle with radius r = 2.5 using math.pi * r ** 2.

Mini practice set (answers by running the code)

  1. Write a calculator that reads two numbers and prints: sum, difference, product, true division, floor division, remainder.
  2. Given seconds = 3672, print hours, minutes, seconds (e.g., 1h 1m 12s) using // and %.
  3. Ask for someone’s height in centimetres and print metres as a float with two decimal places.
  4. Create x = 0.1; add it to itself 10 times. Why isn’t it exactly 1.0? Show a round(..., 10) version.
  5. Use Decimal to add 0.1 three times and show that it equals 0.3 exactly.

Common beginner mistakes (and fixes)

  • Forgetting to convert input n = input("Number: ") print(n + 1) # ❌ TypeError (string + int) print(int(n) + 1) # ✅
  • Surprised by division print(7 / 2) # 3.5 (float) print(7 // 2) # 3 (floor division)
  • Float comparison print(0.1 + 0.2 == 0.3) # False (precision) print(round(0.1 + 0.2, 10) == 0.3) # True-ish for display

Summary

  • Integers are whole numbers; floats include decimals.
  • Use standard operators (+ - * / // % **) and remember order of operations.
  • Convert types with int(), float(), str(), and convert input() before maths.
  • Floats can show tiny precision errors; for exact decimal work, use decimal.Decimal.
  • round(), abs(), and math functions are your friends.

You’re now ready to work with numbers in Python, whether you’re keeping score in a game, doing simple sums, or formatting money neatly.

Main Topic

Python Fundamentals

A colourful cartoon-style landscape illustration shows a person sitting at a computer terminal, looking overwhelmed by the amount of information on the screen. Papers and symbols float around them, representing different programming concepts. The word “Fundamentals” is clearly written above the scene, highlighting the focus on beginner Python basics.

Beginner’s guide to Python fundamentals: strings, numbers, booleans, syntax, and text manipulation explained simply and clearly.

Other Tutorials in this Topic

A playful cartoon-style landscape illustration shows a large elephant on the left and a small cat on the right. Between them, a bold greater than ( > ) symbol points toward the cat, representing a Python comparison operator in a fun way.

Python Operators

Beginner’s guide to Python operators: arithmetic, comparison, logical, assignment, membership, and identity explained with examples.

A colourful cartoon-style landscape illustration shows a smiling robot holding up two placards: one clearly labelled True and the other False. The background is bright and cheerful, symbolising the concept of Boolean values in Python programming.

Python Booleans

Learn Python Booleans: True or False values, comparisons, logic operators, and decision-making with simple examples.

A colourful cartoon-style landscape illustration shows the text “python ” written across the scene. Above the gap, a labelled box with the word “strings” is tilted, pouring its contents into the space, visually representing a variable being used to fill in a placeholder.

Python Strings – Part 4

Discover Python string formatting: use f-strings, .format(), and number formatting to neatly combine variables and text into…

A colourful cartoon-style landscape illustration shows the words “python strings” in lowercase being fed into a large whimsical machine. On the other side, the text emerges transformed into “PYTHON STRINGS” in uppercase letters, symbolising string methods like .upper(). The machine has gears and pipes, giving a fun and playful feel.

Python Strings – Part 3

Learn Python string methods: change case, remove spaces, replace text, split and join words, and check content…

A digital landscape illustration shows a beige fabric banner with the words “Python Strings” written across it. The material is ripped down the centre, with large visible stitches crudely sewing the two halves back together, symbolising fixing or escaping characters in strings. The background is simple and muted, drawing attention to the torn fabric.

Python Strings – Part 2

Explore Python strings further: escape characters, multi-line strings, and slicing to extract parts of text, with simple…

A colourful cartoon-style landscape illustration shows festive bunting strung across the top, with triangular flags spelling out “Python Strings” in bright, playful letters. The background is light and cheerful, giving a fun and welcoming atmosphere.

Python Strings – Part 1

Learn Python strings: text in quotes, printing, joining with +, counting length, and accessing characters by position…

A colourful cartoon-style illustration shows a confused student with orange hair shrugging in front of a computer screen. The screen displays a Python function with a missing indentation error:

Python Syntax

Learn Python syntax basics with clear examples and beginner-friendly practice exercises.

A cartoon-style illustration shows a person’s hands typing on a laptop running Visual Studio Code in dark mode. The screen displays a Python file with the code print("Hello, world!"), alongside a large, colourful Python logo. The background is bright blue, creating a vibrant and engaging learning atmosphere.

Start Coding Python with VS Code

Beginner’s guide to installing Python, setting up VS Code, and getting ready for coding.

Python Variables

Python Variables

Learn Python variables with simple explanations, real-world analogies, practice tasks to build confidence using data in programs.