# -*- coding: utf-8 -*- """ Created on Sat Dec 12 19:03:48 2020 A simple python program to illustrate: an 'executable' or 'runable' complete python program importing a 'module' to extend the capacity of basic python basic program output using 'print()' example of a typical 'for loop', a method to do repeated calculations @author: neil """ # using the (very inefficient) Euler series for estimating PI # PI**2 /6 = 1+1/2^2 + 1/3^2 + 1/4^2 .... # by convention most imports of modules should be at the begining of a program import numpy as np # also good programming practise puts any variables that the user might want to change as a header section terms = 20 # how many terms of the series are we going to use? ourpi = 1 # ourpi is the '~pi' given by increasing terms in Euler series for n in range(2,terms): # example 'for' loop, the 'range()' function returns a list of numbers (start,stop) ourpi += 1/(n**2) # each time through the loop, add an extra terms based on 'n' (which is incremented) print(n,np.sqrt(6*ourpi))# 'print' is a complex function, but at its simplest it just prints readable numbers print(np.sqrt(6*ourpi), "our estimate of pi after",n+1,"terms in series") print(np.pi,"an accurate value for pi")