Skip to ContentGo to accessibility page

1.5 Number basics

Learning objectives

By the end of this section you should be able to

  • Use arithmetic operators to perform calculations.
  • Explain the precedence of arithmetic operators.

Numeric data types

Python supports two basic number formats, integer and floating-point. An integer represents a whole number, and a floating-point format represents a decimal number. The format a language uses to represent data is called a data type. In addition to integer and floating-point types, programming languages typically have a string type for representing text.

Checkpoint

Integer and floating-point

Integer and floating point; ch 1, video 8

Concepts in Practice

Integers, floats, and strings

Assume that x = 1, y = 2.0, and s = "32".

1.
What is the output of the following code?
print(x, type(x))
  1. 1 'int'.
  2. 1.0 <class 'float'>.
  3. 1 <class 'int'>.
2.
What is the output of the following code?
print(y, type(y))
  1. 2.0 <class 'int'>
  2. 2.0 <class 'float'>
  3. 2 <class 'int'>
3.
What is the type of the following value?
"12.0"
  1. string
  2. int
  3. float

Basic arithmetic

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division.

Four basic arithmetic operators exist in Python:

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Division (/)

Checkpoint

Examples of arithmetic operators

Examples of arithmetic operators; ch 1, video 9

Concepts in Practice

Applying arithmetic operators

Assume that x = 7, y = 20, and z = 2.

4.
Given the following lines of code, what is the output of the code?
c = 0
c = x - z
c = c + 1
print(c) 
  1. 1
  2. 5
  3. 6
5.
What is the value of a?
a = 3.5 - 1.5
  1. 2
  2. 2.0
  3. 2.5
6.
What is the output of print(x / z)?
  1. 3
  2. 3.0
  3. 3.5
7.
What is the output of print(y / z)?
  1. 0
  2. 10
  3. 10.0
8.
What is the output of print(z * 1.5)?
  1. 2
  2. 3
  3. 3.0

Operator precedence

When a calculation has multiple operators, each operator is evaluated in order of precedence. Ex: 1 + 2 * 3 is 7 because multiplication takes precedence over addition. However, (1 + 2) * 3 is 9 because parentheses take precedence over multiplication.

Operator Description Example Result

()

Parentheses

(1 + 2) * 3

9

**

Exponentiation

2 ** 4

16

+, -

Positive, negative

-math.pi

-3.14159

*, /

Multiplication, division

2 * 3

6

+, -

Addition, subtraction

1 + 2

3

Table 1.4 Operator precedence from highest to lowest.

Checkpoint

Order of operations in an arithmetic expression

Order of operations in an arithmetic expression; ch 1, video 10

Concepts in Practice

Multiple arithmetic operators

9.
What is the value of 4 * 3 ** 2 + 1?
  1. 37
  2. 40
  3. 145
10.
Which part of (1 + 3) ** 2 / 4 evaluates first?
  1. 4 ** 2
  2. 1 + 3
  3. 2 / 4
11.
What is the value of -4 ** 2?
  1. -16
  2. 16
12.
How many operators are in the following statement?
result = -2 ** 3
  1. 1
  2. 2
  3. 3

Try It

Values and types

Write a Python computer program that:

  1. Defines an integer variable named 'int_a' and assigns 'int_a' with the value 10.
  2. Defines a floating-point variable named 'float_a' and assigns 'float_a' with the value 10.0.
  3. Defines a string variable named 'string_a' and assigns 'string_a' with the string value "10".
  4. Prints the value of each of the three variables along with their type.

Try It

Meters to feet

Write a Python computer program that:

  1. Assigns the integer value 10 to a variable, meters.
  2. Assigns the floating-point value 3.28 to a variable, meter2feet.
  3. Calculates 10 meters in feet by multiplying meter by meter2feet. Store the result in a variable, feet.
  4. Prints the content of variable feet in the output.
Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.