*This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.*

*The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!*

< 2.7 Introducing Numpy Arrays | Contents | 3.0 Functions >

# Summary¶

Storing, retrieving, and manipulating information and data is important in any science and engineering field.

Variables are an important tool for handling data values.

There are different data types for storing information in Python: int, float, boolean for single value, and strings, lists, tuples, sets, dictionaries for sequential data.

Numpy array is a powerful object that used a lot in scientific computing.

# Problems¶

Assign the value 2 to the variable x and the value 3 to the variable y. Clear just the variable x.

Write a line of code that generates the following error:

```
NameError: name 'x' is not defined
```

Let x = 10 and y = 3. Write a line of code that will make each of the following assignments.

```
u = x + y
v = xy
w = x/y
z = sin(x)
r = 8sin(x)
s = 5sin(xy)
p = x**y
```

Show all the variables in the Jupyter notebook after you finish problem 3.

Assign string ‘123’ to the variable S. Convert the string into a float type and assign the output to the variable N. Verify that S is a string and N is a float using the

*type*function.Assign the string ‘HELLO’ to the variable s1 and the string ‘hello’ to the variable s2. Use the

*==*operator to show that they are not equal. Use the*==*operator to show that s1 and s2 are equal if the*lower*method is used on s1. Use the**==**operator to show that s1 and s2 are equal if*upper*method is used on s2.Use the

*print*function to generate the following strings.The world ‘Engineering’ has 11 letters.

The word ‘Book’ has 4 letters.

Check if ‘Python’ is in ‘Python is great!’.

Get the last word ‘great’ from ‘Python is great!’

Assign list [1, 8, 9, 15] to a variable

*list_a*and insert 2 at index 1 using the*insert*method. Append 4 to the*list_a*using the*append*method.Sort the

*list_a*in problem 10 in ascending order.Turn ‘Python is great!’ to a list.

Create one tuple with element ‘One’, 1 and assign it to tuple_a

Get the 2nd element in the tuple_a in problem 13.

Get the unique element from (2, 3, 2, 3, 1, 2, 5).

Assign (2, 3, 2) to set_a, and (1, 2, 3) to set_b. Get the following:

union of set_a and set_b

intersection of set_a and set_b

difference of set_a to set_b using

*difference*method

Create a dictionary that has the keys ‘A’, ‘B’, ‘C’ with values ‘a’, ‘b’, ‘c’ individually. Print all the keys in the dictionary.

Check if key ‘B’ is in the dictionary defined in problem 17.

Create array x and y, where x = [1, 4, 3, 2, 9, 4] and y=[2, 3, 4, 1, 2, 3]. Compute the assignments from Problem 3.

Generate an array with size 100 evenly spaced between -10 to 10 using

*linspace*function in Numpy.Let array_a be an array [-1, 0, 1, 2, 0, 3]. Write a command that will return an array consisting of all the elements of array_a that are larger than zero. Hint: Use logical expression as the index of the array.

Create an array \(y = \begin{pmatrix} 3 & 5 & 3 \\ 2 & 2 & 5 \\ 3 & 8 & 9 \\ \end{pmatrix}\) and calculate the transpose of the array.

Create a zero array with size (2, 4).

Change the 2nd column in the above array to 1.

Write a cell magic to clear all the variables in the Jupyter notebook