10
4/16/14 1 1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first version of these slides) is licensed under a Creative Com . Instructor: Dan Garcia 2 Outline Computational Thinking (5) Codification (10) Python? (5) Getting Started (10) Why? (5) Syntax (20) Turtle (5) The Goals of BJC BJC’s goal is not to teach you Snap! It’s to teach you critical thinking about societal implications of computing It’s also to teach you how to program (Snap! is the best intro language we know) and help you succeed in CS61A More importantly it’s to teach you how to think like a computer scientist in life, called “computational thinking” 3 Computational Thinking It’s using abstraction (removing detail and generalization with parameters) It’s understanding the value of a “spec” that specifies a contract It’s the iterative design cycle: design, prototype, implement, evaluate (loop) It’s thinking about how solutions scale, parallelize, generalize, and trying to foresee the unintended consequences! 4 Why Python? (1/2) Up until now, it’s just been Snap! There’s an advantage in just one language, there’s only one cognitive tax paid for “learning a new language” However, we want CS61A success too! The feeling is this will help, it’s in Python We also see the benefit of another tool, and learning when you’d use Snap! vs Python… It’s “Besides” blocks, not “Beyond” blocks! 5 Props to edX and BB teams! (see website) 6 Codification Jens Moenig wrote Snap! with design support from Brian Harvey. Here is the coolest program ever. tinyurl.com/BJCcodification Lesson: Snap! is Turing Complete, so anything they can do, we can do. 7 Why Python (2/2) Easy to learn and use, looks like pseudocode Minimal text-based syntax Easy to cut & paste, people type faster than drag Has a Turtle mode! Popular lots of online support Incredible libraries Makes you marketable 8 9 Learning Python 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 Hope is that “foreign” syntax becomes less intimidating and more approachable Plan: Lec, Lab, Dis, Lec, Lab

2014Sp-CS10-L21-DG-Besides-Blocks-Iinst.eecs.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I-9up.pdf1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first

  • Upload
    lediep

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 2014Sp-CS10-L21-DG-Besides-Blocks-Iinst.eecs.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I-9up.pdf1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first

4/16/14

1

1

Besides Blocks: Python

Session #1

(thanks to Glenn Sugden for the first version of these slides) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Instructor: Dan Garcia

2

Outline

•  Computational Thinking (5)

•  Codification (10)

•  Python? (5)

•  Getting Started (10)

•  Why? (5)

•  Syntax (20)

•  Turtle (5)

The Goals of BJC •  BJC’s goal is not to teach you Snap!

•  It’s to teach you critical thinking about societal

implications of computing

•  It’s also to teach you how to program (Snap!

is the best intro language we know) and help

you succeed in CS61A

•  More importantly it’s to teach you how to

think like a computer scientist in life, called

“computational thinking”

3

Computational Thinking •  It’s using abstraction (removing detail and

generalization with parameters)

•  It’s understanding the value of a “spec” that

specifies a contract

•  It’s the iterative design cycle: design,

prototype, implement, evaluate (loop)

•  It’s thinking about how solutions scale,

parallelize, generalize, and trying to foresee

the unintended consequences!

4

Why Python? (1/2) •  Up until now, it’s just been Snap!

•  There’s an advantage in just one language,

there’s only one cognitive tax paid for “learning

a new language”

•  However, we want CS61A success too!

•  The feeling is this will help, it’s in Python

•  We also see the benefit of another tool, and

learning when you’d use Snap! vs Python…

•  It’s “Besides” blocks, not “Beyond” blocks!

5

Props to edX and BB teams!

(see website)

6

Codification

•  Jens Moenig wrote Snap! with design support

from Brian Harvey.

•  Here is the coolest program ever.

tinyurl.com/BJCcodification!

•  Lesson: Snap! is Turing Complete, so

anything they can do, we can do. 7

Why Python (2/2) •  Easy to learn and use, looks like pseudocode

