5.7 Exercises
125
So, using the function add to fill the array, takes 50% longer time!
5.7 Exercises
Exercise 5.1: Nested for Loops and Lists
The code below has for loops that traverse lists of different kinds. One is with
integers, one with real numbers and one with strings. Read the code and write
down the printout you would have got if the program had been run (i.e., you are
not supposed to actually run the program, just read it!).
for i in [1, 2]:
# First indentation level (4 spaces)
print(’i: {:d}’.format(i))
for j in [3.0, 4.0]:
# Second indentation level (4+4 spaces)
print(’
j: {:.1f}’.format(j))
for w in [’yes’, ’no’]:
# Third indentation level (4+4+4 spaces)
print(’
w: {:s}’.format(w))
# First indentation level
for k in [5.0, 6.0]:
# Second indentation level (4+4 spaces)
print(’
k: {:.1f}’.format(k))
Filename: read_nested_for_loops.py.
Exercise 5.2: Exception Handling: Divisions in a Loop
Write a program that N times will ask the user for two real numbers a and
b and print the result of a/b. Any exceptions should be handled properly (for
example, just give an informative printout and let the program proceed with the
next division, if any). The user should also be allowed to stop execution with
Ctrl-c.
Set N = 4 in the code (for simplicity) and demonstrate that it handles different
types of user input (i.e., floats, integers, text, just pressing enter, etc.) in a sensible
way.
Filename: compute_division.py.
Exercise 5.3: Taylor Series, sympy and Documentation
In this exercise, you are supposed to develop a Python function that approximates
sin(x) when x is near zero. To do this, write a program that utilizes sympy to develop
a Taylor series for sin(x) around x = 0, keeping only 5 terms from the resulting
expression. Then, use sympy to turn the expression into a function. Let the program
also plot sin(x) and the developed function together for x in [−π, π].
Filename: symbolic_Taylor.py.
Remarks Part of your task here, is to find and understand the required documentation. Most likely, this means that you have to seek more information than found in
our book. You might have to read about the Taylor series (perhaps use Wikipedia or
125
So, using the function add to fill the array, takes 50% longer time!
5.7 Exercises
Exercise 5.1: Nested for Loops and Lists
The code below has for loops that traverse lists of different kinds. One is with
integers, one with real numbers and one with strings. Read the code and write
down the printout you would have got if the program had been run (i.e., you are
not supposed to actually run the program, just read it!).
for i in [1, 2]:
# First indentation level (4 spaces)
print(’i: {:d}’.format(i))
for j in [3.0, 4.0]:
# Second indentation level (4+4 spaces)
print(’
j: {:.1f}’.format(j))
for w in [’yes’, ’no’]:
# Third indentation level (4+4+4 spaces)
print(’
w: {:s}’.format(w))
# First indentation level
for k in [5.0, 6.0]:
# Second indentation level (4+4 spaces)
print(’
k: {:.1f}’.format(k))
Filename: read_nested_for_loops.py.
Exercise 5.2: Exception Handling: Divisions in a Loop
Write a program that N times will ask the user for two real numbers a and
b and print the result of a/b. Any exceptions should be handled properly (for
example, just give an informative printout and let the program proceed with the
next division, if any). The user should also be allowed to stop execution with
Ctrl-c.
Set N = 4 in the code (for simplicity) and demonstrate that it handles different
types of user input (i.e., floats, integers, text, just pressing enter, etc.) in a sensible
way.
Filename: compute_division.py.
Exercise 5.3: Taylor Series, sympy and Documentation
In this exercise, you are supposed to develop a Python function that approximates
sin(x) when x is near zero. To do this, write a program that utilizes sympy to develop
a Taylor series for sin(x) around x = 0, keeping only 5 terms from the resulting
expression. Then, use sympy to turn the expression into a function. Let the program
also plot sin(x) and the developed function together for x in [−π, π].
Filename: symbolic_Taylor.py.
Remarks Part of your task here, is to find and understand the required documentation. Most likely, this means that you have to seek more information than found in
our book. You might have to read about the Taylor series (perhaps use Wikipedia or
