# CS21: Quiz 2 Study Guide

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

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

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

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

• basic for loops
• range()
• basic arithmetic expressions using +, -, *, /, **, %
• conversion between the int, float, and str types
• the empty string ""
• string concatenation with +, string repetition with *
• string indexing with []
• for loops over strings
• len()
• tracing a program (show the output)
• if, if/else, if/elif/else
• the relational operators <, <=, ==, >, >=, !=
• the boolean operators and, or, and not
• nested statements (e.g., nested if statements, if/else in a loop; etc)
• print formatting
• accumulator pattern using string, float, or int variables

#### Practice problems:

1. Show the output from the following code fragments:
i = 10
while i < 15:
print(i)
print("--")
i = i + 1
for x in range(6):
print(x, x**2)
print("done!")
text = "hello!"
for i in range(len(text)):
print("-" + text[i] + "-")
1. Write a program that asks the user for their name, and then prints each letter in their name on a separate line.

2. Given these assignments:

x = 5
y = 13
S = "we love computer science"
isDone = False

Show the value and type of each expression below:

x < 10 and y < 10
x < 10 or y < 10
x < 10 and x > 0
x > 10 or x < 0
(5/x) > 7.0
len(S) >= 10
S < S
not isDone
1. Consider the following program
x = 3
print("%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))
1. Trace the program and show it's output
2. Create a table having columns for iteration, i, and x and show how the values change as the loop executes
1. Write a program that asks the user how many grades they want to average, reads in those grades and prints their average.
Enter number of grades to average: 5

Average is: 76.0
Enter a word: repetitive
rEpEtitivE