Program 0: a gentle introduction to Python programming

Due date: Friday, September 8 at 5pm

Instructions

Unless otherwise indicated, use only the Python operations and commands we have described thus far in class.

Start by working with the supplied starter file, but save the file as hw0_yourusername.py where yourusername is your @gm.slc.edu username.

Replace with your name.

Your written (i.e. non-programming) answers must be in the form of comments so that your entire file can load correctly using IDLE.

Fill in all the areas of the supplied file marked by "...".

As mentioned above, name your file hw0_yourusername.py where yourusername is your @gm.slc.edu username. (If your email was msiff@gm.slc.edu, you would name your file hw0_msiff.py.)

When ready to submit your work, email the instructor the file with the subject: ICP17: Programming Assignment 0 (or, preferably, just respond to the email reminding you to submit this file) and attach the one file. (Do not paste the contents of the file into the body of the message. Do not expect the instructor to read anything in the body of the message. The instructor will only examine the attached file.)

Take a number called x. Output the difference between the square of two more than x and the square of one more than x.

Use variables and assignments. Keep your arithmetic expressions simple: no more than a single arithmetic operation per assignment statement. Example:

>>> prose()
Enter a value for x: 4
The difference is 11
>>> prose()
Enter a value for x: 7
The difference is 17

Write a function that asks for two pieces of text, prints the length of each string, then prints the concatenation of the two strings, and finally prints the length of that concatenation. Example:

>>> concat_len()
Enter some text: hello
Enter some more text: nwheels
The first phrase has 5 characters
The second phrase has 7 characters
The concatenation of those phrases is: hellonwheels
That has 12 characters

In a comment, explain why it makes sense for Python to use + for both the usual addition of two numbers and the concatenation of two strings.

Write a function that prints "All work and no play make Jack a dull boy" 50 times without using any kind of loop. In fact, use only variable assignment and string concatenation and no other operations. Try to use as few characters as possible. The phrase "All work and no play makes Jack a dull boy" has 42 symbols (letters and spaces). It can be done in well less than 50 * 42 = 2100 characters. (My solution uses less than 1/10th of that - i.e., less than 210 characters.) Include a comment explaining how this problem demonstrates one of the benefits of using symbolic abstraction.

(Courtesy of Jim Marshall.) Write a program that asks the user for a total number of seconds, and converts this to the corresponding number of hours, minutes, and seconds. Hint: the integer division (//) and remainder (%) operators might come in handy here. Examples: