Sample Midterm 1
-
Which of the following Python identifiers are legal variable names?
For those that are not, say why not.
- x
- Celsius
- spam3
- <hello>
- question?
-
What is the data type (int, long int, float, string, list) of the following:
- 3
- 3.0
- 3L
- "hello"
- 'x'
- [ 1, 2, 3 ]
-
Write the following formulas in Python:
- 7 × 9
- 42
-
What does the following program print?
for i in "hello":
print i, "-->", chr(ord(o)+1);
-
What does the following program print?
for i in [1, 7, 4]:
x = (-1) ** i * i;
print i, x;
-
When I ran the following program:
x = input("Enter a number: ");
y = 1.0 / x;
print "The reciprocal of", x, "is", y;
I got the following error message:
Traceback (most recent call last):
File "test.py", line 2, in <module>
y = 1 / x;
ZeroDivisionError: integer division or modulo by zero
What caused the error? Please be specific.