Quiz 1 Study Guide
You are responsible for all material needed for Lab 2, plus basic if-else
statments and relational operators and from early Week 3 (see below
for examples).
We will not test you on Unix or vscode
.
Study guides are not graded. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, or with meetings with faculty and staff. We do not provide full solutions to the study guides.
The study guide is not indicative of length, difficulty, or format of the quiz.
Terms
You should understand the following terms:
-
computer
-
algorithm
-
program
-
Python as a programming language
-
variable
-
assignment
-
loop
-
data types:
int
,float
,str
,bool
Python concepts and functions
You should be able to use the following Python concepts and functions:
-
assignment using
=
-
reading in strings using
input()
-
int()
,str()
,float()
, and type conversion -
print()
-
def main():
-
arithmetic expressions using +, -, *, /
-
string operators: concatenation
+
, repetition*
,len()
function -
for
loops -
range()
function -
accumulator pattern with numbers and strings
-
relational operators
==
,!=
,<=
,<
,>=
,>
-
basic
if
,if-else
(no chainingif-elif
on quiz 1)
Sample practice problems
1) Write a program to convert a certain number of gallons (entered by the user) to liters. There are 3.79 liters in a gallon.
$ python3 gallons.py
Gallons: 2
That's 7.58 liters.
2a) What is the value and type for each of the following
expressions? Figure them out by hand first, then try them in python3
to check
your answers. For example, the answer to the first one (2 + 3
) would be
value 5
and type int
.
-
2 + 3
-
2.0 + 3
-
5 - 2 * 3
-
"123"
-
2.0
-
"Swat" + "CS"
-
7 / 2
-
"hi"*4
-
6 < 10
-
5 != 3
2b) What sequence of values does range(6)
produce?
3) Write a program that takes a user’s hotel room rate and outputs the cost after adding the 10% city tourism fee. The program should then output the total bill.
$ python3 bill.py
Room rate: 100
Total nights: 3
-------------------
Tourism Fee: $ 30.0
Total: $ 330.0
4) Show the output from the following code fragments:
for i in range(4):
print(i)
print("--")
for x in range(6):
print("x = " + str(x))
print("done!")
n = 6
v = 2
for i in range(n):
x = i + 1
print(str(x) + " " + str(x * v))
5) Write a program that asks the user for their name and a number (n
),
then prints out their name n
times.
$ python3 name.py
name: cs21
n: 6
cs21
cs21
cs21
cs21
cs21
cs21
6) Write a program that asks the user for an int value (n
) and prints
out the following pattern of of a
and b
s:
$ python3 abs.py
n: 6
abababababab
ababababab
abababab
ababab
abab
ab
$ python3 abs.py
n: 4
abababab
ababab
abab
ab
7) Write a program that asks the user for an int value (n
) and
computes the sum of the first n even numbers. So if the user
enters 2
your program will compute: 2
+ 4
, and if the user
enters 3
your program will compute: 2
+ 4
+ 6
, and if the user
enters 4
your program will compute: 2
+ 4
+ 6
+ 8
, and
so on.
$ python3 sumeven.py
n: 1
result is 2
$ python3 sumeven.py
n: 2
result is 6
$ python3 sumeven.py
n: 3
result is 12
8) The code fragment below asks the user for two input values.
-
What is the program’s output if the user enters the value
7
? -
What is the program’s output if the user enters the value
20
?
n = int(input("Enter the first number: "))
if (n < 10):
print("cs21 ")
else:
print("is ")
print("awesome!")
9) Write a program that asks the user to enter two numbers and prints out the square of the larger of the two.
$ python3 square.py
enter a number: 8
enter another number: 7
the square of the larger of 8 and 7 is: 64
$ python3 square.py
enter a number: -11
enter another number: 4
the square of the larger of -11 and 4 is: 16