The Python interpreter has strict rules for variable names. Which of the following are legal Python variable names? If the variable name is not legal, state the reason.
It is important that we know the type of the values stored in a variable so that we can use the correct operators. Python automatically infers the type from the value you assign to the variable. Write down the type of the values stored in each of the variables below. Pay special attention to punctuation: values are not always the type they seem!
To verify your answers, you can use the interactive Python shell, but first try to do the exercise without help.
>>> a = False False >>> type(a) <type 'bool'> >>>
String operators might be a little less intuitive than those on numbers. This exercise will give you a chance to practice those. Given the following variables:
look = 'Look at me!' now = ' NOW'
What are the values of the following expressions?
You can use the interactive Python shell if you are unsure.
This exercise will let you explore the string operators we discussed in class. Start IDLE and create a new file. Cut and paste the following code in it:
name = 'Harry Potter' greeting = 'Hello, world!' goodbye = 'Goodbye, all!' space = ' ' star = '*'
Save the file as greetings.py.
Now write a series of print statements to print out a greeting box and a goodbye box for the name. Use only the first name in the greeting and only the last name in the goodbye message, e.g.
*******************
* *
* Hello, Harry!!! *
* *
******************* ***********************
* *
* Goodbye, Potter!!! *
* *
***********************
but you are only allowed to use the variables above. For example, if you wanted to draw the letter T using stars, you would do:
print star * 3 print space + star + space print space + star + space
The answer below would not be acceptable:
print '***' print ' * ' print ' * '
Refer to the slides and/or text if you are trying to remember the different operators or you can find them at http://en.wikibooks.org/wiki/Python_Programming/Strings#String_operations as well.
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit ps2 greetings.py
(Do not worry if the printer is acting weird. Just make sure you submit the file electronically.)
Boolean operators can seem tricky at first, and it takes practice to evaluate them correctly. Write the value (True or False) produced by each expression below, using the assigned values of the variables a, b, and c.
a = False b = True c = False
Hint: Work from the inside out, starting with the inner-most expressions, like in arithmetic.
The order of evaluation for an arithmetic or logical expression is determined by operator precedence rules. For each of the following expressions, add parentheses so that the precedence is explicit, and then write down the value of the expression. Then add parentheses that change the value of the expression, and write the new value (you only need to find one example of this). For example:
Example: 3 * 4 + 60 / 2 Answer: (3 * 4) + (60 / 2) = 42 3 * (4 + 60) / 2 = 96
a. 4 + 3 * 4 + 4
b. 4.0 / 3.0 + 1
c. not False and (3 > 4)
In this exercise, we will ask the user for her first and last name and date of birth and print them out formatted. Create a new file and call it userinput.py. Here is an example of what the program would do:
Enter your first name: Harry
Enter your last name: Potter
Enter your date of birth:
Month? July Day? 31
Year? 1980
Harry Potter was born on July 31, 1980
The text in italics is the what the user inputs.
To print a string and a number in one line, you just need to separate the arguments with a comma. The print statement adds a space between the two arguments.
print 'October', 20
Or you can convert the number to a string and then use the string operator:
print 'October ' + str(20)
We will see more on formatting output later.
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program. (Do not worry if the printer is acting weird. Just make sure you submit the file electronically.)
Submit the file electronically by typing:
athena% submit ps2 userinput.py
Zeller’s algorithm computes the day of the week on which a given date will fall (or fell). In this exercise, you will write a program to run Zeller’s algorithm on a specific date. You will need to create a new file for this program, zellers.py. The program should use the algorithm outlined below to compute the day of the week on which the user's birthday fell in the year you were born and print the result to the screen.
Start with the program in the previous exercise but ask for the month as a number between 1-12 where March is 1 and February is 12. If born in Jan or Feb, enter previous year. (See the notes below) In the end, print out the name of the user and on what day of the week they were born.
Zeller’s algorithm is defined as follows:
Let A, B, C, D denote integer variables that have the following values:
• A = the month of the year, with March having the value 1, April the value 2, . . ., December the value 10, and January and February being counted as months 11 and 12 of the preceding year (in which case, subtract 1 from C) • B = the day of the month (1, 2, 3, . . . , 30, 31) • C = the year of the century (e.g. C = 89 for the year 1989) • D = the century (e.g. D = 19 for the year 1989)
Note: If the month is January or February, then the preceding year is used for computation. This is because there was a period in history when March 1st, not January 1st, was the beginning of the year.
Let W, X, Y, Z, R also denote integer variables. Compute their values in the following order using integer arithmetic:
W = (13*A - 1) / 5 X = C / 4 Y = D / 4 Z = W + X + Y + B + C - 2*D R = the remainder when Z is divided by 7
The value of R is the day of the week, where 0 represents Sunday, 1 is Monday, . . ., 6 is Saturday. If the computed value of R is a negative number, add 7 to get a nonnegative number between 0 and 6 (you don't need to do this in the code). Print out R. You can check to be sure your code is working by looking at http://www.timeanddate.com/calendar/.
Turn in the answers to Exercises 1, 2, 3, 5, 6 and 8 (if you've done it) on this paper itself.
Print a copy of the source code files and turn it in. Make sure your name is in the comment section of your program. (Do not worry if the printer is acting weird. Just make sure you submit the file electronically.)
Submit the file electronically by typing:
athena% submit ps2 greetings.py
athena% submit ps2 userinput.py
athena% submit ps2 zellers.py