29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

Computer Programming w/ Eng. Applications
Return values.
Types and Arithmetic Operators
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Basic Input/Output and Variables Ethan Cerami New York
INTRODUCTION TO PYTHON PART 1 CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Python Programming Fundamentals
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
Computer Programming Fundamental Data Types Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons.
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may.
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.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
Variables, Expressions, and Standard Functions. Topics Basic calculation Expressions, variables, and operator precedence Data types Input / Output Examples.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Input, Output, and Processing
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
Variables, Expressions and Statements
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
3. COMPUTING WITH NUMBERS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
22 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Introduction to Programming
Introduction to Programming
Fundamentals of Programming I Overview of Programming
Introduction to Programming
Data Types and Conversions, Input from the Keyboard
BIL 104E Introduction to Scientific and Engineering Computing
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
CS150 Introduction to Computer Science 1
Core Objects, Variables, Input, and Output
Introduction to Programming with Python
Algorithms computer as the tool process – algorithm
CS150 Introduction to Computer Science 1
Terminal-Based Programs
Introduction to Programming
Introduction to Programming
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
4.3 Arithmetic Operators and Mathematical Functions
Presentation transcript:

29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 2016 Week 4: More Arithmetic and Input

Recall Operators and Expressions  Operators: +, -, *, /, **  Example of an expression: (p+4)*5 where 4, 5 are number literals and p is a variable  If p is assigned a value then the expression can be evaluated to yield a number 29 January 2016PFE Section

Recall Precedence of Operators  In order of decreasing precedence exponentiation ** multiplication and division * / addition and subtraction + -  If in any doubt then use brackets, = (3-5)-6 29 January 2016PFE Appendix B3

Recall Built-in Functions  The following functions are always available  abs(-5) # returns 5  round(3.4) # returns 3  round(3.452, 2) # returns 3.45  max(1, 5, 2, 9, 3) # returns 9  min(1, 5, 2, 9, 3) # returns 1 29 January 2016PFE Section

Arithmetic Expression Examples 29 January 2016PFE Section Mathematical Expression Python Expression Comments (x+y)/2 Parentheses required. x+y/2 has the value x+(y/2) x*y/2 Parentheses not required. Operators with the same precedence are evaluated left to right (1+r/100)**n Parentheses are required sqrt(a**2+b**2) Import the sqrt function from the math module pi pi is a constant declared in the math module

Balanced Parentheses  The following formula is not correct ((a+b)*t/2*(1-t) The parentheses are not balanced  Check: count from left to right starting at 0, add 1 for a left bracket, subtract 1 for a right bracket. In this case,  The parentheses are balanced if and only if the count is always non-negative and the final count is January 2016PFE Section

Examples of Function Calls  price = 125 rate = tax1 = round(price*rate, 2) # round to 2 decimal places tax2 = round(price*rate) # round to the nearest integer # The value of tax1 is The value of tax2 is 22.  best = min(price1, price2, price3, price4) # The function min has an arbitrary number of arguments 29 January 2016PFE Section

Math Module  All Python systems have the standard library  The functions and data types in the standard library can be used in your programs, e.g. abs, float, int, input, min, max, print, round …  The math module in the standard library contains functions such as sqrt, cos, sin, tan, exp, etc.  See PFE Appendix D 29 January 2016PFE Section

Selected Functions in the Math Module 29 January 2016PFE Section Function Returns sqrt(x) trunc(x)Truncates floating-point value x to an integer cos(x)The cosine of x radians sin(x)The sine of x radians tan(x)The tangent of x radians exp(x) degrees(x) radians(x) log(x) log(x, base) The natural logarithm of x (to base e) or the logarithm of x to the given base

Obtaining a math Module Function  To use e.g. sqrt, put this statement at the top of the program from math import sqrt  Multiple functions can be obtained using a single statement from math import sqrt, sin, cos  To obtain everything use from math import *  See PFE Appendix D, math Module 29 January 2016PFE Section

Exercise 29 January 2016PFE Review Question R2.311

Roundoff Errors price = 4.35 quantity = 100 total = price * quantity # Should be 100 * 4.35 = 435 print(total) # Prints # The number 4.35 cannot be represented exactly as a # binary floating point number 29 January 2016PFE Section

User Input first = input("Enter your first name: ") # The input function displays the string argument (prompt) in # the console window and places the cursor on the same line, # immediately following the string. Enter your first name: _ # The program waits until the user types a name followed # by Enter. The name is stored as the value of first in the # form of a string. 29 January 2016PFE Section

Numerical Input userInput = input("Please enter the number of bottles: ") bottles = int(userInput) # The input function reads in a string and returns the string to # the calling program. The function int converts the string to # an integer. userInput2 = input("Enter price per bottle: ") price = float(userInput2) # The function float converts the string to a floating point value. 29 January 2016PFE Section

The Function int print(int("5")) # print 5 print(int("test")) # invalid literal for int print(int(7.6)) # truncate to 7 print(int(-7.6)) # truncate to -7 print(int("5.6")) # invalid literal for int 29 January 2016PFE Section

Description of int in Appendix D The Python Standard Library Built-in Functions int(x) This function converts a number or string to an integer. Parameter: x A string or numerical value Returns: The new integer object 29 January 2016PFE Appendix D16

The Function float print(float("5")) # print 5.0 print(float("7.6")) # print 7.6 print(float(7.6)) # print 7.6 print(float("test")) # ValueError: could not convert string # to float print(float("3E2")) # print print(float("3e2")) # print January 2016PFE Section

Vending Machine Write a program that simulates a vending machine. A customer selects an item for purchase and inserts a bill into the vending machine. The vending machine dispenses the purchased item and gives change. Assume that all item prices are multiples of 25 cents, and the machine gives all change in dollar coins and quarters. Compute how many coins of each type to return 29 January 2016PFE Section 2.518

Preliminaries Step 1: identify inputs and outputs. Step 2: Work out an example by hand, e.g. the item costs $2.25 and a $5 bill is inserted. Step 3: Write pseudo code for computing the answers. 29 January 2016PFE Section 2.519

Coding Step 4: Declare the variables and constants that are needed. Step 5: Turn the pseudo code into Python statements. Step 6: Provide input and output. Step 7: Write the program 29 January 2016PFE Section 2.520