Introduction to Programming

Similar documents
Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Introduction to Engineering gii

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Variable and Data Type 2

Python Programming: An Introduction to Computer Science

Intrinsic Functions Outline

MYSQL NUMERIC FUNCTIONS

9 Using Equation Networks

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Chapter 3 Mathematical Functions, Strings, and Objects

CT 229 Java Syntax Continued

Programming in QBasic

Chapter 4: Basic C Operators

Introduction to Python, Cplex and Gurobi

Goals for This Lecture:

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

C++ Programming Lecture 11 Functions Part I

Product Price Formula extension for Magento2. User Guide

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Methods CSC 121 Fall 2014 Howard Rosenthal

Chapter 2. Outline. Simple C++ Programs

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Introduction to FORTRAN

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Operators Functions Order of Operations Mixed Mode Arithmetic VOID Data. Syntax and type conventions Using the Script window interface

Engineering Problem Solving with C++, Etter/Ingber

Excel Tool: Calculations with Data Sets

Built-in Types of Data

LAB 1 General MATLAB Information 1

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Expressions. Eric McCreath

Lecture 2 FORTRAN Basics. Lubna Ahmed

4. Modules and Functions

Data Types and Basic Calculation

Standard Library Functions Outline

2 Making Decisions. Store the value 3 in memory location y

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Matlab Programming Introduction 1 2

Trigonometric Functions of Any Angle

Dr Richard Greenaway

Fundamentals of Programming & Procedural Programming

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

Computational Physics

Custom Variables (Virtual Variables)

Maths Functions User Manual

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Matlab as a calculator

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

C Programs: Simple Statements and Expressions

Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

AMS 27L LAB #1 Winter 2009

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Methods CSC 121 Fall 2016 Howard Rosenthal

Basic stuff -- assignments, arithmetic and functions

Python Programming Exercises 1

Introduction to MATLAB

Methods CSC 121 Spring 2017 Howard Rosenthal

Program Structure and Format


ELEMENTARY MATLAB PROGRAMMING

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

FORTRAN Basis. PROGRAM LAYOUT PROGRAM program name IMPLICIT NONE [declaration statements] [executable statements] END PROGRAM [program name]

Downloaded from Chapter 2. Functions

The Very Basics of the R Interpreter

Fundamentals: Expressions and Assignment

1001ICT Introduction To Programming Lecture Notes

Introduction to Programming

ME1107 Computing Y Yan.

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

ME 142 Engineering Computation I. Unit 1.2 Excel Functions

Writing Python Programs, Numerical Data Types

Julia Calculator ( Introduction)

ANSI C Programming Simple Programs

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

General MATLAB Information 1

Green Globs And Graphing Equations

Introduction to C++ Dr Alex Martin Room 6.10

The Number object. to set specific number types (like integer, short, In JavaScript all numbers are 64bit floating point

10 Using the PCFL Editor In this chapter

Program Workspace. Why numerical methods? Problem examples Why programming? Why numerical methods and programming? Why VBA?

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

Chapter 1 Introduction to MATLAB

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

SIMPLE I/O WITH PYTHON

Quick MATLAB Syntax Guide

Introduction to MATLAB

PART 1 PROGRAMMING WITH MATHLAB

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Expressions and operators

Lecture 1. Introduction to course, Welcome to Engineering, What is Programming and Why is this the first thing being covered in Engineering?

Single row numeric functions

Introduction to Matlab

Transcription:

Introduction to Programming Department of Computer Science and Information Systems Tingting Han (afternoon), Steve Maybank (evening) tingting@dcs.bbk.ac.uk sjmaybank@dcs.bbk.ac.uk Autumn 2017 Week 4: More Arithmetic and Input 24 October 2017 Birkbeck College, U. London 1

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 numerical value then the expression can be evaluated to yield a number 24 October 2017 PFE Section 2.2.5 2

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 = (3-5)-6 24 October 2017 PFE Appendix B 3

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 24 October 2017 PFE Section 2.2.4 4

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

Balanced Parentheses The parentheses in the following formulae are not balanced ((a+b)*t/2*(1-t) (a+b))*(t/2*(1-t) Check: count from left to right starting at 0, add 1 for a left bracket, subtract 1 for a right bracket, 0 1 2 1 2 1 0 1 0-1 0 1 0 The parentheses are balanced if and only if the count is always nonnegative and the final count is 0. 24 October 2017 PFE Section 2.2.5 6

Examples of Function Calls price = 124 rate = 0.173 tax1 = round(price*rate, 2) # round to 2 decimal places tax2 = round(price*rate) # round to the nearest integer # The value of tax1 is 21.45. The value of tax2 is 21. best = min(price1, price2, price3, price4) # The function min has an arbitrary number of arguments 24 October 2017 PFE Section 2.2.4 7

Standard Library All Python systems have the standard library The standard library contains built in functions that can be used immediately in your programs, e.g. abs, float, int, input, min, max, print, round The standard library also contains a math module with functions such as sqrt, cos, sin, tan, exp, etc. See PFE Appendix D 24 October 2017 PFE Section 2.2.5 8

Selected Functions in the Math Module Function Returns sqrt(x) The square root of x x 0 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) e x degrees(x) Convert x radians to degrees (returns x 180/π) radians(x) Convert x degrees to radians (returns x π/180) log(x) log(x, base) The natural logarithm of x (to base e) or the logarithm of x to the given base 24 October 2017 PFE Section 2.2.5 9

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 24 October 2017 PFE Section 2.2.5 10

Exercise Write the following mathematical expressions in Python s = s 0 + v 0 t + 1 2 gt2 G = 4π 2 a 3 p 2 m 1 +m 2 YRS FV = PV 1 + INT 100 c = a 2 + b 2 2a b cos γ 24 October 2017 PFE Review Question R2.3 11

Roundoff Errors price = 4.35 quantity = 100 total = price * quantity # Should be 100 * 4.35 = 435 print(total) # Prints 434.99999999999994 # The number 4.35 cannot be represented exactly as a # binary floating point number 24 October 2017 PFE Section 2.2.5 12

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 string followed # by Enter. The string is stored as the value of first. 24 October 2017 PFE Section 2.5.1 13

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. 24 October 2017 PFE Section 2.5.1 14

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 24 October 2017 PFE Section 2.5.1 15

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 24 October 2017 PFE Appendix D 16

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 300.0 print(float("3e2")) # print 300.0 24 October 2017 PFE Section 2.5.1 17

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 24 October 2017 PFE Section 2.5 18

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. 24 October 2017 PFE Section 2.5 19

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 24 October 2017 PFE Section 2.5 20