COMPSCI 101 Principles of Programming

Slides:



Advertisements
Similar presentations
Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
Advertisements

COMPSCI 101 Principles of Programming Lecture 6 – Getting user input, converting between types, generating random numbers.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
Python Programming Fundamentals
Introduction to Python
Munster Programming Training
Programming Training Main Points: - Python Statements - Problems with selections.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
COMPSCI 101 Principles of Programming Lecture 28 – Docstrings & Doctests.
CS0004: Introduction to Programming Variables – Strings.
Computing with Numbers Zelle - Chapter 3 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science, John.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
COMPSCI 101 Principles of Programming Lecture 4 –The type() function, the string type, the len() function, string slices.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Lecture 08 – Documentation, debugging.  docstring  A special kind of string (text) used to provide documentation  Appears at the top of a module 
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Neal Stublen What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
The Math Class Methods Utilizing the Important Math Operations of Java!
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
JavaScript 101 Lesson 3: Variables and Arithmetic.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Introduction to Programming
Fundamentals of Programming I Overview of Programming
Main Points: - Python Statements - Problems with selections.
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Variables, Expressions, and IO
Number and String Operations
Learning Outcomes –Lesson 4
Introduction to Programming
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Presentation transcript:

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

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

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

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

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 = 3.14159265359 * radius ** 2 print("Area of circle", area) CompSci 101

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

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

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

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 = 221.67 radius = math.sqrt(area / math.pi) print("Radius of circle", radius) Radius of circle 8.399985266079987 CompSci 101

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

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

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

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

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

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

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 // 3 + 4 * 10 % 3 result2 = 10 - 7 // 3 * 3 + 13 % 5 / 5 * 2 result3 = 17 % 3 * 2 - 3 ** 2 * 3 + 19 // 2 print(result1, result2, result3) CompSci 101

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 -36.8404, -174.7399 Sydney -33.8600, -151.2094 CompSci 101

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 3959 miles) distance = Auckland -36.8404, -174.7399 Sydney -33.8600, -151.2094 Note that for this formula the angles need to be in radians to pass to trig functions! CompSci 101

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

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

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

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