160 likes | 270 Views
COMPSCI 101 Principles of Programming. Lecture 03 – Introduction to Code Development. Learning outcomes. At the end of this lecture, students should be able to: Read and understand a program consisting of multiple assignment statements Understand how to develop a program in steps. Recap.
E N D
COMPSCI 101Principles of Programming Lecture 03 – Introduction to Code Development
Learning outcomes • At the end of this lecture, students should be able to: • Read and understand a program consisting of multiple assignment statements • Understand how to develop a program in steps COMPSCI 101 - Principles of Programming
Recap • Instructions are executed in sequence • Variables hold one piece of information at a time • There are different types of information • The variable name is used to identify that variable • Assignment statements are used to store information in a variable • Expressions are things that can be evaluated and may be used anywhere that a value is expected • Variable names • Calculations involving variables and literal values • Every module should start with a docstring COMPSCI 101 - Principles of Programming
Exercise • What do each of the following expressions evaluate to? • 10 + 4 • 10 - 4 • 10 * 4 • 10 / 4 • 10 ** 4 COMPSCI 107 - Computer Science Fundamentals
Exercise • What values are stored in each variable after this code is executed? Perform a code trace. a = 7 b = 3 c = 2 d = 4 e = a a = b b = e e = c c = d d = e COMPSCI 107 - Computer Science Fundamentals
Example • Calculate the area of a circle • Formula: area = π r2 COMPSCI 101 - Principles of Programming
Importing functions • Code is stored in modules • We want to reuse code as much as possible • Build up libraries of code • Importing a module allows you to access code in that module • Help function provides access to the docstring • Python.org has a list of all the modules and functions provided >>> import math >>> math.pi 3.141592653589793 >>> math.sqrt(4) 2.0 COMPSCI 101 - Principles of Programming
Example • Calculate the area of a circle """Calculates the area of a circle. Author: Andrew Luxton-Reilly """ import math radius = 10 area = math.pi * radius ** 2 print(area) COMPSCI 101 - Principles of Programming
Integer division (floor division) and modulus • Floor division means to divide and throw away everything except the integer part • Python uses // to indicate floor division (also called integer division) • Modulus gives us the remainder after performing a division • Python uses % to indicate modulus >>> 5 // 3 1 >>> 7 // 2 3 >>> 5 % 3 2 >>> 7 % 2 1 COMPSCI 101 - Principles of Programming
Exercise • Imagine you are working in a group of 7 people, and you are given a box containing 30 pencils. You distribute the pencils equally among all the group members, so every person has the same number of pencils. • Write the Python expression that calculates the number of pencils that each person receives. • Write the Python expression that calculates the number of pencils remaining in the box. COMPSCI 101 - Principles of Programming
Exercise • Convert a weight from kilograms to pounds • Starting information: number of kilograms • Formula: 1 kilogram = 2.20462 pounds • Print the resulting number of pounds docstring initialisation calculation output COMPSCI 101 - Principles of Programming
Example • Write a program that calculates the distance between two cities, given the longitude and latitude of those cities. COMPSCI 101 - Principles of Programming
Design questions -- input • Identify the input data • What data do we need to perform the task? • What form does the data need to be in? • What type is the data? COMPSCI 101 - Principles of Programming
Design questions -- output • Describe the output data • What will be produced by the program? • What form/format will the data have? • What type will the data be? COMPSCI 101 - Principles of Programming
Design – what else do we need to know? • Formula/algorithm for how to calculate the distance • Use Google • Haversine formula • r is the radius of the earth • Latitude • Longitude COMPSCI 101 - Principles of Programming
Summary • Code stored in other modules can be imported • Provides access to the functions defined in that module • The help() function provides the docstring for a module or function • Integer division and modulus are used to get parts of a division • Longer programs are best broken up into steps COMPSCI 101 - Principles of Programming