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}\)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.
Assume that x = 1, y = 2.0, and s = "32". What is the output of the following code?
print(x, type(x))
1 'int'.1.0 <class 'float'>.1 <class 'int'>.
- Answer
-
c.
x = 1is an integer, and when printing the type of an integer variable, the output is<class 'int'>.
Assume that x = 1, y = 2.0, and s = "32". What is the output of the following code?
print(y, type(y))
2.0 <class 'int'>2.0 <class 'float'>2 <class 'int'>
- Answer
-
b.
y = 2.0is floating-point number.
Assume that x = 1, y = 2.0, and s = "32". What is the type of the following value?
"12.0"
stringintfloat
- 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:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
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)
156
- Answer
-
c. Line 2 of the code evaluates as follows:
c = x - z = 7 - 2 = 5. Line 3 increments variablecby 1, making the total equal to 6.
Assume that x = 7, y = 20, and z = 2. What is the value of a?
a = 3.5 - 1.5
22.02.5
- Answer
-
b. The output of
3.5-1.5equals2.0, which is a floating-point number.
Assume that x = 7, y = 20, and z = 2. What is the output of print(x / z)?
33.03.5
- Answer
-
c. The result of the division operation is a floating-point number that isn't rounded down.
7/2 = 3.5.
Assume that x = 7, y = 20, and z = 2. What is the output of print(y / z)?
01010.0
- Answer
-
c. The output of the
20/2equals10.0because the division operation results in a float.
Assume that x = 7, y = 20, and z = 2. What is the output of print(z * 1.5)?
233.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 |
|
|
|
|
Exponentiation |
|
|
|
|
Positive, negative |
|
|
|
|
Multiplication, division |
|
|
|
|
Addition, subtraction |
|
|
What is the value of 4 * 3 ** 2 + 1?
3740145
- Answer
-
a. 3 raised to the power 2 is 9, times 4 is 36, plus 1 is 37.
Which part of (1 + 3) ** 2 / 4 evaluates first?
4 ** 21 + 32 / 4
- Answer
-
b. Although addition has lower precedence than exponentiation, the parentheses cause
1 + 3to be evaluated first.
What is the value of -4 ** 2?
-1616
- Answer
-
a. In Python, exponentiation, or raising to a power, has higher precedence than making a value negative. Python's
-16result can be confusing because, in math,"-4 squared"is16.
How many operators are in the following statement?
result = -2 ** 3
- 1
- 2
- 3
- Answer
-
c. The statement uses the assignment operator (
=), the negative operator (-), and the exponentiation operator (**).
Write a Python computer program that:
- Defines an integer variable named
'int_a'and assigns'int_a'with the value10. - Defines a floating-point variable named
'float_a'and assigns'float_a'with the value10.0. - Defines a string variable named
'string_a'and assigns'string_a'with the string value"10". - 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))
Write a Python computer program that:
- Assigns the integer value
10to a variable,meters. - Assigns the floating-point value
3.28to a variable,meter2feet. - Calculates 10 meters in feet by multiplying
meterbymeter2feet. Store the result in a variable,feet. - Prints the content of variable
feetin the output.
Interactive Code
- Answer
-
meters = 10
meter2feet = 3.28
feet = meters * meter2feet
print(feet)


