beyond blocks: python session #1cs10/sp13/bb/python... · beyond blocks: python session #1 beyond...

63
Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License . CS10 Spring 2013 Thursday, April 30, 2013 Michael Ball 1 1 Thursday, May 9, 13

Upload: others

Post on 29-May-2020

54 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

Beyond Blocks: PythonSession #1

Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

CS10 Spring 2013Thursday, April 30, 2013

Michael Ball

1

1Thursday, May 9, 13

Page 2: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

Goals

• Quick introduction to Python

• Not a tutorial or “how to”

• Hope is that you’ll want to learn (more)

• Advantages over higher level languages

• Challenges of programming syntax

• It’s really like “writing BYOB on Paper”!

2

2Thursday, May 9, 13

Page 3: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

Beyond Blocks: Python #1Installation: Mac Check

• Open Terminal

• Type python3 and hit return

• Type print(“hello world”) and hit return

• The result should be:

3

3Thursday, May 9, 13

Page 4: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

• Get Python to "print" something with these instructions:

http://docs.python.org/faq/windows.html

(You only have to get to the "Many people use the interactive mode as a convenient yet highly programmable calculator" paragraph)

Beyond Blocks: Python #1Installation: Windows Check

4

4Thursday, May 9, 13

Page 5: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

• Computer Science Circles : Run Python at Home

cemclinux1.math.uwaterloo.ca/~cscircles/wordpress/run-at-home/

Beyond Blocks: Python #1Installation: More Information

5

5Thursday, May 9, 13

Page 6: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

Beyond Blocks: Python #1Installation: Version Check

Michael> python3 -VPython 3.3.4We’ll be talking about version 3.3.x here,

although version 2.7.x (which is more common) works just as well!

If curious, there’s more version info at:http://docs.python.org/whatsnew/index.html

6

6Thursday, May 9, 13

Page 7: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

<demo>

Beyond Blocks: Python #1Why used “text based” programming?

7

7Thursday, May 9, 13

Page 8: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB Python

8

8Thursday, May 9, 13

Page 9: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonVariables

9

9Thursday, May 9, 13

Page 10: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonVariables

10

10Thursday, May 9, 13

Page 11: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonVariables

NOTE:Assignment doesn’t

“evaluate” to anything, so nothing is printed!

11

11Thursday, May 9, 13

Page 12: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonVariables

12

12Thursday, May 9, 13

Page 13: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonVariables

print(var)

NOTE:Printing is one of the big differences between

Python 2 and 3. Python 3 requires () with print!For the sake of humanity, just use print()! :)

13

13Thursday, May 9, 13

Page 14: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonOperators

14

14Thursday, May 9, 13

Page 15: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonTypes

• Everything in Python has an internal “type”

• Types are determined dynamically

• x = 1

• x now has the type “int”:

• (short for “integer”)

We’ll talk about this “script” (or function) later...15

15Thursday, May 9, 13

Page 16: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

• ‘bool’ is short for boolean

• ‘bool’s can have two values:

• True

• False

BYOB PythonTypes: bool

16

16Thursday, May 9, 13

Page 17: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

• ‘bool’ is short for boolean

• ‘bool’s can have two values:

• True

• False

BYOB PythonTypes: bool

NOTE: Upper case is important!

>>> type(True)<class 'bool'>

>>> TrueTrue>>> FalseFalse

17

17Thursday, May 9, 13

Page 18: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonTypes: function type()

This function returns the type that Python has

assigned the identifier.

>>> type(True)<class 'bool'>>>> type(1)<class 'int'>>>> type(1.0)<class 'float'>>>> type("Hello, there!")<class 'str'>>>> type("1")<class 'str'>

18

18Thursday, May 9, 13

Page 19: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonOperators

19

19Thursday, May 9, 13

Page 20: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonOperators

20

20Thursday, May 9, 13

Page 21: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonOperators

• Note the double =s!

• = means assign, == means compare

• Very common source of bugs!21

21Thursday, May 9, 13

Page 22: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonOperators

= means assign,

== means compare

22

22Thursday, May 9, 13

Page 23: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonOperators

= means assign,

== means compare

23

23Thursday, May 9, 13

Page 24: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonOperators

24

24Thursday, May 9, 13

Page 25: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonSidebar: Division (integer vs. real/float)

Python 2 Python 3

>>> 5/60.8333333333333334>>> 5.0/6.00.8333333333333334>>> 5.0//6.00.0

>>> 5/60>>> 5.0/6.00.8333333333333334>>> 5.0//6.00.0

25Thursday, May 9, 13

Page 26: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonSidebar: Division (integer vs. real/float)

“Force” integer division

>>> 5/60.8333333333333334>>> 5.0/6.00.8333333333333334>>> 5.0//6.00.0

26Thursday, May 9, 13

Page 27: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonSidebar: Exponent

BYOB has e and 10 ,but Python can do any base & exponent!

x x

>>> 2**8256>>> 2**101024>>> 2**1001267650600228229401496703205376

27Thursday, May 9, 13

Page 28: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonConditionals

28

28Thursday, May 9, 13

Page 29: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonConditionals

29Thursday, May 9, 13

Page 30: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonConditionals

Notice the colon and indentation syntax!

