Agenda Announcements Overview Course Topics Range function List - - PowerPoint PPT Presentation

agenda
SMART_READER_LITE
LIVE PREVIEW

Agenda Announcements Overview Course Topics Range function List - - PowerPoint PPT Presentation

Agenda Announcements Overview Course Topics Range function List BMI 1/14/2013 CompSci101 Peter Lorensen 1 Course topics Variables Types and values String Function For loop If While loop Lists


slide-1
SLIDE 1

Agenda

  • Announcements
  • Overview Course Topics
  • Range function
  • List
  • BMI

1/14/2013 CompSci101 Peter Lorensen 1

slide-2
SLIDE 2

Course topics

1/14/2013 CompSci101 Peter Lorensen 2

  • Variables
  • Types and values
  • String
  • Function
  • For loop
  • If
  • While loop
  • Lists
  • Set
  • Tuples
  • Dictionaries
  • Sorting
  • Recursion
slide-3
SLIDE 3

range(start, end, increment)

1/14/2013 CompSci101 Peter Lorensen 3

  • range(…) simply gives a list of numbers

range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Note that 10 is not included.

range(2, 8) [2, 3, 4, 5, 6, 7] range(6) [0, 1, 2, 3, 4, 5,]

Only 1 number makes it start from 0.

range(0, 10, 2) [0, 2, 4, 6, 8]

Increment by 2

slide-4
SLIDE 4

Lists are simple (we love them)

1/14/2013 CompSci101 Peter Lorensen 4

lst = [ ] points = [982, 234, -123] zoo = [“snake”, “hog”, “tiger”]

Example…

slide-5
SLIDE 5

Lists under the hood

1/14/2013 CompSci101 Peter Lorensen 5

zoo = [“snake”, “hog”, “tiger”] “snake” “hog” “tiger” 1 2

print = zoo[ 0 ] snake

for animal in zoo: print animal snake hog tiger

Access individual elements

For loop for easy run- through of content

slide-6
SLIDE 6

Often Used List Functions

1/14/2013 CompSci101 Peter Lorensen 6

ACCESS item = lst[index] getting a specific item out of the list more = lst[start:stop] slicing several items out of the list with index start & stop n = len(lst) getting the length of the list. Number of item in it. MODIFY lst(i) = “dog”

  • verwrites “dog” on the i’th place

lst.append(...) append an element to lst, changing lst newLst = lst[:] makes a copy of lst into newLst lst.extend(lst2) append every element of lst2 to lst lst.pop(index) remove and return element at position index in lst, so has side-effect of altering list and returns value. If you don’t give an index the last value is automatically popped.

slide-7
SLIDE 7

Often Used List Functions

1/14/2013 CompSci101 Peter Lorensen 7

SEARCHING & SORTING if “dog” in zoo: print dog in keyword finds the element in the list. lst.index(elt) return index of elt in lst, error if elt not in lst lst.count(elt) return number of occurrences of elt in lst max(lst) returns maximal element in lst min(lst) returns minimum element in lst sum(lst) returns sum of elements in list lst lst.sort() sorts the elements of lst

slide-8
SLIDE 8

In-Class Assignment

1/14/2013 CompSci101 Peter Lorensen 8

Write a tiny program that asks and calculate 1 persons BMI

1) Ask the users weight and height (pound, inch) 2) Save in tiny list 3) Use formula to calculate BMI (picking data from list):

Pounds / (inch * inch) * 705

4) Print BMI

slide-9
SLIDE 9

Alma Whitten

1/14/2013 CompSci101 Peter Lorensen 9

  • Google: Engineering Lead for

Privacy, Director for Privacy

– Across marketing and engineering – Why Johnny Can't Encrypt

"It's more and more the case that every individual is going around with a cheap yet powerful data- capture device, and the ability to connect that device to powerful data services. … We need to

Build powerful information tools that are equally available to everyone, and if we are sufficiently transparent about (those tools and how they use data), then the outside world gets to say, yes, it's worth it, we want those tools."

http://news.cnet.com/8301-30684_3-20021090-265.html