How transferable is what we’re learning with python to other languages? When taking 35 and beyond with C++ etc., will it mostly just be a matter of learning different, less intuitive syntax, or will we have to change how we’re thinking about CS in general?

We have structured CS21 to emphasize core concepts in computer science. For at least 95% of things we cover, the language only defines how we do something, not what we can do. For loops, types, conditionals, etc. are topics that you will see in any language. For example, CS21 used to be taught in C and we considered doing Java as well. But yes, Python syntax is generally much simpler to follow than other languages.

Why is it important to format templates? Can we program the “old” way and still have clean code?

There are things we can’t do with just the traditional print function, such as specifying significant digits and creating evenly spaced output.

Is there a place holder for a boolean

Not directly. Any type can have a method for telling how to convert to other types. We’ll create new types late in the semester and see how we can define this behavior. For boolean types, we could use %s and python would try to convert to string. Or, we could use %d to convert to an int. This only works if this conversion is defined behind the scenes. Luckily, this is true for Boolean. ("True"/"False" as a string or 1/0 as an int).

Is there a more efficient way than string template?

This is a pretty efficient way to do it, although a little weird when first looking at. There are other format functions, but they behave in similar ways.

What happens if you print %f but don’t do %(name) afterwards?

Python thinks that you literally want to print "%f"

>>> print("You owe $%f")
You owe $%f

what does d in %d stand for?

That’s a great question! It is an inherited designation from older languages, so I’m not totally sure. My guess is for decimal (as in, base 10, not a decimal point). %x is hexadecimal. The other option is for signed integer (%s is already taken by string). %u is unsigned integer (only positive numbers).

Does %.2f always round down?

Good inference! Yes, truncation is the default.

Did we need to start a new accumulator for the second task in the program or could we have just entered a new for loop?

We could have re-used the accum variable and the program would still work.

When would adding width/spaces be useful?

Here is a program where we want to print the swim time as a swimmer takes laps in a pool and there base rate slows 10% (each lap takes 10% longer than the previous one). Without formatting, it looks ugly:

Laps    Split   TotalTime
----    -----   ---------
1   30.0    30.0
2   33.0    63.0
3   36.3    99.3
4   39.93   139.23
5   43.923  183.153
6   48.3153 231.4683
7   53.14683    284.61513
8   58.461513   343.076643
9   64.3076643  407.3843073
10  70.73843073 478.12273803

With width and precision formatting, we get much nice tables (I used %10.2f for the last two columns and %4d for the first column):

Laps      Split Total Time
----      ----- ----------
   1      30.00      30.00
   2      33.00      63.00
   3      36.30      99.30
   4      39.93     139.23
   5      43.92     183.15
   6      48.32     231.47
   7      53.15     284.62
   8      58.46     343.08
   9      64.31     407.38
  10      70.74     478.12

What else can we use accumulators for?

We’ll see the pattern in many different contexts - including for boolean types and lists!

What are some examples of when string accumulators would be used in real-life setting?

Cryptography - the study of hiding messages - requires transforming a string from some input that we can read to some garbled version that is unreadable by a human. Both the encoding (English to cryptic message) and the decoding (cryptic to English) would require looping over the string and accumulating the translation.

Translating a text into a foreign language would follow a similar pattern.

Can you use the same variable in more than one place when formatting with %

Sure!

Do you ever initialize a string accumulator at a value and not just ""?

It’s not as common, but it depends on the application. For example, certain communications require a “header” that tells the receiver what format the text will be (e.g., the file format for a music file). Since that header is always the same, we could initialize our accumulator to the header value.

If a float has 0 precision will it be treated as a float or an int?

It’ll be treated as a float, but it will appear as an int since we won’t see the decimal point or anything after.

Can you print multiple lines of code and use string formating throughout?

Yes! Each print statement can use formatting (and you can also have print statements that don’t use formatting in the same program).

Is it a default that %f have 6 decimal points?

I believe so.

I am still confused about the function of the percentage sign but I think I can figure it out.

It is a special syntax that notifies Python that what follows should be printed exactly, but rather is a placeholder for what comes after.

What is the difference between brackets and parenthesis?

Brackets are always related to sequences (strings and lists). We use them to index into a sequence (name[1], word[i]). Parentheses are used to group things together. If there is a function involved, such as range(), print(), and len(), they are used to tell Python what to run the function on. For example, we say range(5) and we need the parentheses around the 5 so Python knows what to calculate the range of.

For a quiz, do you recommend using string formatting or does it not matter

No! It is not very important for quizzes, but is important for labs. You don’t need it for Quiz 1.