Today’s topics Notes from Zachary Dodds’ CS 5 course at Harvey Mudd Upcoming More Python Reading How to Think, 7.1-7.7 & 9.1-9.12 CompSci 001 4.1 Data and lists? ‘Many years later, as he faced the firing squad, Colonel Aureliano Buendia was to remember that distant afternoon when his father took him to discover ice.’ { 2, 3, 5, 7, 11 } Sets Can all information be represented using lists ? Sounds/Speech Networks CompSci 001 Text Images/Video 4.2 Ideas? Inside the machine… What's happening in python: x = 41 y = x + 1 What is happening behind the scenes: "variables as containers" 42 41 name: y type: int LOC: 304 name: x type: int LOC: 300 memory location 300 Computation CompSci 001 memory location 304 Data Storage id, 4.3del Python Data Types Numeric Name Example float 3.14 values with a fractional part long 10**100 integers > 2147483647 int bool 42 True False What is it? integers <= 2147483647 the results from a comparison: ==, !=, <, >, <=, >= "Boolean value" CompSci 001 4.4 Datatypes as genes… Dominant What will these results be? float 1.0 / 5 long 10**100 - 10**100 int 1 / 5 bool 41 + True Recessive CompSci 001 4.5 Precedence ( ) Caution Level Highest ** - * / + % Python Operators set equal to = divide / remainder % power ** is equal to == * - + > < == > as usual = Lowest < - CompSci 001 ( 4.6 ) % the "mod" operator x%y returns the remainder when x is divided by y 7 % 3 8 % 3 9 % 3 16 % 7 x%2 == 0 For what values of x are these True? x%2 == 1 x%4 == 0 CompSci 001 What happens on these years? 4.7 Computer Memory Random Access Memory (RAM) is a long list of memory locations on or off bit = 1 "bucket" of charge byte = 8 bits word = 4 bytes = 32 bits 42 is it really a coincidence that this looks like the string theory picture?? name: x type: int LOC: 300 CompSci 001 4 bytes for an int 4.8 string functions str len + * str(42) returns '42' converts input to a string len('42') returns 2 returns the string’s length 'XL' + 'II' returns 'XLII' concatenates strings 'VI'*7 returns 'VIVIVIVIVIVIVI' Given these strings What are repeats strings s1 = "ha" s2 = "t" s1 + s2 2*s1 + s2 + 2*(s1+s2) CompSci 001 4.9 String surgery s = ’duke university baby' 0 1 2 3 4 5 6 7 8 9 s[ ] 10 11 12 13 14 15 16 18 17 19 indexes into the string, returning a one-character string index s[0] returns ’d' Read "s-of-zero" or "s-zero" s[6] returns s[ ] returns 'e' Which index returns 'e'? s[len(s)] returns CompSci 001 4.10 python != English More on strings Negative indices count from the back s[-1] returns ‘y' s[-11] returns s[-0] s[ : ] returns s[ : : ] slices the string, returning a substring skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 CompSci 001 4.11 Lists ~ Strings of anything Commas separate elements. L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. len(L) L[0] Indexing: could return a different type Slicing: always returns the same type L[0:1] How could you extract from L CompSci 001 'hi' 4.12 Raising and razing lists Name(s): pi = [3,1,4,1,5,9] "Quiz" Q = [ 'pi', "isn't", [4,2] ] message = 'You need parentheses for chemistry !' Part 1 What are Part 2 len(pi) len(Q) len(Q[1]) What are What slice of pi is [3,1,4] What slice of pi is [3,4,5] Q[0] Q[0:1] Q[0][1] Q[1][0] What is message[9:15] What are pi[0] * (pi[1] + pi[2]) and What is message[::5] pi[0] * (pi[1:2] + pi[2:3]) Extra! Mind Muddlers CompSci 001 What is pi[pi[2]]? 4.13 How many nested pi's before pi[…pi[0]…] produces an error? Functioning in Python Some basic, built-in functions: abs bool absolute value max float min int of lists sum range round these change data from one type to another long list str creates lists only as accurately as it can! These are the most important: CompSci 001 help dir 4.14 Functioning in Python Far more are available in separate files, or modules: import math accesses math.py's functions math.sqrt( 1764 ) dir(math) lists all of math.py's functions from math import * pi same, but without typing math. all of the time… sin( pi/2 ) CompSci 001 help() help modules 4.15 Functioning in Python # my own function! def dbl( x ): """ returns double its input, x """ return 2*x CompSci 001 4.16 Functioning in Python # my own function! def dbl( x ): """ returns double its input, x """ return 2*x keywords Some of Python's baggage… def starts the function return stops it immediately and sends back the return value Comments They begin with # CompSci 001 Docstrings They become part of python's built-in help system! With each function be sure to include one that (1) describes overall what the function does, and (2) explains what the inputs mean/are 4.17 Functioning in Python # is it dis-0 or dis-O, anyway? def undo(s): """ this "undoes" its string input, s """ return 'de' + s >>> undo('caf') >>> undo(undo('caf')) CompSci 001 strings, lists, numbers … all data are fair game 4.18 random thoughts import random random.choice( range(0,100) ) These are the most important: CompSci 001 help dir 4.19 DNA Our DNA's nucleotides: http://genome.ucsc.edu/cgi-bin/hgGateway AGCT CompSci 001 4.20