月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫

Slides:



Advertisements
Similar presentations
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
Advertisements

Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
Python Programming: An Introduction to Computer Science
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Functions and methods A method is a function that is a member of a class A method is a function that is a member of a class FCL(Framework Class Library)
Chapter 3 Computing with Numbers
Chapter 7 Decision Structures
WHAT KIND OF THING IS IT? Python: Basic Numeric Types
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers Python Programming, 2/e1.
Announcements Class / Recitation slides available on the syllabus page Check morning of for latest version We will start grading clickers next lecture.
CSC 110 Numeric data types [Reading: chapter 3] CSC 110 D 1.
Vahé Karamian Python Programming CS-110 CHAPTER 3 Computing with Numbers.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
Computer Science 111 Fundamentals of Programming I Number Systems.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may.
CSC1015F – Chapter 3, Computing with Numbers Michelle Kuttel
Python Types Python values are of various “types” Ints, Floats, Strings, Characters, and more Two representations of numbers 1 vs 1.0.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Variables, Expressions, and Standard Functions. Topics Basic calculation Expressions, variables, and operator precedence Data types Input / Output Examples.
STANDARD FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
Topics: IF If statements Else clauses. IF Statement For the conditional expression, evaluating to True or False, the simple IF statement is if : x = 7.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Computers and Numbers. Types of Numbers Computers store two different types of numbers: Whole Numbers AKA Integers (mathematics) AKA Fixed Point Numbers.
3. COMPUTING WITH NUMBERS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Programming Languages Programming languages are a compromise between spoken language and formal math. They allow humans to communicate with computers at.
Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A def circumference(diameter):
Python Programming, 3/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Cosc 2150: Computer Organization Chapter 9, Part 3 Floating point numbers.
Python Programming, 3/e1 Python Programming: An Introduction to Computer Science Chapter 7 Decision Structures.
Variables, Operators, and Expressions
Simple C Programs.
Python Boot Camp Booleans While loops If statements Else clauses
Today Variable declaration Mathematical Operators Input and Output Lab
Python Programming: An Introduction to Computer Science
Copyright © Cengage Learning. All rights reserved.
Introduction to Computing Science and Programming I
Arithmetic Operations
ECE 1304 Introduction to Electrical and Computer Engineering
Built-in MATLAB Functions Chapter 3
Floating Point Math & Representation
BIL 104E Introduction to Scientific and Engineering Computing
TMF1414 Introduction to Programming
Python Programming: An Introduction to Computer Science
Computer Simulation Lab
FUNCTIONS EXAMPLES.
Fundamental of Java Programming Basics of Java Programming
Introduction to Programming
Math Library and IO formatting
Evaluate nth Roots and Use Rational Exponents
8/8/17 Warm Up Solve the inequality.  .
Fundamentals of Programming I Number Systems
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
15-110: Principles of Computing
CS190/295 Programming in Python for Life Sciences: Lecture 3
CHAPTER 3: String And Numeric Data In Python
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
class 5 retrieving a previous statement in Thonny or IDLE // %
Output Formatting Bina Ramamurthy 4/16/2019 BR.
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Chopin’s Nocturne.
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Presentation transcript:

月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫 Python Programming, 3/e

春望 國破山河在,城春草木深。 感時花濺淚,恨別鳥驚心。 烽火連三月,家書抵萬金。 白頭搔更短,渾欲不勝簪。 杜甫 Python Programming, 3/e

Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers Python Programming, 3/e

Objectives To understand the concept of data types. To be able to use the Python math library. To understand the accumulator program pattern. To be able to read and write programs that process numerical data. Python Programming, 3/e

Numeric Data Types The information that is stored and manipulated by computers programs is referred to as data. There are two different kinds of numbers! (5, 4, 3, 6) are whole numbers – they don’t have a fractional part (0.25, 0.10, .05, .01) are decimal fractions Python Programming, 3/e

Numeric Data Types Inside the computer, whole numbers and decimal fractions are represented quite differently! We say that decimal fractions and whole numbers are two different data types. The data type of an object determines what values it can have and what operations can be performed on it. https://www.swarthmore.edu/NatSci/echeeve1/Ref/BinaryMath/NumSys.html Python Programming, 3/e

Numeric Data Types Whole numbers are represented using the integer (int for short) data type. Numbers that can have fractional parts are represented as floating point (or float) values. How can we tell which is which? A numeric literal without a decimal point produces an int value A literal that has a decimal point is represented by a float (even if the fractional part is 0) Python Programming, 3/e

Numeric Data Types Python has a special function type() to tell us the data type of any value. >>> type(3) <class 'int'> >>> type(3.1) <class 'float'> >>> type(3.0) >>> myInt = 32 >>> type(myInt) >>> Python Programming, 3/e

Numeric Data Types Why do we need two number types? Values that represent counts can’t be fractional (you can’t have 3.5 computers) Most mathematical algorithms are very efficient with integers The float type stores only an approximation to the real number being represented! Since floats aren’t exact, use an int whenever possible! Python Programming, 3/e

