200 likes | 306 Views
COMPSCI 101 Principles of Programming. Lecture 02 -- Calculations. Learning outcomes. At the end of this lecture, students should be able to: perform calculations using standard arithmetic operators import standard Python modules call functions from standard Python modules
E N D
COMPSCI 101Principles of Programming Lecture 02 -- Calculations
Learning outcomes • At the end of this lecture, students should be able to: • perform calculations using standard arithmetic operators • import standard Python modules • call functions from standard Python modules • use variables to store values • describe differences between int and float types • print numbers to standard output • use a docstring • use comments COMPSCI 101 - Principles of Programming
Recap • Programs are a sequence of instructions • Instructions are specified using a formal language • Computer executes the instructions one after the other • Programs are deterministic • The result of the instructions is well defined • Rules govern the results of instructions • Once we learn the rules, we can model what the computer does • Then the output is completely predictable • Experimental • We don’t always know what the rules are • We need to experiment to find out COMPSCI 101 - Principles of Programming
Sample problems • Convert a length from inches to centimetres • Convert a weight from kilograms to pounds • Calculate the area of a circle COMPSCI 101 - Principles of Programming
Example • Convert from inches to centimetres • What value are we converting (input)? • What is the formula to convert inches to centimetres? • How do we show the result (output)? • Answers • Convert the value 100 from inches to centimetres • 1 inch is 2.54 centimetres • Print out the result COMPSCI 101 - Principles of Programming
Storing information using variables • Variables are names for storage locations • Almost any name will work – there are some constraints though • A variable stores only 1 value at a time • Store a value to a variable location using = • Get a value from a variable location using the variable name name = 'Andrew' height = 167.5 age = 42 >>> name 'Andrew' >>> age 42 COMPSCI 101 - Principles of Programming
Question • Use the following list of valid and invalid variable names to figure out the rules behind naming variables. • Valid • x • age • age_of_child • box1 • box_2 • _age • age_ • Invalid • 1 • age of child • age-child • 1st • 2_box COMPSCI 101 - Principles of Programming
integers and floating point numbers • Information in a program is categorised into different types • integer • floating point • string, boolean, and many more • Integer values are numbers without decimal points. • Floating point numbers are numbers with decimal points • Limited precision 356 0 1.5 3.333333333333 COMPSCI 101 - Principles of Programming
Arithmetic • Mathematical operators • Addition + • Subtraction - • Multiplication * • Division / • Exponentiation ** >>> 34 + 2 36 >>> 12 / 3 4.0 COMPSCI 101 - Principles of Programming
Expression • An expression is part of the program that can be evaluated (i.e. when the computer follows the rules that define the instructions, an expression turns into a value). • An expression can be used anywhere that a value is used x = 3 + 4 3 + 4 is an expression COMPSCI 101 - Principles of Programming
Printing to standard output • When text is printed, the text goes to “standard output” • Under normal circumstances standard output is the screen • Any type of data can be printed print(125) print(3.5) print('Hello') 125 3.5 Hello COMPSCI 101 - Principles of Programming
Example • Convert a length from inches to centimetres length_in_inches = 100 length_in_cm = length_in_inches * 2.54 print(length_in_cm) COMPSCI 101 - Principles of Programming
Tracing code • Keep track of the contents of variables • Write down the name of each variable • Change the value when (and only when) an assignment occurs • When you change a value, cross out the old one and write a new one length_in_inches: 100 length_in_cms: 254.0 COMPSCI 101 - Principles of Programming
Documentation • docstring • A special kind of string (text) used to provide documentation • Appears at the top of a module • Appears at the top of a function (later lecture) • Uses three double-quotes to surround the documentation • All modules should include a docstring """Converts a length in inches to a length in centimetres. Author: Andrew Luxton-Reilly """ length_in_inches= 100 length_in_cm = length_in_inches * 2.54 print(length_in_cm) COMPSCI 101 - Principles of Programming
Comments • Comment • A programming comment is a note to other programmers • Anything between a # and the end of the line is ignored by the computer • Add comments sparingly to explain code that is difficult, or tell other programmers something they need to know about the code. """Converts a length in inches to a length in centimetres. Author: Andrew Luxton-Reilly """ length_in_inches= 100 #Alter this value to convert a different length length_in_cm = length_in_inches * 2.54 print(length_in_cm) 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 • 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 • 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
Summary • 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