- Evaluate each expression. Indicate floats by including a decimal
point (so to show 1 as a float, write “1.0”). If any
cannot be evaluated, say why.
`3 + 5.0``10 % 4 + 7 // 2``abs(5 - 20 // 3) ** 4``"If %d + %d = %2.2f, then %s" % (2, 2, 4, "bye")``4 // "3"`

- Convert the following into Python; you may assume the string and math
modules are imported already:
- The volume
*vol*of a sphere is 4π*r*^{3}divided by 3 (remember the result is a floating point number!) - The value of the string variable str with all occurrences of
the letter “
`e`” replaced by the character “`3`” - Subtract 159 from the product of 3 and 27, using integers

- The volume
- What does the following function do when given a list of numbers as
the argument?
`def f(lst):`

a = i = 0

n = len(lst)

while i < n:

if lst[i] <= 0:

i += 1

continue

a += lst[i]

i += 1

return a / n

- Rewrite the function in problem 4 so that it uses a “for” loop, not a “while” loop.
- What does the following program do:
`d = dict()`

while True:

try:

line = input("EOF to stop> ")

except EOFError:

break

for i in line:

d[i] = d.get(i, 0) + 1

u = d.keys()

for i in sorted(u):

print(i, d[i])

- What does the following program do:
`def y(n):`

if n < 10:

return str(n)

else:

d = str(n % 10)

return y(n // 10) + d

print(y(174))

A PDF version is available here.

ECS 10, Basic Concepts of Computing Winter Quarter 2012 |