besides blocks: python session #1 -...

87
1 Besides Blocks: Python Session #1 (thanks to Glenn Sugden for the first version of these slides) is licensed under a Creative Com mons Attribution-NonCommercial-ShareAlike 3.0 Unported License . Instructor: Dan Garcia

Upload: vokien

Post on 14-Aug-2019

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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

Page 2: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

2

Outline

•  Computational Thinking (5)

•  Codification (10)

•  Python? (5)

•  Getting Started (10)

•  Why? (5)

•  Syntax (20)

•  Turtle (5)

Page 3: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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

Page 4: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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

Page 5: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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

Page 6: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

Props to edX and BB teams!

(see website)

6

Page 7: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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

Page 8: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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

Page 9: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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 10: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

•  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

Page 11: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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:

Page 12: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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

Page 13: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

13

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

at-home/

Beyond Blocks: Python #1 Installation: More Information

Page 14: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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/

Page 15: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

15

Snap! Python

Page 16: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

16

Snap! Python

Variables

Page 17: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

17

Snap! Python

Variables

Page 18: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

18

Snap! Python

Variables

Page 19: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

19

Snap! Python

Variables

Page 20: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

20

Snap! Python

Variables

NOTE:

Assignment doesn’t

“evaluate” to anything,

so nothing is printed!

Page 21: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

21

Snap! Python

Variables

Page 22: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

22

Snap! Python

Variables

Page 23: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

23

Snap! Python

Operators

Page 24: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

24

Snap! Python

Operators

Page 25: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

25

Snap! Python

Operators

•  Note the double =s!

•  = means assign, == means compare

•  Very common source of bugs!

Page 26: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

26

Snap! Python

Operators

Page 27: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

27

Snap! Python

Sidebar: Division (integer vs. real/float)

Page 28: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

28

Snap! Python

Sidebar: Exponent

Snap! has e and 10 ,

but Python can do any base & exponent!

x x

Page 29: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

29

Snap! Python

Sidebar: Exponent

What’s that “L?”

Page 30: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

30

Snap! Python

Sidebar: Exponent

Page 31: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

31

Snap! Python

Sidebar: Exponent

Just (for now) means:

“a really big integer.”

Page 32: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

32

Snap! Python

Operators

Page 33: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

33

Snap! Python

Conditionals

Page 34: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

34

Snap! Python

Conditionals

Page 35: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

35

Snap! Python

Conditionals

Notice the colon and

indentation syntax!

Page 36: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

36

Snap! Python

Conditionals

Notice the colon and

indentation syntax!

Page 37: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

37

Snap! Python

Conditionals

Page 38: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

38

Snap! Python

Conditionals

Page 39: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

39

Snap! Python

Loops

Page 40: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

40

Snap! Python

Loops

Page 41: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

41

Snap! Python

Loops

Note the indentation (again)!

Page 42: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

42

Snap! Python

Loops

Page 43: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

43

Snap! Python

Loops

Page 44: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

44

Snap! Python

More Loops

Page 45: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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 46: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

46

Snap! Python

Functions: Calling

•  Calling functions (the syntax) looks like this:

•  Equivalent to creating & running a Snap! block:

Page 47: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

47

•  Calling functions (the syntax) looks like this:

•  Equivalent to creating & running a Snap! block:

Snap! Python

Functions: Calling

Page 48: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

•  Calling functions (the syntax) looks like this:

•  Equivalent to creating & running a Snap! block:

Snap! Python

Functions: Calling

Page 49: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

Snap! Python

Functions: Calling

•  Calling functions (the syntax) looks like this:

•  Equivalent to creating & running a Snap! block:

Page 50: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

50

Snap! Python

Functions : Defining

Keyword: DEF

Page 51: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

51

Snap! Python

Functions : Defining

Name of the function

Page 52: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

52

Snap! Python

Functions : Defining

“Arguments,” or inputs to the function

Page 53: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

53

Snap! Python

Functions : Defining

Indentation: the key to “scope.”

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

Page 54: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

54

Snap! Python

Functions : Defining

pass: Python’s “placeholder” or NOP

NOP: short for “NO OPeration”

(or do nothing...)

Page 55: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

55

Functions must have a body!

Snap! Python

Functions : Defining

pass: Python’s “placeholder” or NOP

NOP: short for “NO OPeration”

Page 56: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

56

Snap! Python

Functions : Defining

Hitting Return/Enter (on an empty line)

“closes” (finishes) the definition.

Page 57: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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!

Page 58: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

58

Snap! Python

Functions : Returning Values

Page 59: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

59

Snap! Python

Functions : Returning Values

Page 60: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

60

Snap! Python

Functions : Returning Values

“return” and “report” are

equivalent!

Page 61: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

61

Snap! Python

Functions : Returning Values

What is the type of the variable ‘c’?

Page 62: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

62

Snap! Python

Functions : Type? It depends!

Page 63: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

63

Snap! Python

Functions : Type? It depends!

Page 64: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

64

Snap! Python

Functions : C’s type? It depends!

!

Page 65: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

65

Snap! Python

Functions : Practice

What will this print?

Page 66: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

66

Snap! Python

Functions : Practice

Page 67: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

67

Snap! Python

Functions : Recursion!

Page 68: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

68

Snap! Python

Functions : Recursion!

Page 69: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

69

Snap! Python

Functions : Recursion! Within Reason!

• • •

Page 70: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

70

Snap! Python

Importing

cosine(radians)

Page 71: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

71

Snap! Python

Importing

ERROR!

Hmmmm....

Page 72: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

72

Snap! Python

Importing

“math” module

Page 73: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

73

Beyond Blocks: Python #1 Importing

Page 74: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

74

Importing

module.function(args)

Beyond Blocks: Python #1

Page 75: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

75

Beyond Blocks: Python #1 Importing, help!

Page 76: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

76

Beyond Blocks: Python #1

module.function

Importing, help!

Page 77: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

77

Beyond Blocks: Python #1 Importing, help!

Page 78: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

import antigravity

78

Page 79: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

79

Help!

Beyond Blocks: Python #1

Page 80: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

80

Help!

Beyond Blocks: Python #1

• • •

Page 81: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

81

Help!

Python keyword

Beyond Blocks: Python #1

Page 82: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

82

Help!

Note the quotes!

Beyond Blocks: Python #1

Page 83: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

83

Help!

Beyond Blocks: Python #1

• • •

Page 84: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

84

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

Page 85: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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

Page 86: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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

Page 87: Besides Blocks: Python Session #1 - gamescrafters.berkeley.edugamescrafters.berkeley.edu/.../2014Sp-CS10-L21-DG-Besides-Blocks-I.pdf · 1 Besides Blocks: Python Session #1 (thanks

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