Top Banner
Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review Wednesday (by Dr. Martino) No Class Friday Week 7 Class Wed /Fri as normal Midterm Thursday Project due Sunday
57

Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Dec 27, 2015

Download

Documents

Andrea Howard
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

AnnouncementsProject 2

Available Tomorrow (we will send mail)Will be due 11:59PM October 9th (Sunday)

Week 6 ( I will be traveling this week)Review Wednesday (by Dr. Martino)No Class Friday

Week 7Class Wed /Fri as normalMidterm Thursday Project due Sunday

Page 2: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Python Boot CampIntroduction to Ranges

Sequences

Definite LoopsFor Loops

Comparison of For and While

Page 3: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Our First For Loop# File: chaos.py# A simple program illustrating chaotic behavior

def main(): print("This program illustrates a chaotic

function") x = eval(input("Enter a number between 0 and 1:")) for i in range(10): x = 3.9 * x * (1 - x) print(x)

main()

Page 4: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Executing our First For Loop

>>> This program illustrates a chaotic functionEnter a number between 0 and 1: .50.9750.09506250.3354999222660.8694649252590.4426331091130.9621652553370.1419727793620.47508438620.9725789275370.104009713267>>>

Page 5: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Definite Loops

In chaos.py, what did range(10) do?>>> list(range(10))[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

range is a built-in Python function that generates a sequence of numbers, starting with 0.

list is a built-in Python function that turns the sequence into an explicit list (sequence)

The body of the loop executes 10 times.

Page 6: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

RangesPython allows us to specify a range of values

range(n) 0, 1, …, n-1

Example:

list(range(10))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Page 7: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Ranges ContinuedBut what if we don’t want to start from 0?

range(n) is short hand for range(0, n)

Example:

list(range(-4, 4))

[-4, -3, -2, -1, 0, 1, 2, 3]

Page 8: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Ranges Continued But what if I don’t want to count up by 1

Python allows us to “step” by a given integer range(start, end, step)

Example:

list(range(0, 10, 2))

[0, 2, 4, 6, 8]

Page 9: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Ranges … againBut what if I want to count down?

Python allows us to “step” by a given integer range(start, end, step)

Lets try:

list(range(0,10, -1))

[]

Example that works:

list(range(10, 0, -1))

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Page 10: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Definite LoopsA definite loop executes a definite number of

times, i.e., at the time Python starts the loop it knows exactly how many iterations to do.

for <var> in <sequence>:<body>

The beginning and end of the body are indicated by indentation.

Sequences: ranges, lists, strings

Page 11: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Definite Loopsfor <var> in <sequence>:

<body>

The variable after the for is called the loop index. It takes on each successive value in sequence.

Page 12: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

For (Definite) LoopsThese loops have a more controlled structure

and cannot run infinitely

This type of loop is commonly used with the range(x,y) function

for x in range(0,10):print (x)

for x in list(range(0,10)):print (x)

Page 13: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Definite Loops>>> for i in [0,1,2,3]:

print (i)

0

1

2

3

Page 14: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Definite Loops>>> for i in range(0,4):

print (i)

0

1

2

3

Page 15: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Definite Loops>>> for i in list(range(0,4)):

print (i)

0

1

2

3

Page 16: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Definite Loops>>> for odd in [1, 3, 5, 7]:

print(odd*odd)

1

9

25

49

Page 17: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

CQ:Are these programs equivalent?

for a in range(0, 10, 1): print(a)

for a in range(10): print(a)

21

A: yes

B: no

Page 18: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Definite Loopsfor loops alter the flow of program execution, so

they are referred to as control structures.

more items in <sequence>

<var> = new item

<body>

yes

no

Page 19: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

More Complex Examples

x = 0for a in range(10): x = x + aprint(x)

x = 1for a in range(1,10): x = x * aprint(x)

Page 20: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

How to “visualize” a loopYou can use print statements to figure out what

is happening inside a loopThis works for For loops and While loops

What should we print?The loop index

This will give us information of how the loop index changes as the loop executes

Any calculations we may perform

Page 21: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

More Complex Examples

x = 0for a in range(10): print(“a is: ”, a) x = x + a print(“x is: ”, x)print(x)

a is: 0x is: 0a is: 1x is: 1a is: 2x is: 3a is: 3x is: 6a is: 4x is: 10a is: 5x is: 15a is: 6x is: 21a is: 7x is: 28a is: 8x is: 36a is: 9x is: 45

Page 22: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

More Complex Examples

x = 1for a in range(1,10): print(“a is: ”, a) x = x * a print(“x is: ”, x)print(x)

a is: 1x ix: 1a is: 2x ix: 2a is: 3x ix: 6a is: 4x ix: 24a is: 5x ix: 120a is: 6x ix: 720a is: 7x ix: 5040a is: 8x ix: 40320a is: 9x ix: 362880

Page 23: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

CQ:Are these programs equivalent?

A: Yes

B: No

x = 0y = 0

for k in range(5):x = x + ky = x + k

print (y)

x = 0y = 0

for k in range(5):x = x + k

y = x + kprint (y)

Page 24: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Loops can be nested

def complex(a, b): for x in range(0, a):

for y in range (0, b): print(x*y)

Page 25: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Visualizing a Nested Loop

def complex(a, b): for x in range(0, a): print(“x is: ”, x)

for y in range (0, b): print(“y is:”, y) print(x*y)

x is: 0y is: 00y is: 10y is: 20x is: 1y is: 00y is: 11y is: 22

Page 26: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

CQ:Are these functions equivalent?

def complex(a,b): for x in range(0, a):

for y in range (0, b): print(x*y)

def complex2(a,b): for y in range(0,b):

for x in range (0, a): print(x*y)

A: yes

B: no

Page 27: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

When might they be equivalent?

What about when a=b? Ie we call the functions and provide the same

values for a and bcomplex(2,2) = complex2(2,2)

Page 28: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Differences between While and For

For loops provide a a finite and enumerated “range” of valuesThis determines the “length” of the loop

For loops explicitly rebind the loop indexfor X in …

While loops express an execution condition It executes until that condition no longer holds

Page 29: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Chaos with a While# File: chaos.py# A simple program illustrating chaotic behavior

def main(): print("This program illustrates a chaotic

function") x = eval(input("Enter a number between 0 and 1:")) a = 0 while a < 10: x = 3.9 * x * (1 - x) print(x) a = a+1

main()

Page 30: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

CQ:Are these programs equivalent?

a = 0while(a < 10): print(a) a = a+1

for a in range(10): print(a)

21

A: yes

B: no

Page 32: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Announcements

Page 33: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Python Boot CampHow to specify strings

Special Characters

ASCII

Unicode

Page 34: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

StringsIn addition to basic data types such as Integers

most languages provide Strings

Strings are sequences of characters. We have seen a few examples already.“This is a string”

Strings are designated using “”

Page 35: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Strings and how we specify them

Strings are defined with quotations“hello”

There are four ways we can define a string ‘hello’“hello”“””hello””” ‘’’hello’’’

Page 36: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Why do we have three versions?

We can use ‘ ‘ to print strings that contain “ Example: print ’ “ ‘Will print “Example: print “ ‘ “Will print ‘

We can use “”” “”” to print strings that span multiple lines

Page 37: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

What about special characters?

There are more characters than we have on our keyboardsOther languages (Greek, Chinese, etc)Characters with accent marks

Recall from recitation that we can encode charactersWe presented ASCII

Page 38: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

UnicodeUnicode is another way to encode characters

Just like there is a limit to the largest integer we can encode in 32 bits there is a limit to the amount of characters we can encode in ASCII

Full Unicode encoding is available at:http://www.unicode.org/charts/

Page 39: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Unicode Examples

Page 40: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

How do we use Unicode in Strings?

“\uXXXX” is a Unicode character, where XXXX is a code and each X can be 0-9 or A-FWe can use such characters in strings“This is a unicode character: \uFFFF”

One Catch (only in Python 2.x):We need to specify a ‘u’ before the string to tell

python it contains unicode charactersprint u”This is a unicode character: \uFFFF”

Page 41: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Try this:

print (”This is a unicode character: \uFFFF”)

print (”This is a unicode character: \uFFFF”)

Page 42: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

There are other special Characters

“\b” is backspaceExample: print (“hello\bworld”)prints hellworld

“\n” is a newline (like pressing the Enter key”Example: print (“hello\nworld”)prints hello

world

Page 43: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

There are other special Characters

“\t” is a tabExample: print (“hello\tworld”)prints hello world

Page 44: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

String OperationsWe already know how to concatenate strings

Example: print “Hello”+”World”Will print HelloWorld

We also know how to get the length of a stringExample: print len(“HelloWorld”)Will print 10

Page 45: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

String OperationsWe also know how to print the ASCII encoding of a

characterExample: print ord(‘b’)Will print 98

We can leverage this to print the ASCII encoding of a string:

str = “hello”

for char in str:

print ord(char)

Page 46: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Advanced String Operations

But first we need to introduce some new syntax

Object.Method(arguments)

We will study Object Oriented programming more formally later in the course

Page 47: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Objects and MethodsMethods are similar to functions

We can think of a Method as a function or an action performed on an Object

An Object can be anythingMore concretely, any python value

We will revisit this definitionFor now we will consider strings as Objects

Different types support different methods

Page 48: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Advanced String Operations

print (“Hello World”.capitalize())Hello world

print (“hello world”.capitalize())Hello world

print (“Hello World”.endswith(“World”))True

print (“Hello World”.endswith(“Hello”))False

Page 49: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

CQ:Are these programs equivalent?

1.capitalize() “1”.capitalize()

21

A: yes

B: no

Page 50: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Advanced String Operations

print (“HeLlO WoRlD”.lower())hello world

print (“hello world”.lower())hello world

print (“Hello World”.upper())HELLO WORLD

print (“hello world”.upper())HELLO WORLD

Page 51: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Advanced String Operations

print (“Hello World”.find(“World”))6

print (“Hello World”.find(“or”))7

print (“Hello World”.find(“Hello”))0

print (“Hello World”.find(“o”))4

Page 52: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Example

b = “HELLO”a = b.lower()print(b)print(a)

>>> b = "HELLO">>> a = b.lower()>>> print(b)HELLO>>> print(a)hello

Page 53: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

Variable StorageIn week 2 we had this

example. Note that b is a string. 10a

Sbally

a = 10b = “Sally”

Page 54: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

String StorageWe can see that strings are

really individual characters stored into different locations in memory, in order

There is another way to store values in memory in this way.

10aSbally

Page 55: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

The Range Function Revisited

Consider this example

10a0b1234

a = 10b = range(0,5)

Page 56: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

String StorageLike a string, we have a

single variable assigned to the start of multiple values in memory

10a0b1234Sca

a = 10b = range(0,5)c = “Sally”

….

Page 57: Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.

HomeworkRead 5.1, 5.2, 5.3, 5.4

Read 8.1 – 8.4 but skip 8.3.3