Skip to main content
Engineering LibreTexts

1.5: Number Basics

  • Page ID
    117517
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)
    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 \(\PageIndex{1}\): Integers, floats, and strings

    Assume that x = 1, y = 2.0, and s = "32". What is the output of the following code?

    print(x, type(x))
    1. 1 'int'.
    2. 1.0 <class 'float'>.
    3. 1 <class 'int'>.
    Answer

    c. x = 1 is an integer, and when printing the type of an integer variable, the output is <class 'int'>.

    Concepts in Practice \(\PageIndex{2}\): Integers, floats, and strings

    Assume that x = 1, y = 2.0, and s = "32". 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'>
    Answer

    b. y = 2.0 is floating-point number.

    Concepts in Practice \(\PageIndex{3}\): Integers, floats, and strings

    Assume that x = 1, y = 2.0, and s = "32". What is the type of the following value?

    "12.0"
    1. string
    2. int
    3. float
    Answer

    a. Any value defined within quotation marks is a string value regardless of the content.

    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 \(\PageIndex{4}\): Applying arithmetic operators

    Assume that x = 7, y = 20, and z = 2. 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
    Answer

    c. Line 2 of the code evaluates as follows: c = x - z = 7 - 2 = 5. Line 3 increments variable c by 1, making the total equal to 6.

    Concepts in Practice \(\PageIndex{5}\): Applying arithmetic operators

    Assume that x = 7, y = 20, and z = 2. What is the value of a?

    a = 3.5 - 1.5
    1. 2
    2. 2.0
    3. 2.5
    Answer

    b. The output of 3.5-1.5 equals 2.0 , which is a floating-point number.

    Concepts in Practice \(\PageIndex{6}\): Applying arithmetic operators

    Assume that x = 7, y = 20, and z = 2. What is the output of print(x / z)?

    1. 3
    2. 3.0
    3. 3.5
    Answer

    c. The result of the division operation is a floating-point number that isn't rounded down. 7/2 = 3.5.

    Concepts in Practice \(\PageIndex{7}\): Applying arithmetic operators

    Assume that x = 7, y = 20, and z = 2. What is the output of print(y / z)?

    1. 0
    2. 10
    3. 10.0
    Answer

    c. The output of the 20/2 equals 10.0 because the division operation results in a float.

    Concepts in Practice \(\PageIndex{8}\): Applying arithmetic operators

    Assume that x = 7, y = 20, and z = 2. What is the output of print(z * 1.5)?

    1. 2
    2. 3
    3. 3.0
    Answer

    c. The output of an arithmetic operator on any floating-point operand results in a floating-point value. 2 * 1.5 = 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 \(\PageIndex{9}\): Multiple arithmetic operators

    What is the value of 4 * 3 ** 2 + 1?

    1. 37
    2. 40
    3. 145
    Answer

    a. 3 raised to the power 2 is 9, times 4 is 36, plus 1 is 37.

    Concepts in Practice \(\PageIndex{10}\): Multiple arithmetic operators

    Which part of (1 + 3) ** 2 / 4 evaluates first?

    1. 4 ** 2
    2. 1 + 3
    3. 2 / 4
    Answer

    b. Although addition has lower precedence than exponentiation, the parentheses cause 1 + 3 to be evaluated first.

    Concepts in Practice \(\PageIndex{11}\): Multiple arithmetic operators

    What is the value of -4 ** 2?

    1. -16
    2. 16
    Answer

    a. In Python, exponentiation, or raising to a power, has higher precedence than making a value negative. Python's -16 result can be confusing because, in math, "-4 squared" is 16 .

    Concepts in Practice \(\PageIndex{12}\): Multiple arithmetic operators

    How many operators are in the following statement?

    result = -2 ** 3
    1. 1
    2. 2
    3. 3
    Answer

    c. The statement uses the assignment operator (=), the negative operator (-), and the exponentiation operator (**).

    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.
    Interactive Code
     
     
    Answer

    int_a = 10
    float_a = 10.0
    string_a = "10"

    print(int_a, type(int_a))
    print(float_a, type(float_a))
    print(string_a, type(string_a))

    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.
    Interactive Code
     
     
    Answer

    meters = 10
    meter2feet = 3.28
    feet = meters * meter2feet
    print(feet)


    This page titled 1.5: Number Basics is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.