Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Monday, 18 November 2019

Back to Python

This post was for year 6 and 7 students taking a short Python class in late 2019.

(For more Python, see an earlier blog post.)
Dec 3rd 

For the last lesson of this short course, let's draw a Christmas tree!  Without any graphics, we might just use the '*' symbol.   For example:

# almost a Xmas tree!! 
height = 10
for wid in range(1,10):
    print((height-wid)*' '+wid*'*') 

gives .... 
a very simple Xmas Tree


Can you understand how this program works?  Can you improve this code?  eg a better shape, add a tree trunk,  add a snowman! or snow!!  There might be a prize for the best solution.








Nov 25th
After the class today, hopefully everybody can run the Python Editor/Compiler app on their Chromebook and save files to their Google Drive - let us know if you can not do this!

Today we discussed summing integers from 1 to N in a "for loop" and we also showed the Maths trick that gives the formula N*(N+1)/2 as the summation result.   Lastly we looked at the bottom right-hand example in my handout from last week, that finds factors of a number.   eg

# find the factors of a number
num = 12
for j in range(2, num-1):
    if int(num/j) * j == num:
    print j, ' is a factor of ', num

…. 
2 is a factor of 12
3 is a factor of 12
4 is a factor of 12
6 is a factor of 12

The key statement here is the test that divides two numbers, chops off any remainder from the division, and then multiplies by the second number (the divisor) to check if the result is equal to what we started with!   If you don't follow this, just try it out, eg 


int(12/5)*5
10
int(12/4)*4

12

int(12/5)*5 == 12
False


Now, instead of using the test "int(num/j) * j == num",  how about using Python's % (remainder) operator?   For example:
12%5
2
12%4

0

OK - so try to change the code so that it uses a remainder test to check if the second number is a factor of the first number.  You might also edit the code so that it will give an extra message when there are no factors (i.e. variable "num" is a prime).   We will look at this in the next lesson.


Nov 18th
After further investigation, it seems that the best approach with Chromebooks might be to use the Python Compiler Editor app (which should now be installed on your laptops).

This app should allow Python 2 code to be edited and saved on your Google drive.  Initially you might start with a very simple program stored on my drive at this location:
https://drive.google.com/drive/folders/146FxzhtjIbP4KeEtpPEG1KX49HQwO_h4?usp=sharing

From the menu items at the bottom of the app screen, try to open the file listed above (copy and paste the long address), or you might start a new file and copy the code from below.)   Try running the code - does it work?  Do you understand how it works?  Make some changes (e.g. can you use a "for" or "while" loop to repeat the last 4 statements many times?


# maths drill
import random

n1 = random.randint(1,10)
n2 = random.randint(1,10)
prod = n1 * n2
print(str(n1)+" * "+str(n2)+" = "+str(prod))

Save the final program on your own Google drive, using the SaveAs button.  Hopefully this approach will let you edit, run and save Python programs!

If the above works, here's nice homework problem.  Please write a program to add up all the (whole) numbers from 1 to 100 and print the result.  We will discuss this next week.

Dr Bill

Saturday, 23 December 2017

Gravity Simulations for Year 6/7 Students

This note discusses Python programming for year 6/7 students at a local primary school.  The activity was part of a STEM program coordinated by CSIRO.

Python provides an excellent option for introducing programming in schools and has been widely adopted in the UK.  After discussions with the class teacher, we decided to use Python within the area of Space Science.  In particular the lessons aimed to introduce the elements of programming in a Python development environment using "Turtle Graphics", with a focus on simulating the motion of heaven bodies subject to gravity.

Primary students can make very good progress with programming concepts and are keen to learn.   However the motion of bodies subject to acceleration is normally a high-school topic.  The examples below aim to show that using first-order equations can put this topic within reach of primary students.  We need the following "maths":
  • distance travelled = velocity * time interval    
  • change in velocity = acceleration * time interval
  • force = mass * acceleration 

The last equation, which is Newton's famous second law, is not actually used in the examples below but provides a great way to talk about the concepts involved, including large rockets that can provide huge thrust forces!   The second equation is basically the definition of acceleration, which might be a novel concept for younger programmers.  We are all familiar with the first equation and strictly speaking this only applies when the velocity is fixed.  Our simulation approach is to take many small time steps and calculate the object's position and velocity at each step.  As long as the velocity is changing "smoothly" this simulation approach gives a good approximation to the formulas normally used (eg s=ut + at^2/2 etc).  



Graphics output from gravity_order1.py
Anyway, two examples are shown below using this approach.  Note that to further simplify the problems we assume that the force of gravity only acts vertically and affects vertical motion, with no acceleration on the horizontal direction.

In the first example a ball is thrown upwards and falls due to gravity.  The graphical output is shown on the left.   The code (below) is very simple:  after some initialisation statements, a loop is used to evaluate the ball's vertical velocity and position at each time step.











From lander.py 

As a second example consider a lunar lander simulation, where the thruster on the lander can be toggled on and off by pressing the 'up' key. A sample output is shown on the right.  Initially the lander has zero vertical velocity and a small horizontal velocity.  It starts to accelerate towards the lunar surface due to the moon's gravity, as shown by the increasing distance between the black dots at each time step.  When the thruster is turned on, the position dots change to red.  For simplicity, we assume in the code (below) that the thruster causes an acceleration of equal magnitude, but upwards. Hence the rate of descent decreases until the thruster is turned off, after which position is shown in blue.  By toggling the thruster, the lander can be brought gently to the surface.  This takes a little practice!

It would have been nice to retain a simple simulation loop like the first example, but include a 'key-pressed' check for thruster control.  That doesn't seem possible in this environment, so the code for this example uses an 'event-driven' programming model.  The position and velocity calculations reside in function 'tloop' and the thruster is called from a key-press handling function.