Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPSCI 101 Principles of Programming

Similar presentations


Presentation on theme: "COMPSCI 101 Principles of Programming"— Presentation transcript:

1 COMPSCI 101 Principles of Programming
Lecture 3 – Expressions, Mathematical Operators & the math module

2 Review Which of the following code is correct? A: B: C: D:
print("Programming is fun") print("Python is fun") print("Programming is fun") print("Python is fun") print("Programming is fun) print("Python is fun") print("Programming is fun) print("Python is fun") CompSci 101

3 Review Which of the following is/are a valid identifier(s)? A. $343
B. mile C. 9X D. 8+9 E. max_radius CompSci 101

4 Learning outcomes At the end of this lecture, students should be able to: import modules and use the functions defined in the module use integer division and modulus operators use comments. Include a docstring at the top of a program use self-documenting code to make the program easy to understand understand that an expression evaluates to one value understand the order of operations when an expression is evaluated understand how to develop a program in steps CompSci 101

5 Docstrings A docstring is a special kind of string (text) used to provide documentation Appears at the top of every program three double-quotes are used to surround the docstring All programs should include a docstring at the beginning of the program The docstring contains the author and a description of what the program does """Calculates the area of a circle. Author: Adriana Ferraro """ radius = 10 area = * radius ** 2 print("Area of circle", area) CompSci 101

6 Skeleton of a Python program
Example01.py DEMO In general the format of a Python program is: """Calculates the area of a rectangle. Author: Adriana Ferraro """ width = 3.56 height = 8.4 area = width * height print("Area of rectangle", area) docstring initialisation calculation output Area of rectangle Every Python program is stored in a file which has .py at the end of the file name, e.g., CalculateArea.py, CompoundInterest.py CompSci 101

7 Comments Example02.py DEMO As well as the docstring describing the purpose of the program at the top of all our programs, comments can be added to the program code. A programming comment is a note to other programmers who need to understand the code. Anything between a # and the end of the line is a comment and is ignored by the interpreter """Converts a length in inches to a length in centimetres. Author: Adriana Ferraro """ length_in_inches = 100 #Change value to convert different lengths length_in_cm = length_in_inches * 2.54 print("Length", length_in_cm) CompSci 101 Length 254.0

8 Use self documenting code
Add comments sparingly to explain code that is difficult, or to tell other programmers something they need to know about the code. It is always important to use good variable names. The program below does the same job as the program on the previous slide but it uses very poor variable names which makes the program difficult to understand. """Converts a length Author: Adriana Ferraro """ a = 100 b = a * 2.54 print("Length", b) Length 254.0 CompSci 101

9 Python libraries CalculateArea.py DEMO Python has libraries of code which contain definitions and functions which perform useful tasks and calculations. The files in these libraries are called modules. The name of a module is the name of the file without the .py extension. The math module contains many useful math functions, e.g., math.sin(), math.cos(), math.pow(), math.sqrt(), math.floor(), … In order to be able to use the functions of a module, we need to import the module. Importing a module means that we can then use all the functions defined inside that module, e.g., """Calculates the radius of a circle, given the area. """ import math area = radius = math.sqrt(area / math.pi) print("Radius of circle", radius) Radius of circle CompSci 101

10 https://docs.python.org/3/library/math.html#module-math
This website contains the documentation about all the Python modules. CompSci 101

11 Expressions – order of operations
Expressions using numbers are evaluated in the same way as in mathematical expressions, i.e., BEDMAS applies: Note: Work from left to right. The / operator always results in a float, e.g., 8 / 4 is 2.0 B - Brackets Exponents Division, multiplication Addition, subtraction CompSci 101

12 Expressions – order of operations
Example03.py DEMO What is the output? 18 * / 3 / 3 result1 = (25 - 7) * / 3 result2 = * / result3 = 32 / 4 ** (3 + 2 * 3 - 7) / 5 print(result1, result2, result3) 17 – 6 – 12 / 17 – 6 – 23.0 32 / 4 ** 2 / 5 32 / 16 / 5 2 / 5 Remember to work from left to right. CompSci 101

13 More arithmetic operators
Two more mathematical operators: Floor division (integer division) // Modulus (remainder) % Floor division (integer division) performs the division and throws away the part after the decimal point, e.g., 16 // 5 gives 3 17 // 5 gives 3 34 // 5 gives 6 CompSci 101

14 More arithmetic operators
Modulus performs the division and gives the remainder, e.g., 16 % 5 gives 1 17 % 5 gives 2 34 % 5 gives 4 16 % 30 gives 16 CompSci 101

15 Exercise 1 Give the output Integer! result1 = 25 % 3 result2 = 20 % 34
print(result1, result2, result3, result4, result5) Integer! CompSci 101

16 Exercise 2 Give the output when the following code is executed
B - Brackets Exponents (**) Multiplication, Division, Modulus, Integer division Addition, Subtraction Give the output when the following code is executed result1 = 25 / 4 // * 10 % 3 result2 = // 3 * % 5 / 5 * 2 result3 = 17 % 3 * ** 2 * // 2 print(result1, result2, result3) CompSci 101

17 Exercise 3: Finding the distance between two cities
We want to find the distance between two cities given the latitude and longitude of the two cities. Auckland , Sydney , CompSci 101

18 Finding the distance between two cities
The formula (thank you Internet) for finding the distance between two points on the earth: where: φ is latitude λ is longitude R is the earth's radius (6371 km or miles) distance = Auckland , Sydney , Note that for this formula the angles need to be in radians to pass to trig functions! CompSci 101

19 Algorithm Break the problem up into manageable sections
2: Sydney 1: Auckland Break the problem up into manageable sections part1 part2 distance = Auckland , Sydney , Get lamba_longitude_sydney Get lamba_longitute_auckland Get theta_latitude_Sydney Get theta_latitude_auckland longitude in radians CompSci 101

20 The Math Module In the math module we can find all the functions we need: math.sin(), math.cos(), math.asin(), math.sqrt(), math.radians() distance = part1 #part 1 part1 = (math.sin((lamba_longitude_sydney - lamba_longitute_auckland)/2)) ** 2 part1 = part1 * math.cos(_____________________) * math.cos(_____________________) CompSci 101

21 The Math Module distance = part2
part2 = (math.sin((theta_latitude_sydney - theta_latitude_auckland)/2)) ** 2 part3 = distance_in_miles = 2 * earth_miles_radius * math.______(part3) CompSci 101

22 Summary In a Python program we can:
import modules and use the functions defined in the imported module use integer division and modulus operators use comments. Every program contains a docstring at the top of the program use self-documenting code to make the program easy to understand understand that an expression evaluates to one value understand the order of operations when an expression is evaluated understand how to develop a program in steps CompSci 101


Download ppt "COMPSCI 101 Principles of Programming"

Similar presentations


Ads by Google