1 / 20

Today’s topics

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. Data and lists?.

Télécharger la présentation

Today’s topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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 Text Can all information be represented using lists ? Sounds/Speech Networks Images/Video Ideas?

  3. 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 memory location 304 Computation Data Storage id, del

  4. True False Python Data Types Numeric Name Example What is it? values with a fractional part float 3.14 long 10**100 integers > 2147483647 int 42 integers <= 2147483647 the results from a comparison: ==, !=, <, >, <=, >= bool "Boolean value"

  5. 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

  6. * / % + - > < == Precedence Caution Level = ( ) set equal to Highest / divide ** Python Operators % remainder - ** power == is equal to * + > as usual = < Lowest - ( )

  7. % 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 What happens on these years?

  8. 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 is it really a coincidence that this looks like the string theory picture?? 42 name: x type: int LOC:300 4 bytes for an int

  9. string functions str(42)returns'42' converts input to a string str len + * len('42')returns2 returns the string’s length 'XL' + 'II'returns 'XLII' concatenates strings 'VI'*7returns 'VIVIVIVIVIVIVI' repeats strings s1 = "ha" Given these strings s2 = "t" What are s1 + s2 2*s1 + s2 + 2*(s1+s2)

  10. String surgery s = ’duke university baby' 0 1 2 3 4 5 6 7 8 9 18 10 11 12 13 14 15 16 17 19 s[ ] indexesinto 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 python!=English

  11. More on strings • Negative indices count from the back s[-1]returns ‘y' s[-11]returns s[-0]returns s[ : ] slices the string, returning a substring s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1

  12. Commas separate elements. Lists ~ Strings of anything 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 'hi'

  13. "Quiz" Raising and razing lists Name(s): pi = [3,1,4,1,5,9] Q = [ 'pi', "isn't", [4,2] ] message = 'You need parentheses for chemistry !' Part 1 Part 2 len(pi) Q[0] len(Q) What are Q[0:1] len(Q[1]) What are Q[0][1] Q[1][0] What slice of pi is [3,1,4] What slice of pi is [3,4,5] What is message[9:15] What are pi[0] * (pi[1] + pi[2]) and pi[0] * (pi[1:2] + pi[2:3]) What is message[::5] Extra! Mind Muddlers What is pi[pi[2]]? How many nested pi's before pi[…pi[0]…] produces an error?

  14. Functioning in Python Some basic, built-in functions: bool float int long list str abs max min sum range round absolute value of lists these change data from one type to another creates lists only as accurately as it can! dir help These are the most important:

  15. Functioning in Python Far more are available in separate files, or modules: import math math.sqrt( 1764 ) dir(math) accesses math.py's functions lists all of math.py's functions same, but without typing math. all of the time… from math import * pi sin( pi/2 ) help() help modules

  16. Functioning in Python # my own function! def dbl( x ): """ returns double its input, x """ return 2*x

  17. Functioning in Python # my own function! def dbl( x ): """ returns double its input, x """ return 2*x keywords Some of Python's baggage… defstarts the function returnstops it immediately and sends back the return value Docstrings They become part of python's built-in help system! With each function be sure to include one that Comments describes overall what the function does, and explains what the inputs mean/are They begin with#

  18. 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')) strings, lists, numbers … all data are fair game

  19. random thoughts import random random.choice( range(0,100) ) dir help These are the most important:

  20. DNA Our DNA's nucleotides: http://genome.ucsc.edu/cgi-bin/hgGateway AGCT

More Related