Warmup Part 3

Part E: String Concatenation

Recall that when we write text between quotation marks "like this", it is a string. We can join two strings together using a +. Let’s try it out!

Select your terminal, and then type python3 without anything following it. You should see something similar to the screenshot below:

We’ll refer to this tool as the Python console. Instead of running files in the Terminal, we can run individual lines of code in the Python console after the >>> symbols. This code is not saved into a file, but it’s helpful for double checking specific statements, like the ones we’re about to do with strings and numbers. It’s similar to how you’ve used a calculator to run individual arithmetic expressions.

ReadMe

To close the console, you can simply type quit() to return to the normal terminal mode.

In the Console, enter print("one" + "two" + "three") and press enter. Notice how it joined, or concatenated, the strings into a single string and printed onetwothree.

Now, enter print("one", "two", "three"). Notice that this one has a comma rather than a + between each string. Compare the output to the previous print.

What’s happening here is that Python’s print() will print each of the items given to it with spaces between them. Handy!

With a partner, predict what each of the following lines prints out. Then run them each to check your predictions. Note carefully which ones have pluses and which ones have commas. Pay careful attention to the spaces in each line and in the output.

print("Computer", "Science", "is", "great", "!")
print("Computer",      "Science",      "is","great","!")
print("Computer", "Science" + "is", "great" + "!")
print("Computer ", "Science " + "is ", "great" + "!")

Finally, write a print() line that uses "Computer", "Science", "is", "great", and "!" with some commas and a + to print the message below.

Computer Science is great!

Write up your notes and answers for this exercise in WARMUP.md.

Part F: Modulus

Recall that we can compute the remainder of dividing x by y using the modulus operator: x % y. For each of the following, work with your partner to figure out what the result will be. Then use the Console to compute them and check your work.

  • 25 mod 4
  • 12 mod 3
  • 5 mod 13
  • 0 mod 10

One thing we can do with a modulus is compute the ones digit of a number. For example, the ones digit of 123 is 3. Given any integer x, what number m should you mod x by to get the ones digit? In other words, we want to determine the m such that 123 % m returns 3.

Test that your value of m works by checking that 51 % m gives 1 and 1028 % m gives 8.

We can do a similar thing with units of time. For example, if working on this warmup takes 5000 seconds, then we can divide by 60 to figure out how many full minutes it takes and we can mod by 60 to figure out how many additional seconds it takes. In this case, 5000 seconds is 5000 // 60 minutes and 5000 % 60 seconds. Go ahead and compute those expressions using the Console.

We can do something similar with hours and minutes. With your partner, work out how to compute the number of hours and minutes there are in 83 minutes.

Assuming this whole lab takes 15000 seconds, how many hours, minutes, and seconds is that? Write up your notes and answers for this exercise in WARMUP.md.