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 atom information.

Your quiz will be on paper, so be sure to practice the concepts below on paper, including writing simple programs on paper.

Quiz Study Guides are provided as a courtesy. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, on piazza, or with meetings with faculty and staff. We do not provide full solutions to the quiz guides.

Terms

You should understand the following terms:

  • syntax

  • semantics

  • algorithm

  • program

  • Python as a programming language

  • computer

  • variable

  • assignment

  • data types: int, float, str

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() and type conversion

  • print()

  • def main():

  • basic for loops

  • string concatenation

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.

  • 2 + 3

  • 2**3

  • 2.0 + 3

  • 5.5/10

  • 5 - 2 * 3

  • "123"

  • 48 % 2

  • 2.0

  • 2+4*2+1

  • "Swat" + "CS"

For example, the answer to the first one (2 + 3) would be value 5 and type int (or integer).

3) Write a madlib program that asks the user for 2 colors and one adjective, then prints out the filled-in poem:

$ python3 roses.py
    color: pink
    color: azure
adjective: funny

Roses are pink
Violets are azure
Sugar is funny
and so are you!

4) Write a program that takes a user’s hotel room rate and outputs the cost of a 6% sales tax and 10.5% city tourism fee. The program should then output the total bill.

$ python bill.py
Room rate: 100
Total nights: 3
-------------------
Tax: $ 18.0
Tourism Fee: $ 31.5
Total: $ 349.50

5) 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
ch = "*"
for i in range(1,n+1):
    print(i, ch*i)

6) 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: jeff
n: 6

jeff
jeff
jeff
jeff
jeff
jeff