Quiz 1 Study Guide

You are responsible for all material needed for Lab 1, plus basic for loops from Week 2 (see below for examples). We will not be covering the accumulator pattern or indexing on this quiz. We will also not be testing you on Unix or emacs information.

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, on Slack, or with meetings with faculty and staff. We do not provide full solutions to the study guides.

Gradescope

You should have received an invitation to Gradescope. The practice quiz included in Gradescope will not be graded, but will give you the chance to set up your account and practice using the interface before the first quiz.

The first question on every real (not practice) quiz will ask you to sign sign an integrity policy that affirms that your work is your own and that you have not used any unauthorized outside sources.

Terms

You should understand the following terms:

  • syntax

  • semantics

  • algorithm

  • program

  • Python as a programming language

  • computer

  • variable

  • assignment

  • data types: int, float, str, list, range

Python concepts and functions

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

  • assignment using =

  • basic arithmetic expressions using +, -, \*, /

  • reading in strings using input()

  • int(), str(), float(), list() and type conversion

  • print()

  • def main():

  • basic for loops

  • string concatenation and string multiplication

  • basic list and string indexing

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.

$ python gallons.py
Gallons: 2
That's 7.58 liters.

2) What is the value and type for each of the following expressions? 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 (or integer).

  • 2 + 3

  • 2.0 + 3

  • 5 - 2 * 3

  • "123"

  • 2.0

  • 2+4*2+1

  • "Swat" + "CS"

  • [1, 2, 3]

  • What sequence of numbers would range(1,7,2) 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.

$ python 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 =", x)
print("done!")
n = 6
ch = "a"
for i in range(1,n+1):
    print(i, ch*i)

5) Write a program that asks the user for their name and a number (n), then prints out their name n times.

$ python name.py
name: cs21
n: 6

cs21
cs21
cs21
cs21
cs21
cs21