Professional Documents
Culture Documents
Procedure
1. Form pairs as directed by your teacher. Meet or greet each other to practice
professional skills and establish norms.
3. If your teacher directs you to turn in your work with an IPython log, set the
working directory for the IPython session, turn on session logging, and title your
log.
4. Start a new program in the code editor by choosing File > New > Python file.
Save the file as JDoe_BSmith_1_3_5.py
Wow!
Out[]: str
In []: slogan
Which of these types can represent the number six million? A correct answer
should include all but the bool type but would still be correct by including the
bool type.
Data of type int, float, long, and str all could easily be used represent the number
six million. If represented as a string, it would have to be converted to another
type before arithmetic were performed with it. The point of the question is to
point out that numbers are represented with strings – such as in text files or
reading from user keyboard entry – and to give opportunity for discussion about
why arithmetic nevertheless doesn't use strings.
A student thinking beyond the intent of the question would also be correct in
thinking that six million could also be represented by a sequence of bools.
TFTTFTTTFFFTTFTTFFFFFFF
7. String literals are enclosed in single or double quotes. The opening and closing
quotes must match.
One of the following two inputs will produce an error. Try this, discuss both
outputs with your partner, and summarize your discussion.
In []: type('tr' + 5)
You can't add a string to an int. A string is not treated as numeric data by various
operators and functions.
8. Strings are iterables. Iterables are sequences that can be counted in order, one
at a time, during iteration. Strings contain a sequence of characters, one after
another. The elements — including the spaces — are indexed, starting at 0.
Character M y space s c h o … t
Index # 0 1 2 3 4 5 6 … 20
Try this, discuss the outputs with your partner, and summarize your discussion.
In []: slogan[0]
Out[]: 'M'
In []: slogan[2]
In []: slogan[8]
In []: slogan[26]
9. Python allows iterables to be sliced. To slice, use square brackets and two
indices separated by a colon. Python returns the iterable from the beginning
index up to but not including the ending index.
In []: slogan[5:21]
When slicing, you can omit the starting (or ending) index if you want to start at
the beginning (or end at the ending) of the string.
In []: slogan[:5]
Try to return 'best' by slicing the variable slogan, omitting the end index.
In []: # 9. Slicing
slogan[-4:] or slogan[17:]
variable[:] + 'fsd'
11. The len() function returns the number of elements in an iterable. The index of
the last element is always one less than the length of the iterable since the
indices begin at 0.
In []: len(slogan)
Out[]: 21
In []: len(activity)
Out[]: True
13. A social media site offers a contest to write a humorous short paragraph. A
constraint on the creative format: the entry must include a question, a quote, a
compound sentence, and an exclamation. These would contain the characters ?,
", ,, and !, respectively.
Out[]: 4
Out[]: 1
Conclusion
1. How many characters are in this sentence? Does it matter whether Python is
storing the string as one byte per character or four bytes per character?
It is likely to be stored in 41 consecutive bytes, but this depends on how the string
is represented by a particular interpreter or compiler. The details usually don't
matter if your focus is at the higher level of abstraction of an algorithm that uses
the string.
2. This question asks you about something you have not learned. In fact, the
question is asking about details that go beyond what you will learn in this course.
However, wondering what is going on at a lower level of abstraction – and talking
about it – can be a useful strategy when learning about computing.
Describe what you think occurs in memory when the following code is executed.
Answers will vary. What actually happens in memory? Students might not have the
conception that there are different types of memory: registers that are part of the CPU, a
cache that is part of the CPU, RAM, hard drive, and flash storage. The "correct" answer
to this question is not important. The purpose of the question is to discover students'
conceptions about memory, instructions, and variables. In fact, there is no single correct
answer for all of Python, because the memory representation of strings varies from
CPython to IronPython to Jython.
What actually happens in memory in CPython, which we are using? To dive down the
ladder of abstraction, you would look at the code for the Python interpreter, which is
written in C and maintained using the Mercurial version control system:
http://hg.python.org/cpython/file/1411df211159/Objects/bytearrayobject.c