Tuesday, December 2, 2014

Activity 1.3.5 - Strings

Tweet Verifier Code:


Conclusion Questions:

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?

There were 41 characters in the first sentence of the question. It shsouldn't matter how the string is stored, as long as it's at least one byte, since all alphabetical characters can be stored via ASCII code within a single byte.

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.

In []: a = 'one string'
In []: b = 'another'
In []: c = a[:3] + ' and ' + b
In []: print(c[6:10])

This code concatenates several pieces of string together and prints a section of the concatenation. "a" and "b" are stored in the memory as 'one string' and 'another', respectively. Then the memory is storing a different variable, "c", as the concatination of the first three letters in "a" ('one'), ' and ', and all of "b" ('another'). This creates the string 'one and another', which is what the memory stores "c" as. Then part of c is printed, from the 6th to 10th character, counting the former and having the first character be counted as the 0th.

This piece of code should print the string 'd an', and, after testing in Canopy, it does. Yay!

No comments:

Post a Comment