Number basics

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

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

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

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.