@Itsu
2015-05-13T04:48:23.000000Z
字数 11595
阅读 2530
What is the difference between an Algorithm and a Program?
True or False? A computational mode of thinking means that everything can be viewed as a math problem involving numbers and formulas.
True or False? Computer Science is the study of how to build efficient machines that run programs.
The two things every computer can do are:
True or False?
Declarative knowledge refers to statements of fact.
Imperative knowledge refers to 'how to' methods.
The recipe for deducing square root cannot be made into a real computer program because computers cannot guess a starting value for y.
True or False? A stored program computer is designed to compute precisely one computation, such as a square root, or the trajectory of a missile.
True or False? A fixed program computer is designed to run any computation, by interpreting a sequence of program instructions that are read into it.
A program counter
What does it mean when we say that "the computer walks through the sequence executing some computation"?
True or False? In order to compute everything that is computable, every computer must be able to handle the sixteen most primitive operations.
In this course, you will be using a programming environment to write Python programs. We recommend you install and use the Enthought Canopy Python Distribution. If you are taking this course again and have an older version of Canopy installed, we urge you to update to the latest version from the Welcome screen or the Help menu or to uninstall and reinstall the newest version.
Note that previous offerings of this course used IDLE as a programming environment. IDLE is mentioned in some of the lectures but you can use Canopy instead. The course staff can offer support if you use IDLE or Canopy.
This initial problem set is simply intended to help you install and test your Python computing environment for use in 6.00.1x this semester. There is no graded assignment for this problem set, but you should still do it as soon as possible. By the end of this problem set, you should be able to execute simple Python commands and expressions, create a file with Python code, and run it in a Python environment.
If you have the ability to install files on your own machine, we strongly recommend that you do so. This will give you the ability to complete your assignments - and future projects after 6.00.1x - on your machine, at any time, regardless of Internet connectivity.
For this course, we will be using Enthought Canopy Python Distribution, which you can download here.
NOTE: Do NOT install any version of Python 3. In 6.00.1x, we will be using Python version 2. 7, and ask that you do the same. If you use any other version of Python 3, your solutions will not be compatible with our tutor platform. Some of your answers will be incorrect. Enthought Canopy provides the proper version of Python, so you don't need to worry about this if you are using Enthought.
How to get started with Enthought
Enthought Canopy includes:
This Canopy application is similar to Python's built-in Idle editor, but is easier to use, more powerful, and suitable for use in your future Python projects.
Your first task is to download Enthought Canopy onto your machine and install it. By clicking on the download link above, you will be taken to a Canopy download web page, which should recognize your operating system and provide the correct version. The next three pages in this problem set present specific instructions for Windows, Mac, and Linux operating systems. Choose the instructions for your computer’s operating system and read on!
If you have questions about installation, post a question in the forum. For general questions, post in the forum at the bottom of this page. For questions specific to your operating system, please post in the forum at the bottom of the specific page that discusses installation instructions for Windows, Mac or Linux.
The course officially uses Canopy, but If you have other IDEs that you have used and recommend, post a description and a link to them in the "Related Resources" section below.
For each of the following expressions, indicate the type of the expression. While you could simply type these expressions into an IDLE or Canopy shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.
Note: The Python 'None' keyword
In Python, the keyword None is frequently used to represent the absence of a value.None is the only value in Python of type NoneType.
For each of the following expressions, indicate the value returned, or if the evaluation would lead to an error, write the word 'error' (note this is a word, not a string). While you could simply type these expressions into an IDLE or Canopy shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.
For decimal answers, give the full result, or at least four decimal places of accuracy (whichever is shortest).
In this class, we will be using Python 2. 7 and not Python 3. 0. Take a look at Problem Set 0 to see how to install a Python Integrated Development Environment, if you would like to start typing in the expressions in these and future exercises to see their result.
For each of the following expressions, indicate the value returned, or if the evaluation would lead to an error, write the word 'error' (note this is a word, not a string). While you could simply type these expressions into an IDLE or Canopy shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.
Hint: Python boolean types
Remember that in Python words are case-sensitive. The word Ture is a Python keyword (it is the value of the Boolean type) and is not the same as the word true. Refer to the Python documentation on Boolean values.Hint: Priority order of Boolean operations
For these problems, it's important to understand the priority of Boolean operations. The order of operations is as follows:
- Parentheses. Before operating on anything else, Python must evaluate all parentheticals starting at the innermost level.
not
statements.and
statements.or
statements.What this means is that an expression like
not True and False
evaluates to
False
, because thenot
is evaluated first (not True
isFalse
), then theand
is evaluated, yieldingFalse and False
which isFalse
.However the expression
not (True and False)
evaluates to
True
, because the expression inside the parentheses must be evaluated first -True and False
isFalse
. Next thenot
can be evaluated, yieldingnot False
which isTrue
.Overall, you should always use parenthesis when writing expressions to make it clear what order you wish to have Python evaluate your expression.
As we've seen here,not (True and False)
is different from(not True) and False
- but it's easy to see how Python will evaluate it when you use parentheses. A statement like notTrue and False
can bring confusion!
For each of the following expressions, indicate the type of the expression and the value returned, or, if the evaluation would lead to an error, choose the type 'NoneType' and write the word 'error' (note this is a word, not a string) as the value returned.
While you could simply type these expressions into an IDLE or Canopy shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.
Below is a transcript of a session with the Python shell. For each expression being evaluated, provide the type and the value the expression returns. If evaluating an expression would cause an error, select 'NoneType' and write the word 'error' (note this is a word, not a string) in the box. While you could simply type these expressions into an Idle shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.
Assume that the expressions are evaluated in the order shown - that is, each problem part is evaluated directly after the previous problem part(s).
For each of the following expressions, indicate the value returned, or if the evaluation would lead to an error, write the word 'error' (note this is a word, not a string). While you could simply type these expressions into an Idle shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.
For each of the expressions below, specify its type and value. If it generates an error, select type 'NoneType' and write the word 'error' (note this is a word, not a string) in the box for the value. While you could simply type these expressions into an Idle shell, we encourage you to answer them directly since this will help reinforce your understand of basic Python expressions.
Assume we've made the following assignments:
> str1 = 'hello'
> str2 = ','
> str3 = 'world'
Note: The Python 'in' operator
The operatorsin
andnot in
test for collection membership (a 'collection' refers to a string, list, tuple or dictionary - don't worry, we will cover lists, tuples and dictionaries soon!). The expression
element in coll
evaluates to
True
ifelement
is a member of the collectioncoll
, andFalse
otherwise.The expression
element not in coll
evaluates to
True
ifelement
is not a member of the collectioncoll
, andFalse
otherwise.
Note this returns the negation ofelement in coll
- that is, the expressionelement not in coll
is equivalent to the expressionnot (element in coll)
.Note: Advanced String Slicing
You've seen in lecture that you can slice a string with a call such ass[i:j]
, which gives you a portion of strings
from indexi
to indexj-1
. However this is not the only way to slice a string! If you omit the starting index, Python will assume that you wish to start your slice at index 0. If you omit the ending index, Python will assume you wish to end your slice at the end of the string. Check out this session with the Python shell:
\>>> s = 'Python is Fun!'
\>>> s[1:5]
'ytho'
\>>> s[:5]
'Pytho'
\>>> s[1:]
'ython is Fun!'
\>>> s[:]
'Python is Fun!'
That last example is interesting! If you omit both the start and ending index, you get your original string!
There's one other cool thing you can do with string slicing. You can add a third parameter,k
, like this:s[i:j:k]
. This gives a slice of the strings
from index ito indexj-1
, with step sizek
. Check out the following examples:
\>>> s = 'Python is Fun!'
\>>> s[1:12:2]
'yhni u'
\>>> s[1:12:3]
'yoiF'
\>>> s[::2]
'Pto sFn'
The last example is similar to the example s[:]. With
s[::2]
, we're asking for the full strings
(from index 0 through 13), with a step size of 2 - so we end up with every other character ins
. Pretty cool!
Write a piece of Python code that prints out the string hello world
For each of the following expressions, indicate the value that prints out when the expression is evaluated. If the evaluation would lead to an error, write the word 'error'; if nothing would print out, write the word 'blank'.
While you could simply type these expressions into an IDLE or Canopy shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.
PS: If the temperatures seem weird to you, like most of the world, you probably use the Celsius system. We Americans still use the crazy Fahrenheit system…
Write a piece of Python code that prints out the string 'hello world'
if the value of an integer variable, happy
, is strictly greater than 2.
For problems such as these, do not include raw_input
statements or define the variable happy
in any way. Our automated testing will provide a value of happy
for you - so write your code in the following box assuming happy
is already defined.
Assume that two variables, varB
and varB
, are assigned values, either numbers or strings.
Write a piece of Python code that prints out one
of the following messages:
"string involved"
if either varA
or varB
are strings"bigger"
if varA
is larger than varB
"equal"
if varA
is equal to varB
"smaller"
if varA
is smaller than varB
For problems such as these, do not include raw_input
statements or define the variable varA
or varB
. Our automating testing will provide values of varA
andvarB
for you - so write your code in the following box assuming varA
and varB
are already defined.