# CS21: Quiz 2 Study Sheet

### In addition to all concepts from Quiz 1...

#### You should be able to define the following terms:

• data types: integer, float, string, boolean, list
• branching and looping
• the Accumulator design pattern
• string and list indexing

#### You should understand and be able to use the following Python concepts:

• basic arithmetic expressions using `+`, `-`, `*`, `/`, `**`, `%`
• conversion between the int, float, and str types
• reading in strings using `input()`
• the empty string ""
• string concatenation with +, repetition with *
• string and list indexing with []
• for loops over strings and lists
• `len()`
• tracing a program (show the output)
• if, if/else, if/elif/else
• the relational operators <, <=, ==, >, >=, !=
• the membership `in` operator
• the boolean operators `and`, `or`, and `not`
• nested statements (loop in a loop; loop in an if/else; etc)
• string formatting

#### Practice problems:

1) Show the value and type of each of the following, given these assignments:

`x = 5y = 13L = ["pony", "zebra", "donkey", "unicorn"]S = "we love computer science" x < 10 and y < 10x < 10 or y < 10x < 10 and x > 0x > 10 or x < 0(5/x) > 7.0"LOVE" in Slen(S) >= 10range(len(L))len(L)"donkey" in L"don" in L"don" in L`

2) Trace the following program and show it's output:

`x = 3print("%d  %d" % (0,x))for i in range(5):    if x % 2 == 0:        x = x / 2    else:        x = (3*x+1)/2    print("%d  %d" % (i,x))`

3) What will each of the following `for` loops print to the screen?

`S = "ABCDEFG"for i in S:  print(i) for i in range(len(S)):  print(i, S[i])`

4) Write a program that accepts 5 grades (0-100) from the user and displays the average of all 5 grades, as well as the corresponding letter grade for each input (where A is >= 90, B is 80-89, etc):

`grade 1: 96 that's an Agrade 2: 86 that's a Bgrade 3: 76 that's a Cgrade 4: 66 that's a Dgrade 5: 56 that's an F the average of those is: 76.0`

5) Write a program that asks the user to enter some text and then displays that text with all e's and E's replaced with 3's, and all s's and S's replaced by 5's. For example, if the user enters the phrase "WE love COMPUTER science", your program would display this: W3 lov3 COMPUT3R 5ci3nc3