CoCalc Public FilesPython Workshop 1 / Workshop 1 - Getting started with Python.ipynb
Author: Charles Farrant
Views : 48
Compute Environment: Ubuntu 20.04 (Default)

# Workshop 1

## How to pair-program with other students

IPlease turn on your video if you okay to do so and if your internet connection can handle it.

Only the driver is allowed to touch their mouse and keyboard. The navigators should not touch their mouse or keyboard.

The navigator tells the driver what to type and why, what Self-study or Exercise Notebooks to open for reference and which websites to visit. They look out for mistakes and tell the driver how to correct them. They tell the driver when to run the code.

If the navigator does not know what to do the driver can write code but must explain to the navigator what they are doing and why so that the navigator is learning.

Swap roles after each Task, so the driver becomes the navigator and the navigator becomes the driver.

If you are stuck "Ask for help" in Zoom and a demonstrator will come and help as soon as they are available. If you receive a message saying the host is currently helping others please wait.

Clear communication is essential. Encourage each other. Help each other to solve the tasks. This is not a competition to see who is the best programmer. Have fun.

Errors and correcting them are a normal part of programming, not evidence that someone lacks ability. As the navigator, let the driver finish writing a complete line of code before pointing out an error. Most people find it annoying to be corrected as they type, but helpful to be corrected when they're finished typing.

The difference between poor pair-programming and good pair-programming is simply this: Pay attention. Look at the screen as the driver types. When your partner speaks, listen. Don't eat cereal. Don't check your phone. Let no detail get past you.

Talk a lot! Say what you are about to do, ask for ideas.

This video shows you how to pair-program:

There are a set of tasks below for you answer together. To be able to answer them, you will need to have read Self-study Notebooks 1 to 4 and attempted Exercise Notebooks 3 and 4 (there are no Exercise Notebooks 1 and 2).

The tasks start easy and get progressively harder. Help each other to answer as many as you can.

You do not need to answer all tasks during this two-hour Workshop.

If you finish the tasks before the workshop has ended it is fine for you to leave the workshop without asking permission.

If you do not finish the tasks by the end of the workshop you can either complete them individually or continue collaborating together. You'll have to arrange between yourselves how and when you wish do this.

Many of the tasks include the output your code should produce in blue boxes. These outputs are there to help you write the correct code.

Answers and code for all the tasks, with accompanying videos for some tasks, will be posted on CoCalc in a folder called "Workshop Solutions" on Friday.

## Arithmetic and numerical variables

Using Python code, convert 50 inches into centimetres and print the result using an f-string so the output reads:

50 inches is X cm


• Hint: If you can't remember how many centimetres in an inch, google it.
The answer is 50 inches is 127.0 cm
In [1]:
print (50 * 2.54)

127.0

Using Python code, convert 50 inches into feet and inches and print the result using an f-string so the output reads:

50 inches is X feet Y inches


• Hint: Use integer divison and remainder.
The answer is 50 inches is 4 feet 2 inches
In [10]:
number_feet = 50 // 12
number_inches = 50 % 12
print( f'50 inches is {number_feet} feet and {number_inches} inches')

50 inches is 4 feet and 2 inches

How do you think you could use Python code to indicate if an integer number is odd or even?

Hint: Think what makes an integer odd or even. Then think which arithmetic operator you have seen in the self-study notebooks that could be used to give you one answer for even and another answer for odd.

Use Python code to test if zero odd or even?

(We will use this code later when calculating the median of a set of data.)

In [19]:
print(type(6%2))

<class 'int'>

Add a print statement with an f-string to the code below to print the following statement:

The percentage of land covered by forest is 1.06%


Google how to format a decimal number to a percentage without multiplying by 100.

In [37]:
total_land_area = 89032840.42390
forest_area = 939402.42445

land_covered = total_land_area/forest_area
round(land_covered)
land_covered_percentage = "{:.0%}".format(land_covered)
print(100/land_covered_percentage)

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-37-7a5394e746ea> in <module> 6 round(land_covered) 7 land_covered_percentage = "{:.0%}".format(land_covered) ----> 8 print(100/land_covered_percentage) TypeError: unsupported operand type(s) for /: 'int' and 'str' 

Which of these variable names should not be used and why?

1. test
2. Len
3. surname
4. ph
5. 0_value
6. lambda
7. alpha
8. O
9. cell_concentration
10. dna_seq

Bacterial cells replicate by binary division. Under conditions of ample resources, the number of bacteria in a population grows exponentially.

If we wish to predict the size of a population of bacteria after a given time from a starting population we can use the formula

where Nt is the population size after time t, N0 is the starting population size at time t = 0, and d is the average time it takes between bacteria replications; also known as the doubling time.

The units of t and d must be the same (e.g. hours or minutes).

Let the starting population be 450 bacteria and the doubling time be 20 minutes. How many bacteria are there after 12 hours? Your calculation should be performed entirely with Python code.

The answer is 30923764531200 cells or 3.1x1013 cells or 3.1e+13 cells.
In [20]:
t=720
d=20
N=450
print(N*2**(t/d))

30923764531200.0

Each bacterium has a mass of approximately 170 femtograms, which is 1.7x10-13 g or 1.7e-13 g in scientific notation. What is the mass of the bacterial culture after 16 hours?

The answer is 21533 grams or 21.5 kg.
In [21]:
t=960
d=20
N=450
print(N*2**(t/d)*1.7e-13)

21532.835718365186

When the wind blows in cold weather, the air feels colder than it actually is because the movement of the air increases the rate of cooling for warm objects, like people. This effect is known as wind chill.

This is the formula for calculating wind chill (the temperature it feels like)

$\text{wind chill} = 13.12 + 0.6215\times T − 11.37\times V^{0.16} + 0.3965\times T\times V^{0.16}$

where T is the air temperature in degrees Celsius and V is the wind speed in kilometres per hour.

Write some code to calculate wind chill using the variables T and V below. Round your answer to 1dp.

The answer is 2.7 degrees Celsius.
In [27]:
# Assign the value 5 degrees Celsius to the variable "T"
T = 5
# Assign the value 10 km/h to the variable "V"
V = 10

print(round(13.12+0.6215*T-11.37*V**0.16+0.3965*T*V**0.16, 1))

2.7

### Python Trial Quiz

Now try the Python Trial Quiz which can be accessed via the QSB1 Learn site. See the pink arrow in the image below for the link.

If you get a page saying "Resource Unavailable" you first need to click on the "Assessment" link (see yellow arrow in image), read the declaration and click "Mark reviewed". Then you can access the trial quiz.

Every week there will be a quiz on that week's topics. Each week's quiz will open on Thursday at 12 noon and will close the following Monday at 2pm. The quizzes are assessed and count toward your final course mark. Quizzes must be your own work. So you should NOT collaborate or share answers with each other.

This week's trial quiz is designed to familiarise you with the quiz system. It is not assessed, it does not count toward your final mark and you can work together on the trial quiz questions.

Once you've finished the Quiz you are free to leave the workshop.