1 / 16

COMPSCI 101 Principles of Programming

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.

season
Download Presentation

COMPSCI 101 Principles of Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 101Principles of Programming Lecture 03 – Introduction to Code Development

  2. 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

  3. 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

  4. 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

  5. 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

  6. Example • Calculate the area of a circle • Formula: area = π r2 COMPSCI 101 - Principles of Programming

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. Example • Write a program that calculates the distance between two cities, given the longitude and latitude of those cities. COMPSCI 101 - Principles of Programming

  13. 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

  14. 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

  15. 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

  16. 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

More Related