Lecture 2: Python Arithmetic

Similar documents
Lecture 4: Simple Input-Calculate-Output Programs

Lecture 10: Strings. CS1068+ Introductory Programming in Python. Dr Kieran T. Herley 2018/19. Department of Computer Science University College Cork

Python Programming Exercises 1

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

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

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

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

>>> * *(25**0.16) *10*(25**0.16)

ENGR (Socolofsky) Week 02 Python scripts

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Approximations and Errors

COMP1730/COMP6730 Programming for Scientists. Data: Values, types 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

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Lecture 4: Basic I/O

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Programming in C++ 5. Integral data types

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Lecture 10: Boolean Expressions

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Giving credit where credit is due

Lecture 8: Simple Calculator Application

Giving credit where credit is due

Chapter 4: Basic C Operators

ECE232: Hardware Organization and Design

Lecture 1. Types, Expressions, & Variables

CS313D: ADVANCED PROGRAMMING LANGUAGE

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

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

CS112 Lecture: Primitive Types, Operators, Strings

ENGR 101 Engineering Design Workshop

CS321. Introduction to Numerical Methods

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Roundoff Errors and Computer Arithmetic

System Programming CISC 360. Floating Point September 16, 2008

Up next. Midterm. Today s lecture. To follow

CS429: Computer Organization and Architecture

Outline. Data and Operations. Data Types. Integral Types

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

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Reserved Words and Identifiers

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Floating Point January 24, 2008

Module 01: Introduction to Programming in Python

CIS 110: Introduction to Computer Programming

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

CIS133J. Working with Numbers in Java

Foundations of Computer Systems

Introduction to Programming

Floating-Point Arithmetic

Review of Calculus, cont d

Floating-point representation

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Algorithms and Programming I. Lecture#12 Spring 2015

Mark Important Points in Margin. Significant Figures. Determine which digits in a number are significant.

Hexadecimal Numbers. Journal: If you were to extend our numbering system to more digits, what digits would you use? Why those?

Chapter 1 & 2 Calculator Test Study Guide

Introduction to Computer Science and Object-Oriented Programming

Lessons on Python Numbers

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Floating Point. What can be represented in N bits? 0 to 2N-1. 9,349,398,989,787,762,244,859,087, x 1067

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Built-in Types of Data

Lecture Set 2: Starting Java

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

Lecture Set 2: Starting Java

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

Data Definition and Data Manipulation. Lecture 5: SQL s Data Definition Language CS1106/CS5021/CS6503 Introduction to Relational Databases

CITS 1401 Problem Solving & Programming

Lecture 5: SQL s Data Definition Language

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Significant Figures IV

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Learning the Language - V

2 Computation with Floating-Point Numbers

Graphics calculator instructions

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

fractional quantities are typically represented in computers using floating point format this approach is very much similar to scientific notation

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Binghamton University. CS-211 Fall Data Conversion. software diversity in action

2. INTRODUCTORY EXCEL

The float type and more on variables FEB 6 TH 2012

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

CEN 414 Java Programming

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Long (LONGMATH) variables may be used the same as short variables. The syntax is the same. A few limitations apply (see below).

Variables, expressions and statements

Floating Point Arithmetic

Data Representation Floating Point

Python Programming: An Introduction to Computer Science

Transcription:

Lecture 2: Python Arithmetic CS1068+ Introductory Programming in Python Dr Kieran T. Herley 2018/19 Department of Computer Science University College Cork

Basic data types in Python

Python data types Programs manipulate data from families called data types; each data type has is own rules and capabilities. int (Integers i.e. Z) e.g. 123, 456 Capabilities: standard arithmetic (+,,, //) and more float (Real numbers R) e.g 3.14, 6.02E23 Capabilities: arithmetic +,,, /; also plethora of maths functions str (Strings i.e. text) e.g To be or not to be. Capabilities: Many text-manipulating functions Many, many others... 1

Integers (int)

Python integers Examples Standard Arabic notation; optional leading sign 123 0 456 12345678901234567890 Non-examples no internal non-digits 1 2 3 456 10lO 12, 345, 678, 901, 234, 567, 890 Notes No limit on number of digits 2

Basic arithmetic Code Result Explanation 1 + 2 3 +,, (multiplication) work as expected 3-4 -1 5 * 6 30 7 + 8 * 9 79 BODMAS rules apply 1-2 - 3-4 (7 + 8) * 9 135 () override default rules 2 ** 3 8 2 to power of 3 Note: spaces around numbers or operators are OK, but not within 3

Integer division operator Integer division operators n // d Integer part when n is divided by d e.g. 10 // 5 yields 2, 10 // 3 yields 3, n % d Remainder when n is divided by d e.g. 10 % 5 yields 0, 10 % 3 yields 1 NB // for integer result (not /) Examples Expression Result Expression Result 8 // 3 2 8 % 3 2 8 // 4 2 8 % 4 0 8 // 5 1 8 % 5 3 4

Division and negative integerss Python s // operator rounds towards (not towards zero); it s the largest integer value less than or equal to the precise real result. Code Result Explanation 5 // 2 2 2.5 2-5 // 2-3 2.5 3 5 // 3 1 1.66 1-5 // 3-2 1.66 2 Note: Python s int division operators obey following identity. (x // y) y + x % y = x 5

Programming errors Misusing Python features causes programming errors, causing program to misbehave and often crash. Code Result Explanation 10l0 SyntaxError Violates Python s rules. Program crashes. n // d ZeroDivisionError (If d = 0). Cannot divide by zero! Program crashes. 1, 000 + 1 (1, 1) Legally OK, but probably not what you had in mind. 6

Operator precedence Precedence Python applies higher precedence operators before lower Op Pre ( ) High, //, % +, Low Association Operators of equal precedence are applied left to right. Code Result Explanation 1 + 2 * (3 + 4) 15 First (), then,, then + 1 + 2-3 + 4-5 -1 Applied l-to-r 7

Real numbers (floats)

Python real numbers (floats) Examples Standard decimal or scientific notation; optional sign 1.23 0 2.718 3.14 5.893E 11 7.35e + 22 6.02E23 Non-examples no extraneous characters 1 2. 3 4, 56 6.02x10 23 Limits on precision; floats are approximations of the true values 8

Inexactitude of Python s reals Code Result Explanation 0.1 + 0.2 0.30000000000000004 Result off in 17th decimal place! math.pi 3.141592653589793 Only 15 decimal places Not all reals can be represented exactly in computer s memory Most are approximated by closest representable number Real calculations are rarely mathematically exact float R 9

Using floats can be tricky Representational and roundoff error issues: Hard to gauge how accurate any result is. Will use floats sparingly in this module. 10

Mixing integers and real numbers Code Result Explanation 1 + 2.0 3.0 ints converted to float automatically in mixed expressions float(1) 1.0 Explicitly converts int to real int(1.23) 1 Converts real to int: truncates decimal part round(3.4) 3 Rounds off to nearest int round(3.5) 4 ditto int(), float(), round() are built-in functions; note function name(...) syntax 11

Real arithmetic and the / operator Code Result Explanation 1.0 + 2.0 * 3.4 7.8 +, -, *, () work as expected 8 / 3 2.666666666666665 The / operator produces a flo result... 8 / 2 4.0 even with integer values 8.5 / 2.7 3.148148148148148 Warning Distinguish carefully between / and //. 12

Python s mathematical functions Python supports multitude of mathematical functions (logs, exponentials, trigonometrics and so on). Code Result Explanation math.sqrt(7) 2.6457513110645907 Need import math at top of program to avail of these features. Look them up as needed. 13

Notes

Notes Code progname.py Description Reading 14