Comp 151 More on Arithmetic and intro to Objects
Admin Any questions 2
The Project Lets talk about the project. What do you need A 'accumulator' variable. Start outside of the loop Lets look at your book's interest example 3
Strings Remember Strings from last time Strings represent text. Discussion of data representation in a computer 1000001 = 65 (A) 110000 = 48 (0) Literal strings (actually written out and not in a variable or anything) Represented inside of quotes in python. 4
Documentation Strings Triple quoted strings that are put frst in a function becomes the documentation for that function The help text on that function we ll look at that more closely next time 5
Literal strings Three types of literal strings in python Double quoted strings: Eg: Its called a 'string' in programming Single quoted strings Eg: 'she said how do you do yesterday' Triple quoted strings (either type of triple quotes) Eg this is an example of a triple quoted string What do you notice about each type of string? 6
Getting help Python has good integrated help Very easy from pycharm Highlight the function/command you want help for Press F1(problems on windows) Choose menu<view><quick Documentation Lookup> Or turn on easy showing in settings/preferences Go to settings (or preferences on mac) <editor><general> and select the Show quick documentation on mouse move checkbox. If there is help text, it will be shown Works diferently from command line 7
Data Types Review from last time Data represented very diferently in computer depending on its type Integer, foating point, character, objects Python hides some of this from you, but the distinction still exists. Time cost of this hiding. Can fnd the type of anything in python by using the type functions demo 8
arithmetic + - and * all work as expected results are 'closed' on the integers English translation: integer + integer always yields an integer. division is not closed on the integers There is a major diference between python 2 and python 3 here. Discuss. but in programming it is print( 8/2) shows? print(7/2) shows? 9
Integer division in python3 In python3 can force integer division Using // 7//2 is what? Why would I want this? Grid drawing example Searching a list example Modulus/remainder : % Explain Rational, integer division & random examples 1
Rest of arithmetic ** exponentiation abs() 4**3 = 4*4*4 Absolute value function 1
Libraries Thus far all of our functions are from the builtin module Now we want to use math functions from the math module In order to use stuf from another module need to import it. Lets use math import math Put at top of your fle Module/library terms in code. 1
Math library functions See 3-2 page 68 in the text for some functions Or https://docs.python.org/3.6/library/math.h tml For the whole thing 1
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 a root, you can do discroot = math.sqrt(b*b 4*a*c) How would the above be written in mathematical notation? If time have students work through factorial example for me Python Programming, 2/e 14
The Limits of Int What is 100!? >>> main() Please enter a whole number: 100 The factorial of 100 is 93326215443944152681699238856266700490715968264381621468 59296389521759999322991560894146397615651828625369792082 7223758251185210916864000000000000000000000000 Wow! That s a pretty big number! Python Programming, 2/e 15
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 16
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. Old PCs/raspberry pi s use 32 bits. (newer ones use 64 bits) Python Programming, 2/e 17
The Limits of Int Lets use 32 bits for a Raspberry Pi That means there are 232 possible values, centered at 0. This range then is 231 to 2 31-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 18
Handling Large Numbers Does switching to float data types get us around the limitations of ints? If we initialize the accumulator to 1.0, we get >>> main() Please enter a whole number: 15 The factorial of 15 is 1.307674368e+012 We no longer get an exact answer! Python Programming, 2/e 19
Handling Large Numbers: Long Int Very large and very small numbers are expressed in scientific or exponential notation. 1.307674368e+012 means 1.307674368 * 1012 Here the decimal needs to be moved right 12 decimal places to get the original number, but there are only 9 digits, so 3 digits of precision have been lost. Python Programming, 2/e 20
Handling Large Numbers Floats are approximations Floats allow us to represent a larger range of values, but with lower precision. Python has a solution, expanding ints! Python Ints are not a fixed size and expand to handle whatever value it holds. Python Programming, 2/e 21
Type Conversions We know that combining an int with an int produces an int, and combining a float with a float produces a float. What happens when you mix an int and float in an expression? x = 5.0 + 2 What do you think should happen? Python Programming, 2/e 22
Type Conversions For Python to evaluate this expression, it must either convert 5.0 to 5 and do an integer addition, or convert 2 to 2.0 and do a floating point addition. Converting a float to an int will lose information Ints can be converted to floats by adding.0 Python Programming, 2/e 23
Type Conversion In mixed-typed expressions Python will convert ints to floats. Sometimes we want to control the type conversion. This is called explicit typing. Python Programming, 2/e 24
Type Conversions >>> float(22//5) 4.0 >>> int(4.5) 4 >>> int(3.9) 3 >>> round(3.9) 4 >>> round(3) 3 Now we can get rid of eval for inputs of numbers!!!! How might we do that? Python Programming, 2/e 25
Assignment Read chapter 3 2