python tricks cmsc 201. overview today we are learning some new tricks to make our lives easier!...

41
Python Tricks CMSC 201

Upload: branden-crawford

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Python Tricks

CMSC 201

Page 2: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

OverviewToday we are learning some new tricks to make our lives easier!

• Slicing and other tricks• Multiple return values• Global variables• Break and continue• Mutable and Immutable types• Try / Except

Page 3: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

List Tricks

Page 4: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Negative IndexIf you want to get the last element of a list or string, you can use negative indices!

myString = “abcdef”print(myString[-1])print(myString[-2])

Prints:

fe

Page 5: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

SlicingSay we have the string “My name is Max” and know, for some reason, that we want only the characters from index 3 to index 7.

myString = “My name is Max”slice = myString[3:7]

This is called string slicing! This will return the characters in myString from character 3 (inclusive) to 7 (exclusive). In this case, it prints the string “name”.

Page 6: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

SlicingIf we leave off a number, it just goes to the end or beginning of the string!

myString = “abcdef”

print(myString[3:])

print(myString[:5])

Prints:

‘def’‘abcde’

Page 7: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

SlicingWe can also control our step size.

myString = “abcdef”

print(myString[0:6:2])

Prints:

‘ace’

Page 8: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

ExerciseWhat do the following print?

myString = “abcdef”

print(myString[:2])print(myString[1:3])print(myString[4:])print(myString[::2])

Page 9: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

ExerciseWhat do the following print?

myString = “abcdef”

print(myString[:2])print(myString[1:3])print(myString[4:])print(myString[::2])

‘ab’‘bc’‘ef’‘ace’

Page 10: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

List SlicingThis works for lists too!

myList = [1, 2, 3, 4, 5]

print(myList[3:])

Prints:

[4, 5]

Page 11: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Copying A ListWant a list full of zeroes?

myList = [0] * 3

print(myList)

[0, 0, 0]

Multiplying a list by something gives you that list, that many times!

print([1, 2, 3] * 2)

Prints:

[1, 2, 3, 1, 2, 3]

Page 12: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

DangerRemember, lists are stored as references, so using the * operator on lists of lists is extremely dangerous!

myList = [1, [2, 3]] * 2

print(myList)

Prints:

[1, [2, 3], 1, [2, 3]]

Page 13: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

DangermyList = [1, [2, 3]] * 2

print(myList)

Prints:

[1, [2, 3], 1, [2, 3]]

However, here’s what’s happening: [ , , , ]

1[2, 3]

Page 14: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Multiple Return Values

Page 15: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Multiple Return ValuesFunctions in python can return multiple things!

def myFunction(arg1, arg2):# Some stuff happens herereturn var1, var2, var3

def main():result1, result2, result3 = myFunction(1, 2)

Page 16: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Global Variables

Page 17: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Global VariablesEarlier in class we mentioned you can have global constants!

PI = 3.14

def area(radius):return PI * (radius ** 2)

These variables are visible anywhere in the program.

Page 18: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Global VariablesWhat happens here?

PI = 3.14

def area(radius):return PI * (radius ** 2)

def main():PI = 10print(PI)

This makes a new variable, also called PI, that lives in main. The original PI is unchanged. The new PI overshadows the old one.

Page 19: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Global Variables

PI = 3.14

def area(radius):print(PI)return PI * (radius ** 2)

def main():PI = 10print(PI)area(1)

Prints:103.14

Page 20: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Global VariablesIf you want to change a global variable (rather than making a new one), you must declare it as global.

PI = 3.14

def area(radius):print(PI)return PI * (radius ** 2)

def main():global PIPI = 10area(1)print(PI)

Page 21: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Global VariablesPI = 3.14

def area(radius):print(PI)return PI * (radius ** 2)

def main():global PIPI = 10area(1)print(PI)

Prints:

1010

Page 22: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Global Variables

Summary:

Global variables are visible anywhere in the program, but to change them, you have to put the global keyword in front of them first.

In this class, you should never need to do this.

Page 23: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Break and Continue

Page 24: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

BreakSometimes, we want to get out of a loop before the termination condition.

Break immediately stops the loop!

Page 25: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Break

for i in range(0, 100):if i == 3:

breakprint(i)

print(“I have escaped the loop!”)

Prints:

012I have escaped the loop.

Page 26: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

BreakBreak breaks out of whatever the innermost loop.

for j in range(0, 10):for i in range(0, 10):

if i == 3:break

print(i)

The break statement will stop the inner loop over and over, but not the outer loop.

Page 27: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

ContinueContinue also changes how the loop works.

When a loop hits a continue statement, it immediately stops what it’s doing and goes back up to the next iteration of the loop.

for i in range(0, 5):if i == 3:

continueprint(i)

.

Page 28: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

ContinueWhen a loop hits a continue statement, it immediately stops what it’s doing and goes back up to the next iteration of the loop.

for i in range(0, 5):if i == 3:

continueprint(i)

Prints:0124

Page 29: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Break and ContinueUsing break and continue correctly can be a bit complicated, so for this semester we ask that you not put them in your code!

Page 30: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Mutable vs. Immutable

Page 31: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Mutable vs. ImmutableThese are important vocab terms—a variable that is immutable can’t be changed without being reassigned (that is, you can’t change it without using the = operator).

a = 10a = 11

A mutable variable can have parts of it changed without being reassigned:

mylist = [1, 2, 3]myList[0] = 2

Notice that we only changed a part of it.

Page 32: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Mutable vs. Immutable

Mutable variables:

• Lists

Immutable variables:

• Float• String• Integer• Boolean

Page 33: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

ReferencesThe way python stores information, each variable “points” to whatever value is in it. So when you say

a = 5

print(a)

Python has somewhere in the system that a variable called a points to 5.

a 5

Page 34: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Referencesa = 5

print(a)

So after setting a to 5, any time python goes to look up a, it finds 5.

a 5

If you reassign a, python changes what a points at. If you make two variables equal to 5, the both point at 5.

Page 35: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

ReassignmentRemember this example?

a = 5b = aa = a + 1

When we change a, b doesn’t change, because there’s no connection to a. b points at the value 5, and so does a.

Since you can’t actually change 5, it doesn’t matter that they are pointing to the same thing.

a 5b a 6

Page 36: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

Reassignment in FunctionsFunctions work the same way.

def someFunc(myVar):myVar = 10

def main():a = 5someFunc(a)print(a)

In this example, a just keeps pointing at 5

Notice that myVar and a both point to the number 5, there is just no way to change 5.

a 5

myVar myVar 10

Page 37: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

ListsLists are different, because the thing they are pointing at is capable of changing!

a = [1, 2, 3]

b = a

a[1] = 10

print(b)

Prints:[1, 10, 3]

a [1, 2, 3]

b

a [1, 10, 3]

b

Page 38: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

ListsWhat do we think this does?

def main():a = [1, 2, 3]myFunc(a)print(a)

def myFunc(myList):myList[1] = 100

Page 39: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

ListsWhat do we think this does?

def main():a = [1, 2, 3]myFunc(a)print(a)

def myFunc(myList):myList[1] = 100

Prints [1, 100, 3]

a [1, 2, 3]

myList

Becomes:

a [1, 100, 3]

myList

Page 40: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

ListsWhat about this?

def main():a = [1, 2, 3]myFunc(a)print(a)

def myFunc(myList):myList = [100]

Page 41: Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global

ListsThe = operator changes what we’re pointing at.

def main():a = [1, 2, 3]myFunc(a)print(a)

def myFunc(myList):myList = [100]

Prints [1, 2, 3]

a [1, 2, 3]

myList

a [1, 2, 3]

myList [100]