Exercises

Ex. 1 → Execute the following statements:

    L = [1, 2]
    L3 = 3*L
  1. What is the content of L3?
  2. Try to predict the outcome of the following commands:
          L3[0]
          L3[-1]
          L3[10]
  3. What does the following command do?
           L4 = [k**2 for k in L3]
  4. Concatenate L3 and L4 to a new list L5.

Ex. 2 → Use the range command and a list comprehension to generate a list with 100 equidistantly spaced values between 0 and 1.

Ex. 3 → Assume that the following signal is stored in a list:

    L = [0,1,2,1,0,-1,-2,-1,0]

What is the outcome of:

L[0]
L[-1]
L[:-1]
L + L[1:-1] + L
L[2:2] = [-3]
L[3:4] = []
L[2:5] = [-5]

Do this exercise by inspection only, that is, without using your Python Shell.

Ex. 4 → Consider the Python statements:

L = [n-m/2 for n in range(m)]
ans = 1 + L[0] + L[-1]

and assume that the variable m has been previously assigned an integer value. What is the value of ans? Answer this question without executing the statements in Python.

Ex. 5 → Consider the recursion formula:

Exercises

with n = 0,..., 1000, h= 1/1000, and a = -0.5.

  1. Create a list u. Store in its first three elements e0, eha, and e2ha. These represent the starting values u0, u1, and u2 in the given formula. Build up the complete list from the recursion formula.
  2. Construct a second list, td, in which you store the values nh, with n = 0, ..., 1000. Plot td versus u (refer section Basic plotting in Chapter 6, Plotting, for more information). Make a second plot in which you plot the difference, that is, |eatn - un|, where tn represents the values inside the vector td . Set axis labels and a title.

The recursion is a multistep formula to solve the differential equation u' = au with the initial value u(0) = u0 = 1. un approximates u(nh) = eanhu0.

Ex. 6 → Let A and B be sets. The set (A B) ∪ (B A) is called the symmetric difference of the two sets. Write a function that performs this operation. Compare your results to the result of the command:

A.symmetric_difference(B).

Ex. 7 → Verify in Python the statement that the empty set is a subset of any set.

Ex. 8 → Study other operations on sets. You find a complete list of those by using the command completion feature of IPython. In particular, study the update and intersection_update methods. What is the difference between intersection and intersection_update?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.123.126