Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
BOOK 000 · CHAPTER 2 · Numbers

Numbers

On this pageIntegersFloatsOperatorsMathematical ExpressionsMini Project: Simple Arithmetic ProgramCoding Challenge

Numbers are a core part of every Python program. They let us count, calculate, and measure values used in everything from budgeting to data analysis and science. Python supports several numeric types and powerful operations.

In this section, you will learn about integers for whole numbers, floats for decimal values, operators for performing calculations, and mathematical expressions for combining them to solve real problems.

Integers

Integers represent whole numbers without a decimal point. They can be positive, negative, or zero. You might use integers to count students in a class, track days of the week, or represent a score in a game.

print(-25)
print(-7)
print(0)
Listing 58 — Integers

Output

-25
-7
0

Integers are the simplest form of numeric data. You’ll find them useful in situations such as counting items, measuring scores, or indexing data in lists and tables.

Floats

Floats (short for floating-point numbers) approximate real-valued measurements, such as distance, weight or time. A decimal point is one way to write a float. Many decimal fractions cannot be represented exactly, so small rounding differences can occur.

print(3.14)
print(-0.75)
print(2.0)
Listing 59 — Floats

Output

3.14
-0.75
2.0

Floats let a calculation represent fractional values; they do not guarantee exact decimal arithmetic.

Operators

Python can perform arithmetic operations just like a calculator.

The basic operators are:

  • + for addition
  • - for subtraction
  • * for multiplication
  • / for division
print(8 + 4)
print(10 - 6)
print(5 * 3)
print(12 / 4)
Listing 60 — Operators

Output

12
4
15
3.0

Division always produces a float, even when the result is a whole number. Python also includes two more useful operators:

  • // for floor division (rounds the quotient down, toward negative infinity)
  • % for modulo (returns the remainder)
print(17 // 3)
print(17 % 3)
Listing 61 — Operators

Output

5
2

You can also raise numbers to a power using ** (the exponentiation operator):

print(2 ** 5)
print(10 ** 2)
Listing 62 — Operators

Output

32
100

Mathematical Expressions

A mathematical expression combines numbers and operators to form a complete calculation. Parentheses () can be used to control the order of operations.

print((8 + 2) * 5 - 10 / 2)
Listing 63 — Mathematical Expressions

Output

45.0

Python follows the same order of operations as regular mathematics—parentheses first, then exponents, multiplication and division, and finally addition and subtraction.

Mini Project: Simple Arithmetic Program

Write a program that performs and displays the results of a few arithmetic operations. It should calculate and print the following directly:

  1. The sum of 15 and 7
  2. The result of 9 divided by 2
  3. The remainder when 19 is divided by 5
  4. The square of 6 (use the exponent operator **)

Coding Challenge

Create your own short arithmetic expression that combines at least three different operators. Try to predict the result before running it, then test your answer in Python. Experiment with parentheses to see how the order of operations affects the final value.