UK CS 115 - Python Programming: An Introduction to Computer Science

Unformatted text preview:

Python Programming: An Introduction to Computer ScienceObjectivesObjectives (cont.)Numeric Data TypesSlide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Using the Math LibrarySlide 13Slide 14Slide 15Slide 16Slide 17Math LibraryAccumulating Results: FactorialSlide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30The Limits of IntSlide 32Slide 33Slide 34Handling Large NumbersHandling Large Numbers: Long IntSlide 37Slide 38Type ConversionsSlide 40Type ConversionSlide 42Python Programming, 2/e 1Python Programming:An Introduction toComputer ScienceChapter 3Computing with NumbersPython Programming, 2/e 2ObjectivesTo understand the concept of data types.To be familiar with the basic numeric data types in Python.To understand the fundamental principles of how numbers are represented on a computer.Python Programming, 2/e 3Objectives (cont.)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, 2/e 4Numeric Data TypesThe information that is stored and manipulated bu 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(.25, .10, .05, .01) are decimal fractionsPython Programming, 2/e 5Numeric 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 6Numeric Data TypesWhole numbers are represented using the integer (int for short) data type.These values can be positive or negative whole numbers.Python Programming, 2/e 7Numeric Data Types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, 2/e 8Numeric 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)<class 'float'>>>> myInt = 32>>> type(myInt)<class 'int'>>>>Python Programming, 2/e 9Numeric Data TypesWhy do we need two number types?Values that represent counts can’t be fractional (you can’t have 3 ½ quarters)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, 2/e 10Numeric Data TypesOperations on ints produce ints, operations on floats produce floats (except for /).>>> 3.0+4.07.0>>> 3+47>>> 3.0*4.012.0>>> 3*412>>> 10.0/3.03.3333333333333335>>> 10/33.3333333333333335>>> 10 // 33>>> 10.0 // 3.03.0Python Programming, 2/e 11Numeric 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 12Using 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 13Using 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!242b b acxa- � -=Python Programming, 2/e 14Using 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 15Using 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 dodiscRoot = math.sqrt(b*b – 4*a*c)Python Programming, 2/e 16Using the Math Library# 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() print("The solutions are:", root1, root2 )main()Python Programming, 2/e 17Using the Math LibraryThis program finds the real solutions to a quadraticPlease enter the coefficients (a, b, c): 3, 4, -1The solutions are: 0.215250437022 -1.54858377035What do you suppose this means?This program finds the real solutions to a quadraticPlease enter the coefficients (a, b, c): 1, 2, 3Traceback (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, 2/e 18Math LibraryIf a = 1, b = 2, c = 3, then we are trying to take the square root of a negative number!Using the sqrt function is more efficient than using **. How could you use ** to calculate a square root?Python Programming, 2/e 19Accumulating Results: FactorialSay you are waiting in a line with five other people. How many ways are there to arrange the six people?720 -- 720 is the factorial of 6 (abbreviated 6!)Factorial is defined as:n! = n(n-1)(n-2)…(1)So, 6! = 6*5*4*3*2*1 = 720Python Programming, 2/e 20Accumulating Results: FactorialHow we could we write a program to do this?Input number to take factorial of, nCompute factorial of n, factOutput factPython Programming, 2/e 21Accumulating Results: FactorialHow did we calculate 6!?6*5 = 30Take that 30, and 30 * 4 = 120Take that 120, and 120 * 3 = 360Take that 360, and 360 *


View Full Document

UK CS 115 - Python Programming: An Introduction to Computer Science

Download Python Programming: An Introduction to Computer Science
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Python Programming: An Introduction to Computer Science and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Python Programming: An Introduction to Computer Science 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?