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!

# Summary¶

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

2. Variables are an important tool for handling data values.

3. 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.

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

# Problems¶

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

2. Write a line of code that generates the following error:

NameError: name 'x' is not defined

1. 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

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

2. 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.

3. 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.

4. Use the print function to generate the following strings.

• The world ‘Engineering’ has 11 letters.

• The word ‘Book’ has 4 letters.

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

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

7. 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.

8. Sort the list_a in problem 10 in ascending order.

9. Turn ‘Python is great!’ to a list.

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

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

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

13. 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

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

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

16. 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.

17. Generate an array with size 100 evenly spaced between -10 to 10 using linspace function in Numpy.

18. 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.

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

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

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

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