python quiz wrapup

Programming Concepts and Problems


When we ask for output:

You DON'T have to write the spaces/newlines in.

Program Text:

print "X",
print "X",
Output:
XX
Day 1: Variables, Operators, and Expressions
Tips/Notes:
Variables are typed (e.g., this is a string, that's an integer, etc.).

type(x) is a function that returns the type of its parameter.

You can convert information from one type to another using the built-in conversion functions:

int(x), str(x), float(x), bool(x).

These functions fail for conversions that don't make sense. (Example: int("hello") crashes.)

Every variable you create should have a meaning.

Use meaningful names for variables instead of generic ones like a, b, or c. (Exception: Quiz-related scenarios.)

Problem 1: Neophyte
Question:
What is the output of the following code?

Program Text:
python
a = 5
b = a + 7
a = 10
print b
Output:
12
Problem 2: Type Theory
Question:
What is the type of each of the following expressions (using the type function)?

Program Texts and Outputs:
Program Text:

python
print type(5)
Output: int

Program Text:

python
print type("abc")
Output: string

Program Text:

python
print type(True)
Output: boolean

Program Text:

python
print type(5.5)
Output: float

Program Text:

python
print type(12/27)
Output: float

Program Text:

python
print type(2.0/1)
Output: float

Program Text:

python
print type(12 ** 3)
Output: int

Program Text:

python
print type(5 == "5")
Output: boolean

Program Text:

python
a = str((-4 + abs(-5) / 2 ** 3) + 321 - ((64 / 16) % 4) ** 2)
print type(a)
Output: string

Problem 3: Expressive Expressions
Question:
What is the output of the following code?

Program Texts and Outputs:
Program Text:

python
print 5 == 5.0
Output: True

Program Text:

python
print float(1/2)
Output: 0.0

Program Text:

python
print float(1)/2
Output: 0.5

Program Text:

python
print 5 == "5"
Output: False

Program Text:

python
print "sdf" != "sdf"
Output: False

Program Text:

python
print True and (False or not True)
Output: False

Program Text:

python
print str(53) + str(True)
Output: 53true

Program Text:

python
a = 20
print 15-(a-15), ",",
a = 10
print 15-(a-15)
Output:

10, 20
Day 3: Conditionals
Tips/Notes:
The if statement executes a sequence of statements only if some condition is true.

This condition can be anything.

elif / else is optional.

At most one block of statements is executed.

else occurs if none of the above conditions are satisfied.

Problem 4: Basics
Question:
Consider the following code:

Program Text:
python
a = ?

if a > 10 and a % 6 == 3:
    print "a",
elif a > 10 and a < 20:
    print "b",
else:
    print "c",
Task:
Give a value for a that would produce the following outputs:

Value of a	Output
none	a b
15, 21, 27, …	a
11, 12, …, 19 (except 15)	b
Any other	c
none	victory is mine!

Quiz

To mark this module as complete, you must finish this quiz. Once submitted, you'll need to wait 2 hours before attempting it again.