Worksheet 1 — Python and NumPy Fundamentals
This worksheet introduces Python programming through a series of practical tasks, building up the skills needed for the finite element application in later worksheets.
Prerequisites:
- A working Python installation — see the Windows or macOS installation guide.
- No prior Python experience required.
Learning Objectives:
After completing this worksheet, you will be able to:
- Write and run basic Python scripts.
- Use variables, loops, conditionals, and functions.
- Perform numerical calculations with NumPy arrays and linear algebra routines.
- Plot functions and data with Matplotlib.
- Read data from files and handle exceptions.
Recommended reading
Please go through the sections up to "Recursion" in the online book How To Think Like a Computer Scientist before starting the worksheet. The Links page collects additional references.
All source files should be stored as separate Python files (.py extension).
Task 1: Write a "hello, world" program
All programming books start with a simple program that prints the text "Hello, world!". Write such a program in Python and save it in a file named hello_world.py.
Task 2: Unit conversion
Write a program that converts a given length in meters to inches, feet, yards and miles. One inch = 2.54 cm, one foot = 12 inches, one yard = 3 feet and a British mil = 1760 yards. For verification, 640 meters is equivalent to 25196.85 inch, 2099.74 ft, 699.91 yards or 0.3977 miles.
Task 3: Interest Calculation
The amount of money in a bank account increases each year by a certain percentage of the amount present at the beginning of the year. This is called interest. The formula for calculating the amount after one year is:
Where:
- A is the amount after n years,
- P is the principal amount,
- r is the annual interest rate,
- n is the number of years (3 years in this case).
Write a program that calculates what 1000 SEK has grown to after three years with 5% interest.
Task 4: Fahrenheit to Celsius conversion table
Write a program that prints a table of Fahrenheit degrees 0, 10, 20, ..., 100 in the first column and the corresponding Celsius in the second column. To calculate degrees Celsius, use the formula:
Task 5: Calculate the sum of the numbers in a file
Read the numbers from a file and calculate the total sum of these. The file contains one number per line. Ensure the program handles errors such as missing files or invalid data gracefully by using appropriate exception handling (e.g., try-except blocks).
Task 6: Graphing a Function
Plot the function
Within the range [0,2π]
For this task, the import declarations made:
Tip
A function can be plotted using the plt.plot() function. The x-values are generated using np.linspace(start, stop, num) and the y-values are calculated by applying the function to the x-values. The plot is displayed using plt.show().
A range of values can be generated using np.linspace(start, stop, num).
Task 7: Calculate the matrices
Define the following matrices in numpy:
Calculate and print the following expression:
Solve then the system of equations
The following module imports must made for this task:
Tip
A linear equation solver is np.linalg.solve(A, b). The inverse of a matrix can be calculated using np.linalg.inv(A). The transpose of a matrix can be calculated using np.transpose(A) or A.T.
Submission requirements
To complete this worksheet, submit a zip archive containing:
hello_world.py— Task 1unit_conversion.py— Task 2interest.py— Task 3fahrenheit.py— Task 4sum_file.py— Task 5 (include the input data file used for testing)plot_function.py— Task 6matrices.py— Task 7