30Thursday, May 9, 13

Page 31: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonConditionals

Notice the colon and indentation syntax!

31Thursday, May 9, 13

Page 32: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonConditionals

32

32Thursday, May 9, 13

Page 33: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonConditionals

33Thursday, May 9, 13

Page 34: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonLoops

34

34Thursday, May 9, 13

Page 35: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonLoops

35Thursday, May 9, 13

Page 36: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonLoops

Note the indentation (again)!

36Thursday, May 9, 13

Page 37: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonLoops

37

37Thursday, May 9, 13

Page 38: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonLoops

38Thursday, May 9, 13

Page 39: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonMore Loops

39

39Thursday, May 9, 13

Page 40: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonMoar [sic] Loops

There isn’t really an exact equivalent of this in Python...

We’ll talk more about this in Session #2...

40Thursday, May 9, 13

Page 41: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions: Calling

• Calling functions (the syntax) looks like this:

• Equivalent to creating & running a BYOB block:

41

41Thursday, May 9, 13

Page 42: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

• Calling functions (the syntax) looks like this:

• Equivalent to creating & running a BYOB block:

BYOB PythonFunctions: Calling

42Thursday, May 9, 13

Page 43: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

• Calling functions (the syntax) looks like this:

• Equivalent to creating & running a BYOB block:

BYOB PythonFunctions: Calling

43Thursday, May 9, 13

Page 44: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions: Calling

• Calling functions (the syntax) looks like this:

• Equivalent to creating & running a BYOB block:

44Thursday, May 9, 13

Page 45: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Defining

Keyword: DEF

45

45Thursday, May 9, 13

Page 46: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Defining

Name of the function

46

46Thursday, May 9, 13

Page 47: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Defining

“Arguments,” or inputs to the function

47

47Thursday, May 9, 13

Page 48: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Defining

Indentation: the key to Python and “scope.”!In Python: Indentation matters!!

We’ll talk about “scope” later...48

48Thursday, May 9, 13

Page 49: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Defining

pass: Python’s “placeholder”Skip this, and do nothing.

49

49Thursday, May 9, 13

Page 50: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Defining

pass: Python’s “placeholder”Skip this, and do nothing.

Functions must have a body!49

49Thursday, May 9, 13

Page 51: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Defining

50Thursday, May 9, 13

Page 52: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Defining

Hitting Return/Enter (on an empty line)“closes” (finishes) the definition.

51

51Thursday, May 9, 13

Page 53: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonSidebar: Keywords

• Words reserved by Python

• List at: docs.python.org/reference/lexical_analysis.html

and del from not whileas elif global or withassert else if pass yieldbreak except import class exec in raisecontinue finally is returndef for lambda try

52

52Thursday, May 9, 13

Page 54: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonBuilt In Functions

• http://docs.python.org/3.3/library/functions.html

abs()dict()help()min()setattr()all()dir()hex()next()slice()any()divmod()id()object()sorted()ascii()enumerate()input()

oct()staticmethod()bin()eval()int()open()str()bool()exec()isinstance()ord()sum()bytearray()filter()issubclass()pow()super()bytes()

float()iter()print()tuple()callable()format()len()property()type()chr()frozenset()list()range()vars()classmethod()getattr()locals()repr()

zip()compile()globals()map()reversed()__import__()complex()hasattr()max()round()delattr()hash()memoryview()set() 

53

53Thursday, May 9, 13

Page 55: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonUSEFUL: Built In Functions

• http://docs.python.org/3.3/library/functions.html

abs()help()min()max()print()range()

type()str()chr()ord()bool()float()int()

map()filter()sum()

open()

iter()len()

tuple()list()dict()set()

54

54Thursday, May 9, 13

Page 56: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Returning Values

55

55Thursday, May 9, 13

Page 57: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Returning Values

56Thursday, May 9, 13

Page 58: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Returning Values

“return” and “report” are equivalent!

57Thursday, May 9, 13

Page 59: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Recursion!

58

58Thursday, May 9, 13

Page 60: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Recursion!

59

59Thursday, May 9, 13

Page 61: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

BYOB PythonFunctions : Recursion! Within Reason!

•••

60Thursday, May 9, 13

Page 62: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

More Information

•Python.org: www.python.org

•Python Docs: www.python.org/doc/

•Python Modules: docs.python.org/modindex.html

•CodeAcademy: codeacademy.com

•Online PythonTutor: http://www.pythontutor.com

Beyond Blocks: Python #1

61

61Thursday, May 9, 13

Page 63: Beyond Blocks: Python Session #1cs10/sp13/bb/Python... · Beyond Blocks: Python Session #1 Beyond Blocks : Python : Session #1 by Michael Ball adapted from Glenn Sugden is licensed

More Information

•Computer Science Circles: Python

cemclinux1.math.uwaterloo.ca/~cscircles/wordpress/using-this-website/

•Dive Into Python: diveintopython.org/toc/

•Cal’s Self-Paced Center:

inst.eecs.berkeley.edu/~selfpace/class/cs9h/How to Think Like a Computer Scientist (Python Version)

www.greenteapress.com/thinkpython/thinkCSpy/html/

Beyond Blocks: Python #1

62

62Thursday, May 9, 13