Float is less accurate a = 4/3 # 1.3333333333333333 sum = 0.0 for i in range(600): sum = sum + a print(sum) 800.0000000000052 Python Programming, 3/e

Numeric Data Types Integer division produces a whole number. That’s why 10//3 = 3! Think of it as ‘gozinta’, where 10//3 = 3 since 3 gozinta (goes into) 10 3 times (with a remainder of 1) 10%3 = 1 is the remainder of the integer division of 10 by 3. a = (a//b)(b) + (a%b) Python Programming, 3/e

Numeric Data Types Operations on floats produce floats , operations on ints produce ints (except for /). >>> 3.0+4.0 7.0 >>> 3+4 7 >>> 3.0*4.0 12.0 >>> 3*4 12 >>> 10.0/3.0 3.3333333333333335 >>> 10/3 >>> 10 // 3 3 >>> 10.0 // 3.0 3.0 Actually, the operation // is a successive subtraction. Python Programming, 3/e

Python Integer Division 一顆橘子 0.5 元, 若你有 17.6 元, 請問共可買到多少顆橘子, 共花掉多少錢? Solution 1: 17.6 // 0.5 # 35.0 0.5 * 35.0 # 17.5 Solution 2: 17.6 / 0.5 # 35.2 int(35.2) # 35 0.5 * 35 # 17.5 Python Programming, 3/e

Type Conversions >>> 5.5 + 2 7.5 >>> float(22//5) 4.0 >>> int(4.5) 4 >>> int(3.9) 3 >>> round(3.9) >>> round(3) Python Programming, 3/e

四拾五入到小數以下四位 a = 123.142857 round(a) # 123 round( a * 10000 ) # 1231429 123.1429 round(123.142857, 4) Python Programming, 3/e

Exercise: BMI (Body Mass Index) Input: mass (in kg) and height (in m). Output: BMI Show the result with a precision of 2 fraction digits Relationship: BMI = mass / (height ** 2) Round the result because we only need a digit in decimal numbers. Instead of 22.460034350640772 We only want 22.5 Python Programming, 3/e

Using the Math Library Besides (+, -, *, /, //, **, %, abs), we have lots of other math functions available in a math library. A library is a module with some useful definitions/functions. Python Programming, 3/e

Using the Math Library Let’s write a program to compute the roots of a quadratic equation! The only part of this we don’t know how to do is find a square root… but it’s in the math library! Python Programming, 3/e

Using the Math Library To use a library, we need to make sure this line is in our program: import math Importing a library makes whatever functions are defined within it available to the program. Python Programming, 3/e

Using the Math Library To access the sqrt library routine, we need to access it as math.sqrt(x). Using this dot notation tells Python to use the sqrt function found in the math library module. To calculate the root, you can do discRoot = math.sqrt(b*b – 4*a*c) 判別式(Discriminant) Python Programming, 3/e

Using the Math Library Python Programming, 3/e # quadratic.py # A program that computes the real roots of a quadratic equation. # Illustrates use of the math library. # Note: This program crashes if the equation has no real roots. import math # Makes the math library available. def main(): print("This program finds the real solutions to a quadratic") print() a, b, c = eval(input("Please enter the coefficients (a, b, c): ")) discRoot = math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print("The solutions are:", root1, root2 ) main() Python Programming, 3/e

Using the Math Library What do you suppose the following error means? This program finds the real solutions to a quadratic Please enter the coefficients (a, b, c): 3, 4, -1 The solutions are: 0.215250437022 -1.54858377035 What do you suppose the following error means? This program finds the real solutions to a quadratic Please enter the coefficients (a, b, c): 1, 2, 3 Traceback (most recent call last): File "<pyshell#26>", line 1, in -toplevel- main() File "C:\Documents and Settings\Terry\My Documents\Teaching\W04\CS 120\Textbook\code\chapter3\quadratic.py", line 14, in main discRoot = math.sqrt(b * b - 4 * a * c) ValueError: math domain error >>> Python Programming, 3/e

Math Library If a = 1, b = 2, c = 3, then we are trying to take the square root of a negative number! You may also use x**(0.5) to calculate a square root, but using the sqrt() function is more efficient than using **. Python Programming, 3/e

Table 3.2 Some math library functions Python Mathematics English pi  An approximation of pi e An approximation of e sqrt(x) The square root of x sin(x) sin x The sine of x cos(x) cos x The cosine of x tan(x) tan x The tangent of x. asin(x) arcsin x The inverse of sine x. log(x) ln x The natural (base e) logarithm of x log10(x) log10x The common (base 10) logarithm of x. exp(x) ex The exponential of x ceil(x) The smallest integer >= x floor(x) The largest integer <= x Python Programming, 3/e

Exercise Adjusting grades Chinese Remainder Theorem 漲停板 Python Programming, 3/e

Accumulating Results: Summation for i in range(11): sum = sum + i print(sum) Demo with Python Tutor Python Programming, 3/e

Exercise: Accumulator Input N Calculate sum = 1 + 3 + 5 + … + (2N-1) Output sum Python Programming, 3/e