beyond blocks: python session #1cs10/sp13/bb/python... · beyond blocks: python session #1 beyond...
TRANSCRIPT
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
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
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
• 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
• 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
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
<demo>
Beyond Blocks: Python #1Why used “text based” programming?
7
7Thursday, May 9, 13
BYOB Python
8
8Thursday, May 9, 13
BYOB PythonVariables
9
9Thursday, May 9, 13
BYOB PythonVariables
10
10Thursday, May 9, 13
BYOB PythonVariables
NOTE:Assignment doesn’t
“evaluate” to anything, so nothing is printed!
11
11Thursday, May 9, 13
BYOB PythonVariables
12
12Thursday, May 9, 13
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
BYOB PythonOperators
14
14Thursday, May 9, 13
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
• ‘bool’ is short for boolean
• ‘bool’s can have two values:
• True
• False
BYOB PythonTypes: bool
16
16Thursday, May 9, 13
• ‘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
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
BYOB PythonOperators
19
19Thursday, May 9, 13
BYOB PythonOperators
20
20Thursday, May 9, 13
BYOB PythonOperators
• Note the double =s!
• = means assign, == means compare
• Very common source of bugs!21
21Thursday, May 9, 13
BYOB PythonOperators
= means assign,
== means compare
22
22Thursday, May 9, 13
BYOB PythonOperators
= means assign,
== means compare
23
23Thursday, May 9, 13
BYOB PythonOperators
24
24Thursday, May 9, 13
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
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
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
BYOB PythonConditionals
28
28Thursday, May 9, 13
BYOB PythonConditionals
29Thursday, May 9, 13
BYOB PythonConditionals
Notice the colon and indentation syntax!
30Thursday, May 9, 13
BYOB PythonConditionals
Notice the colon and indentation syntax!
31Thursday, May 9, 13
BYOB PythonConditionals
32
32Thursday, May 9, 13
BYOB PythonConditionals
33Thursday, May 9, 13
BYOB PythonLoops
34
34Thursday, May 9, 13
BYOB PythonLoops
35Thursday, May 9, 13
BYOB PythonLoops
Note the indentation (again)!
36Thursday, May 9, 13
BYOB PythonLoops
37
37Thursday, May 9, 13
BYOB PythonLoops
38Thursday, May 9, 13
BYOB PythonMore Loops
39
39Thursday, May 9, 13
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
BYOB PythonFunctions: Calling
• Calling functions (the syntax) looks like this:
• Equivalent to creating & running a BYOB block:
41
41Thursday, May 9, 13
• Calling functions (the syntax) looks like this:
• Equivalent to creating & running a BYOB block:
BYOB PythonFunctions: Calling
42Thursday, May 9, 13
• Calling functions (the syntax) looks like this:
• Equivalent to creating & running a BYOB block:
BYOB PythonFunctions: Calling
43Thursday, May 9, 13
BYOB PythonFunctions: Calling
• Calling functions (the syntax) looks like this:
• Equivalent to creating & running a BYOB block:
44Thursday, May 9, 13
BYOB PythonFunctions : Defining
Keyword: DEF
45
45Thursday, May 9, 13
BYOB PythonFunctions : Defining
Name of the function
46
46Thursday, May 9, 13
BYOB PythonFunctions : Defining
“Arguments,” or inputs to the function
47
47Thursday, May 9, 13
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
BYOB PythonFunctions : Defining
pass: Python’s “placeholder”Skip this, and do nothing.
49
49Thursday, May 9, 13
BYOB PythonFunctions : Defining
pass: Python’s “placeholder”Skip this, and do nothing.
Functions must have a body!49
49Thursday, May 9, 13
BYOB PythonFunctions : Defining
50Thursday, May 9, 13
BYOB PythonFunctions : Defining
Hitting Return/Enter (on an empty line)“closes” (finishes) the definition.
51
51Thursday, May 9, 13
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
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
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
BYOB PythonFunctions : Returning Values
55
55Thursday, May 9, 13
BYOB PythonFunctions : Returning Values
56Thursday, May 9, 13
BYOB PythonFunctions : Returning Values
“return” and “report” are equivalent!
57Thursday, May 9, 13
BYOB PythonFunctions : Recursion!
58
58Thursday, May 9, 13
BYOB PythonFunctions : Recursion!
59
59Thursday, May 9, 13
BYOB PythonFunctions : Recursion! Within Reason!
•••
60Thursday, May 9, 13
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
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