0% found this document useful (0 votes)
18 views

Lecture 2

This document discusses computer architecture and program execution. It introduces compiled and interpreted programming languages. It also provides an overview of using Jupyter Notebook as an interactive environment for coding and taking notes.

Uploaded by

0
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lecture 2

This document discusses computer architecture and program execution. It introduces compiled and interpreted programming languages. It also provides an overview of using Jupyter Notebook as an interactive environment for coding and taking notes.

Uploaded by

0
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 20

{

"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lecture 2\n",
"\n",
"The objectives of this lecture are to:\n",
"\n",
"1. Learn the different modes of program execution (by the operating
system).\n",
"2. Overview the interactive lecture notes environment [iPython Notebook]
(http://ipython.org/notebook).\n",
"3. Introduce the concepts of *expressions*, *values*, and *types*.\n",
"4. Learn how numeric values are *approximated* by the computer."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# I. Computer Architecture and Execution of a Program\n",
"\n",
"In general, there are two different classes of programming languages based on
how programs written in these languages are executed: *compiled* and *interpreted*.
Before we can discuss this significance of these two classifications, you will need
some background on *computer architecture*.\n",
"\n",
"Computer architecture describes the different operational components of a
computer (hardware), the instructions that the computer executes (software),
interaction with its environment (input/output), and how they interrelate. In order
to keep things simple, we will focus on four components of a computer:\n",
"\n",
"* *Processor Unit* (hardware): the *processor* executes the instructions of a
program and understands a \"low-level\" set of instructions called *machine code*.
Modern computers typically have multiple processors (\"cores\") which can operate
independent of each other but share common infrastructure. They are located on the
same piece of hardware which is referred to as the *central processing unit*. \n",
"* *Input/Output Devices* (hardware): these devices should be familiar in that
they are what we use to interact with a computer. The keyboard, mouse, microphone,
camera, etc are all input devices. The monitor and speakers are all output
devices.\n",
"* *Memory* (hardware): a computer needs to store programs in order to
function, which is a more complicated task than you might imagine. An engineering
problem exists in that there is a competition between the capacity (amount of
information stored), throughput (rate at which data can be accessed/stored), and
latency (delay between information requests). As a result of this there exists a
hierarchy of memory devices that the CPU must interact with; we will discuss this
soon. \n",
"* *Operating System and Applications* (software): a computer needs
instructions in order to perform useful work. These are grouped into three types of
programs:\n",
" * *Firmware*: this is the first program loaded by the computer on start-
up. It initializes all of the hardware, performs tests to ensure they function
properly, and then the operating system.\n",
" * *Operating System*: this program performs many different tasks and
services that are commonly used in different applications (next item). The OS also
provides a layer of *abstraction* between the hardware and other programs.\n",
" * *Applications*: applications are what we typically refer to as programs;
they are typically OS-specific and perform a focused task.\n",
" \n",
"![alt text](files/images/lecture2/lecture2-1.png)\n",
"\n",
"In this course we focus on developing programs that are classified as
applications. Focusing on them, we may further classify applications based upon the
type of program language that they are written in: compiled or interpreted.\n",
"\n",
"*Compiled* programming languages use a special program called a *compiler* to
translate source code into machine code. This machine code is then executed by the
OS.\n",
"\n",
"*Interpreted* programming languages use a special program called an
*interpreter* to interpret and execute program statements on the CPU. Thus the
interpreter is an additional layer of abstraction above the OS in this scenario.
What does this imply about the implementation of the interpreter? \n",
"\n",
"Given our discussion about low- versus high-level programming languages in the
previous lecture, one might assume that this is directly related to whether or not
the programming language is compiled or interpreted. While this is definitely the
predominant trend, the true distinction between low- and high-level languages is
that the former exposes much of the details of the underlying computer architecture
while the latter uses abstraction to hide these detailed from the programmer.\n",
"\n",
"Python Language (interpreted, high-level):\n",
" \n",
"```python\n",
"import time;\n",
"\n",
"localtime = time.asctime( time.localtime(time.time()) )\n",
"print(\"Local current time :\", localtime)\n",
"```\n",
"\n",
"C Language (compiled, low-level):\n",
"\n",
"```c\n",
"#include <time.h>\n",
"#include <stdio.h>\n",
" \n",
"#define SIZE 256\n",
" \n",
"int main (void)\n",
"{\n",
" char buffer[SIZE];\n",
" time_t curtime;\n",
" struct tm *loctime;\n",
" \n",
" curtime = time (NULL);\n",
" loctime = localtime (&curtime);\n",
" \n",
" fputs (asctime (loctime), stdout);\n",
" \n",
" return 0;\n",
"}\n",
"```\n",
"\n",
"Finally, interpreted programming languages enabled two different usage modes:
standalone and interactive. At the end of the lecture we will try both, but for now
continue to follow along in this special environment."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Overview of Interactive Lecture Notes (iPython Notebook)\n",
"\n",
"iPython Notebook is a web-based computational interpreter. It allows us to
combine code editing and execution, text, mathematics and plots, and other forms of
media (i.e. images, videos) all in one document. Each notebook is saved to a
computer as an .ipynb file and viewed in a web browser as a static web page.\n",
"\n",
"We will be using iPython as interactive lecture notes to make your learning
more engaging. Each notebook corresponds to a video lecture and consists of text
explaining concepts, as well as regions of unexecuted code. As you read or follow
along, try to predict what the result of running the code will be. Try to
understand the code, and then run the code to see if your prediction was right. You
can also add text to the notebooks if you wish to add your own notes. You can also
add your own code and execute it to see what the results will be.\n",
"\n",
"Take the time to to explore iPython Notebook environment and get used to it.
The more familiar you become with it, the more you can get out of it as a way to
organize your course notes.\n",
"\n",
"### iPython Dashboard\n",
"Once we launch iPython Notebook from the terminal, a web browser page is
launched. The first page we're directed to is the iPython Dashboard. This is where
we find all of the notebooks in our current directory listed. At the top of the
list, you will see where on your machine those files are located: the current
directory from which you launched iPython Notebook. To open a notebook, click on
its name. The selected notebook will open in a new tab. \n",
"\n",
"Before you close a notebook, make sure to save it, either by pressing Ctrl + S
or clicking the save icon in the top left corner of the toolbar in your notebook.
Once you have saved, close the notebook tab. In the iPython Dashboard, next to the
notebook you've just closed, click Shut Down.\n",
"\n",
"To shut down the iPython Notebook server, first close the iPython Dashboard
tab. In the terminal, press Ctrl + C. When you are asked if you want to shut down
the notebook server, press Y.\n",
"\n",
"### Notebook Structure: Cells\n",
"A notebook consists of a sequence of cells, which are text input fields that
can be executed right in the web browser. We add cells by clicking Insert > Insert
Cell Above/Below in the menu bar or click the up/down __circular__ arrow icons in
the toolbar to insert a cell above or below the current cell. If we want to move
the cell once it's created, click the __non-circular__ up/down arrow icons in the
toolbar. To remove a cell, in the menu bar click Edit > Delete Cell while the cell
is selected.\n",
"\n",
"The input of the cell can be executed either by pressing Shift + Enter on the
keyboard, clicking the \"Play\" icon in the toolbar, or in the clicking Cell > Run
in the menu bar. The way in which the contents of the cell are executed depends on
the type of cell. When you have a cell selected, you can change the type in the
dropdown menu in the toolbar. There are 4 different cell types:\n",
"* __Python Code Cells:__ This is the default cell type once we create any
cell. You can edit and write Python code in these cells and executing them runs the
code. The output (i.e. result of the execution of the cell) is shown in a cell
below the input cell. You can hide these output cells by clicking Cell > All Output
> Clear.\n",
"* __Raw Text Cells:__ These cells allow you to enter plain text into the
notebook. iPython Notebook does not execute these files, so what is typed as input
is the exact same as the output that is shown once we \"run\" the cell.\n",
"* __Heading Cells:__ You can use these cells to organize your notes with
headings. There are 6 levels of headings with Heading 1 being the highest and
Heading 6 being the lowest. You simply select the level of heading, enter your
text, and run the cell and iPython Notebook will format it for you.\n",
"* __Markdown Cells:__ This cell type allows you to edit the notebook with
*rich text*. Rich text supports text formatting, such as bold, italics, and
underlining, as well as different fonts and font sizes. In iPython Notebook, this
is accomplished using the language *Markdown*. To see what Markdown looks like,
double click on one of the text cells (this cell is a Markdown cell). When you
execute the cell, you see all of the formatting. You probably won't be using this
type of cell, but if you are interested in Markdown as an HTML replacement see
[here](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet).\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Statements, Expressions, and Values (in Python)\n",
"\n",
"You will find that you already know quite a bit about how to write programs
that perform arithmetic. An *expression* is a minimal statement that can be
evaluated by the Python interpreter. Here are a few examples of basic arithmetic
(**evaluate**):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1 + 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5 - 4"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"-3 * 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"8 / 4 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the examples the *operators* (+,-,\\*,/) and *operands* (integers) are
already familiar to you. In these simple cases the operands are also *values*, but
in general operands can be any *expression* (**evaluate**):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(3 - 4) * 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Clearly one of the operands of the multiplication operator is an expression
(that evaluates to a value). In fact, all expressions evaluate to a value! Values
must belong to a *type*, which is specified by:\n",
"\n",
"* the definition of a set of values that belong to the type.\n",
"* the definition of a set of operations that can be applied to those
values.\n",
"\n",
"An illustrative example of the significance of type is (**evaluate**):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"4 / 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"4. / 3."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"While both expressions look similar, they involve different *types* of values:
*integers* which have type `int` in Python and *floating point* values (type
`float`) which are an approximation to the set of real numbers. Python 3 implements
\"true division\" for all numeric values, and thus the resulting value is the same,
but in many programming languages this is not the case. The reason being is that
for the first expression, the output value is not of type `int`!\n",
"\n",
"One of the many conveniences (**and dangers**) of Python is that values are
*dynamically typed*. The Python interpreter does not require that the type of a
value be explicitly specified and will interpret the type of a value based upon the
context in which it is used. This is a fundamental property of Python which we need
to always keep in mind, in many other programming languages the expressions above
would evaluate like this instead:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"4 // 3 # use \"classic\" *floor division* so that the result maintains the int
type"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"4. / 3."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and thus the type of the output is the same as that of the input.\n",
"\n",
"Another example of dynamic typing is performing arithmetic with values of
mixed type:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"4 / 3."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"4. / 3."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It should be obvious from these results that the Python interpreter is using
context to interpret integer values as `float`. This scenario will occur frequently
in your use of Python, particularly with numerical types. It will be more
thoroughly addressed later in the course.\n",
"\n",
"Now that we have learned about two familiar types in Python, `int` and
`float`, let's practice using Python to evaluate expressions with these types.
Python defines a set of arithmetic operators for these types:\n",
"\n",
"|**Symbol** |**Operator** |**Example Expression**|**Resulting Value**|\n",
"|------------|--------------|----------------------|-------------------|\n",
"|- |negation |-5 |-5 |\n",
"|+ |addition |11 + 3.1 |14.1 |\n",
"|- |subtraction |5 - 19 |-14 |\n",
"|* |multiplication|8.5 * 4 | 34.0 |\n",
"|/ |division |11 / 2 |5.5 |\n",
"|// |integer division: returns the _floor_ of the result of a
division; returned value is of type _float_|11 // 2|5|\n",
"|% |remainder: returns the remainder from a division|8.5 % 3.5|
1.5|\n",
"|** |exponentiation|2 ** 5|32|\n",
"\n",
"\n",
"\n",
"Some operators require two operands, while others just one. The former are
called *binary* operators and the latter *unary* operators. Let's practice with
*binary* operators first using integers (**evaluate**):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"53 // 24 # integer division"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"53 / 24 # true division"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"53 % 24 # modulo/remainder"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(24 // 5) * 2 # an example of nested expressions, try to guess the answer
before evaluating!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's try *binary* operators with floats (**evaluate**):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"53. // 24. # integer division...with floating point values!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"53. / 24. # division"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"53. % 24. # modulo...with floating point values!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"2.**4 # exponentiation with a float and an integer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"2**4 # exponentiation with two integers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"16**0.5 # exponentiation with an integer and a float"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(24. // 5.) * 2 # note that one of the values is an integer, what did the
interpreter do?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's use a few unary operators with integers and floats (**evaluate**):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"-1 # negation, same symbol as subtraction"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"+4 # redundant"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you have seen, we have been able to apply unary and binary arithmetic
operators two both integers and floats, and combinations of the two. Any operator
that can be applied to different *types* of values is called an **overloaded**
operator. This distinction is not especially important in Python in that all
operators tend to be overloaded, but in statically typed programming language this
is less frequent."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Operator Precedence"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Operator precedence in Python, and most programming languages, is based upon
the same concepts used in the algebra you learned in grade school.\n",
"\n",
"|**Precedence**|**Operator**|**Operation** |\n",
"|--------------|------------|--------------|\n",
"|1 (highest) |** |exponentiation|\n",
"|2 |- |negation |\n",
"|3 |*, /, //, % |multiplication, division, integer division,
remainder|\n",
"|4 (lowest) |+, - |addition, subtraction|\n",
"\n",
"**NOTE:** Operators of equal precedence are applied from left to right
_**except**_ exponentiation, which is applied from right to left.\n",
"\n",
"Using these precedence rules, operators of higher precedence are evaluated
before those with lower precedence. In order to communicate a specific order of
evaluation we may parentheses,\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"- 2**4 # the interpreter applies its rules"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"- (2**4) # parentheses do not change the order of operation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(-2)**4 # parentheses override the interpreter order of operation rules"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A more complex example is converting temperature values from units of
Fahrenheit to Celsius. Given a temperature in Fahrenheit, we must subtract 32 and
multiply by 5/9,"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"212 - 32 * 5 / 9 # 212 F = 100 C"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(212 - 32) * 5 / 9 # appropriate placement of parentheses results in the
desired order of operations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An additional use of parentheses is to provide visual cues to increase the
readability of your code, even if order of operation overriding is not needed,"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1. + 2. - 3.**4. * 2. / 5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1. + 2. - ((3.**4. * 2.) / 5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Finite Precision"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The computer has finite memory capacity and is thus limited in the
representation of numbers. For integers, Python is able to represent arbitrary
integer values limited by the memory capacity of the computer. Most compiled
languages can represent integers only up to a certain magnitude, above
which \"overflow\" occurs. With floating point numbers, which approximate the set
of real numbers, there are both magnitude and precision tolerances involved."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"2. / 3."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We know that the decimal representation of 2/3 has an infinite number of 6's
following the decimal point. The value that the interpreter displays is the closest
value to 2/3 that can be represented given the amount of memory that is allocated
to a `float` in Python. We will learn of numerical libraries that allow us to
increase (or decrease) the memory allocated to each value later in the class. Later
in your studies you will learn the exact expression used to represent `float`'s,
but that is too advanced for this course."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A significant consequence of the fact that real values are approximated is
shown here,"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"2. / 3. + 1."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5. / 3."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is an example of *rounding error*; while the value `1` can be stored
exactly, `2/3` and `5/3` cannot. While the second result is a more accurate
representation of `5/3`, given a rounding convention, clearly there was loss of
information in the evaluation of the first expression.\n",
"\n",
"Rounding error increases in significance as the number of computations
increases, or the sensitivity of the computations increases. It also increases when
computations involving values of very different magnitudes are used,"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"100000000000000. + 0.000000000000001"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Clearly the addition resulting in rounding error and such issues are pervasive
in the use of computers to perform computations within continuous domains. Much of
science and engineering involves continua, thus you will must be aware of floating
point arithmetic and the resulting occurrence of rounding error.\n",
"\n",
"You will learn techniques to minimize the occurrence of rounding error later
in your coursework, such as scaling of equations and minimum parametrization."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Practice Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### PracProg Section 2.10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**1.** For each of the following expressions, what value will the expression
give? Verify your answers by running the cell to evaluate the expressions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"9 - 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"8 * 2.5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"9 / 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"9 / -2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"9 // -2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"9 % 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"9.0 % 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"9 % 2.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"9 % -2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"-9 % 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"9 / -2.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"4 + 3 * 5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(4 + 3) * 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**2.** Unary minus negates a number. Unary plus exists as well. For example,
Python understands `+5`. If `x` has the value `-17`, what do you think +x should
do? Try it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3.** Write assignment statements that do the following: \n",
">**a)** Create a new variable, `temp`, and assign it the value `24`. Run the
`print temp` command below to see the variable's current value."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
">**b)** Convert the value in `temp` from Celsius to Fahrenheit by multiplying
by `1.8` and adding `32`; make `temp` refer to the resulting value. Run the `print
temp` command again to see how the value of the variable has changed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(temp)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**4.** For each of the following expressions, in which order are the
subexpressions evaluated?\n",
">**a)** \n",
"```python\n",
"6 * 3 + 7 * 4\n",
"``` \n",
">**b)** \n",
"```python\n",
"5 + 3 / 4\n",
"``` \n",
">**c)** \n",
"```python\n",
"5 - 2 * 3 ** 4\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**5.** a. Create a new variable x , and assign it the value 10.5.\n",
"\n",
"b. Create a new variable y , and assign it the value 4.\n",
"\n",
"c. Sum x and y , and make x refer to the resulting value. What are x and y ’s
new values?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**6.** Which of the following expressions results in SyntaxError s?\n",
"a. 6 * -----------8\n",
"\n",
"b. 8 = people\n",
"\n",
"c. ((((4 ** 3))))\n",
"\n",
"d. (-(-(-(-5))))\n",
"\n",
"e. 4 += 7 / 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Additional Questions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**1.** The following expressions evaluate to results that are either of type
`int` or type `float`. If the result is of type `int`, make changes to the
expression so that the result is of type `float`. Similarly, if the result is of
type `float`, make changes to the expression so that it is of type `int`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"2 + 9"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"7 / 2.4"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"float(3. * 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**2.** Remove parentheses from the following expressions without changing the
value of the result."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(3 * 4) / (5 + 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(3 ** 2) + (1 - 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"-(3 ** (4 / 2)) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(-5) + (3 - 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(-12) % ((-5) - 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"2 ** (3 ** (1 + 2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3.** Write a bullet list description of what happens when Python
evaluates\n",
"the statement x += x - x when x has the value 3."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**4.** When a variable is used before it has been assigned a value, a
NameError\n",
"occurs. Write an expression that results in a NameError ."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}

You might also like