•  Minimal text-based syntax

•  Easy to cut & paste, people type faster than drag

•  Has a Turtle mode!

•  Popular

•  lots of online support

•  Incredible libraries

•  Makes you marketable

8 9

Learning Python •  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

•  Hope is that “foreign” syntax becomes less

intimidating and more approachable

•  Plan: Lec, Lab, Dis, Lec, Lab

Page 2: 2014Sp-CS10-L21-DG-Besides-Blocks-Iinst.eecs.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I-9up.pdf1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first

4/16/14

2

•  SPOC: “Small Private Online

Course”

•  Hybrid MOOC

•  Think of MOOC = ebook

•  Teacher signs up class, picks

parts they want

•  The forum discussions are self-

contained

•  Teacher gets analytics of only

their students

•  Teacher is in control

•  We’re going to trial edX with

the “Besides Blocks” labs

BJC Future: edX SPOC

11

Beyond Blocks: Python #1 Installation: Mac Check

•  Open Terminal

•  Type “python3” and hit return •  (without the quotes)

•  Type “print("hello world")” return •  print("hello world")

•  The result should be:

12

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

docs.python.org/3.4/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 #1 Installation: Windows Check

13

•  Computer Science Circles : Run Python at Home cemclinux1.math.uwaterloo.ca/~cscircles/wordpress/run-

at-home/

Beyond Blocks: Python #1 Installation: More Information

14

Beyond Blocks: Python #1 Installation: Version Check

unix% python –VPython 3.4.0

We’ll be talking about version 3.4.0 in here.

If curious, there’s more version info at: https://docs.python.org/3.4/whatsnew/

15

Snap! Python

16

Snap! Python

Variables

17

Snap! Python

Variables

18

Snap! Python

Variables

Page 3: 2014Sp-CS10-L21-DG-Besides-Blocks-Iinst.eecs.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I-9up.pdf1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first

4/16/14

3

19

Snap! Python

Variables

20

Snap! Python

Variables

NOTE:

Assignment doesn’t

“evaluate” to anything,

so nothing is printed!

21

Snap! Python

Variables

22

Snap! Python

Variables

23

Snap! Python

Operators

24

Snap! Python

Operators

25

Snap! Python

Operators

•  Note the double =s!

•  = means assign, == means compare

•  Very common source of bugs! 26

Snap! Python

Operators

27

Snap! Python

Sidebar: Division (integer vs. real/float)

Page 4: 2014Sp-CS10-L21-DG-Besides-Blocks-Iinst.eecs.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I-9up.pdf1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first

4/16/14

4

28

Snap! Python

Sidebar: Exponent

Snap! has e and 10 ,

but Python can do any base & exponent!

x x

29

Snap! Python

Sidebar: Exponent

What’s that “L?”

30

Snap! Python

Sidebar: Exponent

31

Snap! Python

Sidebar: Exponent

Just (for now) means:

“a really big integer.”

32

Snap! Python

Operators

33

Snap! Python

Conditionals

34

Snap! Python

Conditionals

35

Snap! Python

Conditionals

Notice the colon and

indentation syntax!

36

Snap! Python

Conditionals

Notice the colon and

indentation syntax!

Page 5: 2014Sp-CS10-L21-DG-Besides-Blocks-Iinst.eecs.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I-9up.pdf1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first

4/16/14

5

37

Snap! Python

Conditionals

38

Snap! Python

Conditionals

39

Snap! Python

Loops

40

Snap! Python

Loops

41

Snap! Python

Loops

Note the indentation (again)!

42

Snap! Python

Loops

43

Snap! Python

Loops

44

Snap! Python

More Loops

45

Snap! Python

More Loops

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

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

Page 6: 2014Sp-CS10-L21-DG-Besides-Blocks-Iinst.eecs.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I-9up.pdf1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first

4/16/14

6

46

Snap! Python

Functions: Calling

•  Calling functions (the syntax) looks like this:

•  Equivalent to creating & running a Snap! block:

47

•  Calling functions (the syntax) looks like this:

•  Equivalent to creating & running a Snap! block:

Snap! Python

Functions: Calling

•  Calling functions (the syntax) looks like this:

•  Equivalent to creating & running a Snap! block:

Snap! Python

Functions: Calling

Snap! Python

Functions: Calling

•  Calling functions (the syntax) looks like this:

•  Equivalent to creating & running a Snap! block:

50

Snap! Python

Functions : Defining

Keyword: DEF

51

Snap! Python

Functions : Defining

Name of the function

52

Snap! Python

Functions : Defining

“Arguments,” or inputs to the function

53

Snap! Python

Functions : Defining

Indentation: the key to “scope.”

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

Snap! Python

Functions : Defining

pass: Python’s “placeholder” or NOP

NOP: short for “NO OPeration”

(or do nothing...)

Page 7: 2014Sp-CS10-L21-DG-Besides-Blocks-Iinst.eecs.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I-9up.pdf1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first

4/16/14

7

55

Functions must have a body!

Snap! Python

Functions : Defining

pass: Python’s “placeholder” or NOP

NOP: short for “NO OPeration”

56

Snap! Python

Functions : Defining

Hitting Return/Enter (on an empty line)

“closes” (finishes) the definition.

57

Snap! Python

Sidebar: Keywords

•  Words reserved by Python

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

and del from not while!as elif global or with!assert else if pass yield!break except import print!class exec in raise!continue finally is return!def for lambda try!

58

Snap! Python

Functions : Returning Values

59

Snap! Python

Functions : Returning Values

60

Snap! Python

Functions : Returning Values

“return” and “report” are

equivalent!

61

Snap! Python

Functions : Returning Values

What is the type of the variable ‘c’?

62

Snap! Python

Functions : Type? It depends!

63

Snap! Python

Functions : Type? It depends!

Page 8: 2014Sp-CS10-L21-DG-Besides-Blocks-Iinst.eecs.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I-9up.pdf1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first

4/16/14

8

64

Snap! Python

Functions : C’s type? It depends!

!

65

Snap! Python

Functions : Practice

What will this print? 66

Snap! Python

Functions : Practice

67

Snap! Python

Functions : Recursion!

68

Snap! Python

Functions : Recursion!

69

Snap! Python

Functions : Recursion! Within Reason!

• • •

70

Snap! Python

Importing

cosine(radians)

71

Snap! Python

Importing

ERROR!

Hmmmm.... 72

Snap! Python

Importing

“math” module

Page 9: 2014Sp-CS10-L21-DG-Besides-Blocks-Iinst.eecs.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I-9up.pdf1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first

4/16/14

9

73

Beyond Blocks: Python #1 Importing

74

Importing

module.function(args)

Beyond Blocks: Python #1

75

Beyond Blocks: Python #1 Importing, help!

76

Beyond Blocks: Python #1

module.function

Importing, help!

77

Beyond Blocks: Python #1 Importing, help!

import antigravity

78

79

Help!

Beyond Blocks: Python #1

80

Help!

Beyond Blocks: Python #1

• • •

81

Help!

Python keyword

Beyond Blocks: Python #1

Page 10: 2014Sp-CS10-L21-DG-Besides-Blocks-Iinst.eecs.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I-9up.pdf1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first

4/16/14

10

82

Help!

Note the quotes!

Beyond Blocks: Python #1

83

Help!

Beyond Blocks: Python #1

• • •

84

Beyond Blocks: Python #1 Sidebar: “sys” module

Turtle Module from turtle import *!color('red', 'yellow')!begin_fill()!while True:! forward(200)! left(170)! if abs(pos()) < 1:! break!end_fill()!done()!

85 86

More Information

• Python.org: www.python.org

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

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

Beyond Blocks: Python #1

87

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