Python computing lab course
Engineering Communication, Measurement, and Data Analysis
Learning the basics of Python
Objectives
After completing these exercises you will be able to:
• Create Jupyter notebooks with cells containing Python code
• Create and be able to identify variables of different types
• Perform simple calculations using Python
Introduction
These computer lab sessions will introduce Python through Jupyter Notebooks, they will build
on the content covered in pre-session notebooks. It is important that you have gone through and
completed the mini-tasks within those notebooks before attempting these exercises. For each session
create a new notebook with a reasonable name.
Getting help
—
There are number of different resources you can use if you get stuck and need help.
1. In case of an error, read the compiler error. It will often tell you the line where the error is
occurring. Try to correct the error and re-run the compiler.
2. Still getting an error, try copying the error message into google, or search using some keywords.
3. Still a bit stuck, ask an demonstrator for help, who will be able to point you in the right
direction
You can also refer back to the pre-lab material and the quick reference sheet on blackboard.
Please note that Exercises 1 to 6 are designed to develop the skills required for the successful
completion of this unit. These are core exercises. Exercises 7 and 8 extend beyond the level
necessary for this unit and introduce skills that will be beneficial later in the programme.
These are extension exercises.
1
1 Exercises
Exercise 1 (core): Strings
Run the following piece of code in the Jupyter notebook:
x = ‘‘Chocolate Cake’’
For each of the commands below, record the output and what the program has done in the notes section.
Command Output Notes
x[0] C found the first letter of string x
x[-1]
len(x)
x[1:3]
x[:5]
[Link]()
Exercise 2 (core): Data Types
Identify the type of data for each variable and convert (cast) to the type shown in the table.
Variable Data Type Cast Type Output Notes
3.4 Float Integer 3 Outputs the integer part of the number
True Float
False String
3 Boolean
5.1 Boolean
7 Float
Exercise 3 (core): Simple Calculations and Comparison Operators
Create and assign the following variables to use in your calculations:
• x = 2
• y = 5.7
• z = -8
Command Output Notes
x + 2 * y 13.4
z % x
y ** x
z - True
z >= x
y != x
x >= 1 and y == 6
y < 10 or z > 1
a = y >= 5
Exercise 4 (core): Outputting a String
Write a short piece of code that calculates the remainder from the variable b (integer value) when
it is divided by two and outputs the following string:
“The remainder when (value of b) is divided by 2 is (value of remainder).”
Example output:
2
Exercise 5 (core): Float Formatting
Format the value of E into exponent notation (scientific notation) to 3 decimal places.
E = 197931859
Output:
Exercise 6 (core): Young’s Modulus
The modulus of elasticity (Young’s Modulus, E) of a material can be calculated from experimental
results. The Young’s Modulus of a material can be calculated by dividing the stress by strain, Equa-
tion 1. Stress is the force applied per unit area and therefore has units of force per unit area, typically
Nm-2 but engineers often use areas square millimetres leading to units of N mm-2 or MPa, Equation
2. Strain is the relative change in length (tension/compression) or the angle of twist (shear). Strain
is dimensionless and has no units, Equation 3.
Figure 1: Cylindrical rod in tension
E = σ/ϵ (1)
σ = F/A (2)
ϵ = ∆L/L0 (3)
Where E = Young’s modulus (MPa or GPa) , σ = Stress (N mm-2 or MPa), ϵ = Strain (no units),
F = Force (N), A = cross-sectional area (mm2), ∆L = change in length (L-L0 , mm); where L =
final length (mm) and L0 =initial length (mm).
A series of experiments were conducted using the test setup in Figure 1, each test used a differ-
ent material with different starting dimensions (cross-sectional area and initial length) as shown in
Table 1. Using the equations given, the predicted change in and final length of the rods can be
calculated. Note: Ensure that consistent units are used throughout calculations (units do not need
to be taken to base SI units) and MPa = Nmm-2 should be used. The value of 3.14 should be used
for π, this will need included in your code as a variable.
3
Material Young’s Modulus (GPa) Force (N) Radius (mm) Initial length (mm)
Steel 200 8000 1.1 500
Aluminium 60 5000 2.5 750
Titanium 115 4000 3 1000
Table 1: Table of material properties and dimensions
Task 6
Write a program that calculates and outputs the change in and final length in mm of each
sample in Table 1. Record the results in the table below or in your lab notebook.
Change in length (mm) Final length (mm)
Steel sample
Aluminium sample
Titanium sample
Hint: 1 GPa = 1000 MPa. Use the following units MPa (Nmm-2 ), N and mm. By using
the Young’s modulus of each material and your program to determine which of the materials
should be selected to fulfil the following design criteria. A material that will have a change in
length of 2.5 mm from its original length of 200mm and radius of 1mm, when a load of 4500N
is applied. (You do not need to re-write your code just change the variable values).
Answer:
Exercise 7 (extension): Evaluating the lift of an aerofoil
The lift produced by an aerofoil can be determined from the lift coefficient and the dynamic pressure
on the aerofoil, see equation 4. The lift (l) and drag (d) are affected by the angle of attack (α),
air density (ρ) and freestream velocity (ν), see Figure 2. The lift (per unit chord) produced by an
Figure 2: Forces acting on an aerofoil
aerofoil in inviscid flow can be approximated as:
l = qCl (4)
where q = 21 ρν 2 is the dynamic pressure (ρ is the air density and ν is the freestream velocity) and
Cl = 2πα is the lift coefficient (α is the angle of attack into the flow direction in radians).
Task 7
Write a program that determines the lift force in N for the following variable values:
ν = 10m/s, ρ = 1.225kg/m3 and α = 10◦ . The program should output a string that says ”The
lift is (value of l) N” with lift being given to 3 decimal places.
Answer:
4
Exercise 8 (extension): Moment of inertia
The mass of a simple pendulum is concentrated at a single point at a distance from the pivot, while
a compound pendulum’s mass is not a fixed point but a rigid body, see Figure 3. The period of the
Figure 3: Diagram of a simple and compound pendulum
compound pendulum (T) depends on the properties of the body: its mass (m), moment of inertia
(I) and the distance from the pivot point to the centre of mass (d), as shown in Equation 5.
s
I + md2
T = 2π (5)
mgd
Where T = period of oscillation (s), I = moment of inertia (kg m2 ), m = mass of rigid body (kg), d
= distance from the pivot to the centre of mass of the body (m) and g = acceleration due to gravity
(9.81 ms-2 ).
An experiment was ran to determine the period of oscillation for Object A, to improve accuracy the
time period was measure 5 times and measured for 10 oscillations. The results of the experiment
are shown in Table 2.
Experiment 1 2 3 4 5
Time period for 10 oscillations (s) 11 12 11 12 9
Table 2: Time period for 10 oscillations for Object A
Task 8
Write a program that determines the moment of interia for Object A. Object A has m = 0.5kg
and d = 0.14 m. The program should print a string that contains the value of I to 3 decimal
places and in scientific notation with units as its output.
Answer: