Agenda Announcements Functions under the hood Strings Problems - - PowerPoint PPT Presentation

agenda
SMART_READER_LITE
LIVE PREVIEW

Agenda Announcements Functions under the hood Strings Problems - - PowerPoint PPT Presentation

Agenda Announcements Functions under the hood Strings Problems solved via strings The BIG SEECRET OF STRINGS Often used operators Often used functions For loop (list) 1/14/2013 CompSci101 Peter Lorensen 1


slide-1
SLIDE 1

Agenda

  • Announcements
  • Functions – under the hood
  • Strings

– Problems solved via strings – The BIG SEECRET OF STRINGS – Often used operators – Often used functions

  • For loop
  • (list)

1/14/2013 CompSci101 Peter Lorensen 1

slide-2
SLIDE 2

%

1/14/2013 CompSci101 Peter Lorensen 2

slide-3
SLIDE 3

How Python executes a function call

1. Evaluate the argument (at the call site) 2. Assign the formal parameter name to the argument’s value

– A new variable, not reuse of any existing variable of the same name

3. Evaluate the statements in the body one by one 4. At a return statement:

– Remember the value of the expression – Formal parameter variable disappears – exists only during the call! – The call expression evaluates to the return value

Function definition Variables: x: 7 square(3 + 4)

University of Washington, Mike Ernst, 2013

def square(x): return x * x Current expression: 1 + square(3 + 4) 1 + square(7) 1 + 49 50 return x * x return 7 * x return 7 * 7 return 49

evaluate this expression

Formal parameter (a variable) Actual argument

slide-4
SLIDE 4

Strings & Avatar

  • “Sometimes your whole life boils down to one

insane move!” (Jake Sully, Avatar, 2009)

  • Oftentimes the core logic of a program boils

down to one big string move!

slide-5
SLIDE 5

Problems that are solved via strings

  • Problem 1: Web crawler

– 1) Get all the links from a web page. – 2) Go to the web pages found and do 1)

  • Problem 2: Password checker

– Are there 8 characters? – Is there at least one number? – Is there at least one capital letter? – Is there at least one symbol?

  • Problem 3: APT “Forming Acronyms”

– How do you pick out each word? – How do you pick out the first letter in a word?

slide-6
SLIDE 6

The Seecret of Strings

  • Python sees a String as a

sequence

  • Each character gets a number (2)

boy[ 0 ] P boy[ -2 ] e boy[ 0:2 ] Pe boy[ 2:4 ] te

P e t e r

  • 5
  • 4
  • 3
  • 2
  • 1

1 2 3 4

boy

boy = “Peter”

slide-7
SLIDE 7

A few string operators

Operator

= Get / assign == Equals . Call function in Answer true/false if string is inside another string

Type

* +

str

Repeat Append

int

Multiplication Addition (sum)

slide-8
SLIDE 8

String functions

Function Description

Result with string res = ‘Team Blue Devils’ res.upper() returns string upper case version of string res TEAM BLUE DEVILS res.count(‘am’) returns int number of (non-overlapping)

  • ccurrences of sub in s

1 res.find(‘m’)

returns int first index at which sub occurs in s or -1 if no occurrence

3 res.split()

returns list of s split on whitespace

[‘Team’, ‘Blue’, ‘Devils’] res.split(‘l’)

returns list of s split on sep, a delimiter

[‘Team B’, ‘ue Devi’, ‘s’] res.strip()

returns copy of res withOUT leading and trailing whitespace

Team Blue Devils len( res ) returns the number of characters in the string (including white spaces) 16 res.isupper() returns true if all the characters are in upper case False

  • res = ‘Team Blue Devil wins’
slide-9
SLIDE 9

Sir Tim Berners-Lee

I want you to realize that, if you can imagine a computer doing something, you can program a computer to do that. Unbounded opportunity... limited

  • nly by your imagination. And a

couple of laws of physics.

  • HTTP, URL, HTML

– How, Why, What, When?

slide-10
SLIDE 10

Loops

Loops

for while

for element in sequence: do this........ ...... while statement: do this........ ...... For each element in the sequence please do the following... While the statement is true please do the following... Great for list with well known number

  • f elements

Great for all other cases with unkown number of elements

slide-11
SLIDE 11

Under the hood of loops

  • Loops is the answer

to how you go through a string

Pick out all the capital letters of the following sentence: red = ‘Most Wanted Criminel’ result = “” for char in red: if char.isupper(): result = result + char print result

adr = “pete@duke.edu” for letter in adr: print letter e t e @ d u k e . e d u p letter