Python Programming: An Introduction to Computer Science

Slides:



Advertisements
Similar presentations
Fractions Review.
Advertisements

©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Python Programming: An Introduction to Computer Science
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Chapter 3 Computing with Numbers
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.
Solving Quadratic Equations Section 1.3
Announcements Class / Recitation slides available on the syllabus page Check morning of for latest version We will start grading clickers next lecture.
Copyright © Cengage Learning. All rights reserved. 0 Precalculus Review.
CSC 110 Numeric data types [Reading: chapter 3] CSC 110 D 1.
Vahé Karamian Python Programming CS-110 CHAPTER 3 Computing with Numbers.
The Real Number System. Real Numbers The set of all rational and the set of all irrational numbers together make up the set of real numbers. Any and all.
1 COMS 161 Introduction to Computing Title: Numeric Processing Date: October 22, 2004 Lecture Number: 24.
Computing with Numbers Zelle - Chapter 3 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science, John.
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.
Operations with Fractions REVIEW CONCEPTS. Fractions A number in the form Numerator Denominator Or N D.
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.
Calculus 3.4 Manipulate real and complex numbers and solve equations AS
1 Data Representation Characters, Integers and Real Numbers Binary Number System Octal Number System Hexadecimal Number System Powered by DeSiaMore.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
ALGEBRA 1 SECTION 10.4 Use Square Roots to Solve Quadratic Equations Big Idea: Solve quadratic equations Essential Question: How do you solve a quadratic.
Level 3 / 4 Basic Facts Stage 7 Advanced Multiplicative.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Programming Languages Programming languages are a compromise between spoken language and formal math. They allow humans to communicate with computers at.
AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence Copyright © 2009–2012 National Academy Foundation. All rights.
Slide Copyright © 2009 Pearson Education, Inc. Slide Copyright © 2009 Pearson Education, Inc. Chapter 1 Number Theory and the Real Number System.
Thinking about programming Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
MATH BASICS Learning the Terminology. Look at the following problem: How many even prime numbers are there between 0 and 100. A. 0 B. 1 C. 2 D. 3 E. 4.
REAL RATIONAL NUMBERS (as opposed to fake numbers?) and Properties Part 1 (introduction)
Python Programming, 3/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Thinking about programming
Python Boot Camp Booleans While loops If statements Else clauses
Real Numbers and Their Properties
Fractions A number in the form Numerator Denominator Or N D.
Python Programming: An Introduction to Computer Science
Chapter 7 Objectives Define basic terms in algebra: integer, number statement, expression, and coefficient Learn the relationships between positive and.
Variables, Expressions, and IO
Real Zeros Intro - Chapter 4.2.
Thinking about programming
Algebra II (Honors) Chapter 1
Data Analysis using Python-I
Math in C The math blocks you've used in Scratch can all be recreated in C!
Numerical Functions & Tricks
Fractions Review.
Fractions Review.
Wednesday 09/23/13.
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
Introduction to Value-Returning Functions: Generating Random Numbers
Fractions Review.
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Fractions Review.
Fractions Review.
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Fractions Review.
Data Types and Maths Programming Guides.
Class code for pythonroom.com cchsp2cs
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

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

Counting, Python Style Python Programming, 2/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 integers– they don’t have a fractional part (.25, .10, .05, .01) are floating point Python Programming, 2/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. Python Programming, 2/e

Numeric Data Types Python has a special function 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, 2/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, 2/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, 2/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, 2/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, 2/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) Python Programming, 2/e

Using the Math Library Python Programming, 2/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, 2/e

The Limits of Int What is 100!? Wow! That’s a pretty big number! >>> main() Please enter a whole number: 100 The factorial of 100 is 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 Wow! That’s a pretty big number! Python Programming, 2/e

The Limits of Int Newer versions of Python can handle it, but… Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> import fact >>> fact.main() Please enter a whole number: 13 13 12 11 10 9 8 7 6 5 4 Traceback (innermost last): File "<pyshell#1>", line 1, in ? fact.main() File "C:\PROGRA~1\PYTHON~1.2\fact.py", line 5, in main fact=fact*factor OverflowError: integer multiplication Python Programming, 2/e

The Limits of Int What’s going on? While there are an infinite number of integers, there is a finite range of ints that can be represented. This range depends on the number of bits a particular CPU uses to represent an integer value. Typical PCs use 32 bits. Python Programming, 2/e

The Limits of Int Typical PCs use 32 bits That means there are 232 possible values, centered at 0. This range then is –231 to 231-1. We need to subtract one from the top end to account for 0. But our 100! is much larger than this. How does it work? Python Programming, 2/e

Chapter 3 problems - page 76+ Reminder of Chapter 2 problems: Discussion problems 2,4 Page 52 Programming problems 4, 10 Page 54 Chapter 3 Problems – page 76+ Programming exercises #1, 3, 8, 17 Enrichment: Programming exercises #11,12,16 Python Programming, 2/e