Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming: Module #2 Python, Trinket, and Turtle Graphics Lois Delcambre.

Similar presentations


Presentation on theme: "Introduction to Programming: Module #2 Python, Trinket, and Turtle Graphics Lois Delcambre."— Presentation transcript:

1 Introduction to Programming: Module #2 Python, Trinket, and Turtle Graphics
Lois Delcambre

2 First … a comment about blockly
Most programming languages – including those used by professionals – have the following constructs: function definition repeat loop – repeat code a specific number of times repeat until – repeat code until some condition is True if statement – to ask questions else statement – second part of an if statement You are already a programmer!

3 Plan for Today Introduce Python at trinket.io
Introduce the turtle module Define functions in Python Compare blockly and Python Write your own Python programs. Write your own trinkets.

4 trinket – see next few slides for images of these steps
go to trinket.io click on “Learn” tab – at the top right click on “Start Learning” for Python with Turtles (in Free Lessons) click on “Meet Tina” (one lesson; the lessons are listed on the left side)

5 click on “Learn” tab – at the top right
go to trinket.io click on “Learn” tab – at the top right

6 Click on Start Learning!

7 To see the Table of Contents, click here:

8 With Table of Contents expanded, you can see the lessons here:

9 We’ll start with “Meet Tina”

10 A Python program

11 The button to push – to run the program

12 The output from this program

13 If you want to reset the program in a lesson …
Click on this menu Then choose Reset Sometimes, you need to click on another lesson and then back to this one after you reset.

14 Plan for Today Introduce Python at trinket.io
Introduce the turtle module Define functions in Python Compare blockly and Python Write your own Python programs. Write your own trinkets.

15 Meet Tina program import turtle tina = turtle.Turtle()
tina.shape('turtle') tina.penup() tina.forward(20) tina.write("Why, hello there!") tina.backward(20)

16 Meet Tina program – explained (1)
import allows you to use all of the functions and methods written in the module. Here we import the turtle module. import turtle tina = turtle.Turtle() tina.shape('turtle') tina.penup() tina.forward(20) tina.write("Why, hello there!") tina.backward(20)

17 Meet Tina program – explained (2)
the Turtle method in the turtle module creates a new turtle. This new turtle becomes associated with the name (that the programmer chose) tina. import turtle tina = turtle.Turtle() tina.shape('turtle') tina.penup() tina.forward(20) tina.write("Why, hello there!") tina.backward(20)

18 Meet Tina program – explained (3)
To use a method in the turtle module: name of the module (turtle) followed by a dot (.) and then the method name. (Turtle). import turtle tina = turtle.Turtle() tina.shape('turtle') tina.penup() tina.forward(20) tina.write("Why, hello there!") tina.backward(20)

19 Meet Tina program – explained (4)
For a turtle object (named tina), we can invoke methods (from the turtle module). Use: the name of the turtle (tina) followed by a dot (.) and the method name (shape, penup, forward, write, backward). import turtle tina = turtle.Turtle() tina.shape('turtle') tina.penup() tina.forward(20) tina.write("Why, hello there!") tina.backward(20)

20 Parameters for functions/methods
Sometimes (in real life), we don’t need parameters: “I’ll take the blue plate special.” That’s it. A nice lunch will be delivered. Sometimes (in real life), we supply parameters: “The cheeseburger meal, please.” What kind of cheese? Swiss What size french fries? medium What size soft drink? large “Swiss”, “medium”, and “large” are given to the chef – so they can prepare your lunch.

21 Parameters for functions/methods Some functions/methods need them; some don’t
the forward method has one parameter – how far to go forward. the penup method has no parameters. It simply puts the pen up. import turtle tina = turtle.Turtle() tina.shape('turtle') tina.penup() tina.forward(20) tina.write("Why, hello there!") tina.backward(20)

22 Meet Tina program – explained (5)
Every time you invoke a function or method, you put the name of the function/method and then you put the parameters to that function/method in parentheses. If you have no parameters, you put open/close parentheses – with nothing in between. import turtle tina = turtle.Turtle() tina.shape('turtle') tina.penup() tina.forward(20) tina.write("Why, hello there!") tina.backward(20)

23 Coordinate space for turtle turtle has a “heading” – starts out East (to the right), as shown
200 -200

24 Class work On your own the Moving lesson

25 Saying Hello program explained (1)
input is a function that is part of Python. Every program can use it. See: no “tina” in front of the name of the function. We just put the name of the function and then the parameter(s). import turtle tina = turtle.Turtle() tina.shape('turtle') your_name = input("What's your name?") tina.penup() tina.forward(20) tina.write("Why, hello, " + your_name + "!") tina.backward(20)

26 functions some functions are built-in to Python
input: allow the user to enter text, abs: returns the absolute value of the parameter some functions are provided in modules math module: gcd, factorial, cos, sin, tan, … functions often have one or more parameters functions typically return some sort of value input – hands over the text typed by the user gcd – hands over the number that is the greatest common divisor of the two parameters

27 functions vs. methods In Python and in existing modules, some things are defined as functions; others are defined as methods. The only difference is that a method needs to be applied to an object (of the right type). The object is kind of like an “automatic” parameter.

28 methods tina is a turtle object in the Meet Tina program, for example
thus, we can invoke all turtle methods for tina tina.forward tina.penup etc.

29 Saying Hello program explained (2)
The parameter to the input function is the text that is displayed to the user. This program will then wait for the user to type in their response (and hit enter). import turtle tina = turtle.Turtle() tina.shape('turtle') your_name = input("What's your name?") tina.penup() tina.forward(20) tina.write("Why, hello, " + your_name + "!") tina.backward(20)

30 Saying Hello program explained (3)
Whenever you have some text in your program, you have to put quotes around it. In Python, single quotes (‘word’) or double quotes (“word”) will work. import turtle tina = turtle.Turtle() tina.shape('turtle') your_name = input("What's your name?") tina.penup() tina.forward(20) tina.write("Why, hello, " + your_name + "!") tina.backward(20)

31 Saying Hello program explained (4)
What does the equal sign do? import turtle tina = turtle.Turtle() tina.shape('turtle') your_name = input("What's your name?") tina.penup() tina.forward(20) tina.write("Why, hello, " + your_name + "!") tina.backward(20)

32 Assignment statements in Python (1)
tina = turtle.Turtle() This is an assignment statement. tina is a variable name, chosen by the programmer. The variable size “gets” or becomes associated with whatever is returned by the right side of the assignment statement. In this case, the variable tina gets a turtle object because the Turtle method in Python returns a turtle object.

33 Assignment statements in Python (2)
An assignment statement introduces a new name (if it’s the first time you’ve used that name). Here, the variable name (tina) gets a new turtle object! import turtle tina = turtle.Turtle() tina.shape('turtle') your_name = input("What's your name?") tina.penup() tina.forward(20) tina.write("Why, hello, " + your_name + "!") tina.backward(20)

34 Assignment statements in Python (3)
Once you have a name (with its value), you can use that name (again and again) to refer to that value. Here, we invoke a number of methods that are appropriate for a turtle object using the name tina – a turtle object. import turtle tina = turtle.Turtle() tina.shape('turtle') your_name = input("What's your name?") tina.penup() tina.forward(20) tina.write("Why, hello, " + your_name + "!") tina.backward(20)

35 Assignment statements in Python (4)
Here, the variable (your_name) gets the value of whatever is returned from the input function. The input function will return whatever the user types in as a string. import turtle tina = turtle.Turtle() tina.shape('turtle') your_name = input("What's your name?") tina.penup() tina.forward(20) tina.write("Why, hello, " + your_name + "!") tina.backward(20)

36 Assignment statements in Python (5)
Now that the variable (your_name) has a value, we can use that variable whenever we want to used the value. import turtle tina = turtle.Turtle() tina.shape('turtle') your_name = input("What's your name?") tina.penup() tina.forward(20) tina.write("Why, hello, " + your_name + "!") tina.backward(20)

37 Saying Hello program explained (5)
The plus symbol (+) is used to concatenate strings (text). Here – the write method (invoked for the turtle object with the name tina) will write out Why, hello, <value of your_name variable>! import turtle tina = turtle.Turtle() tina.shape('turtle') your_name = input("What's your name?") tina.penup() tina.forward(20) tina.write("Why, hello, " + your_name + "!") tina.backward(20)

38 Class work On your own: Color lesson Tina’s Pen lesson (Draw 2 parallel sides) Tina’s grid lesson (Draw box at bottom left of screen) Going in Circles lesson We’ll cover Repeating with Loops and Lists tomorrow Skip: Lists of numbers, Loops of Lists, Changing Colors lessons Turtles are Objects lesson Tina and Tommy’s Colors lesson

39 Plan for Today Introduce Python at trinket.io
Introduce the turtle module Define functions in Python Compare blockly and Python Write your own Python programs. Write your own trinkets.

40 the Functions are recipes! lesson – explained (1)
import turtle tina=turtle.Turtle() tina.shape('turtle') tina.color('purple') def triangle(): tina.left(60) tina.forward(30) tina.left(120) triangle()

41 the Functions are recipes
the Functions are recipes! lesson – explained (2) def to define a function import turtle tina=turtle.Turtle() tina.shape('turtle') tina.color('purple') def triangle(): tina.left(60) tina.forward(30) tina.left(120) triangle() Function definition here. The name of the function here.

42 the Functions are recipes
the Functions are recipes! lesson – explained (3) def to define a function import turtle tina=turtle.Turtle() tina.shape('turtle') tina.color('purple') def triangle(): tina.left(60) tina.forward(30) tina.left(120) triangle() Function definition here. The Python interpreter processes this function definition; remembers the name of the function; it will run the code inside the function if the function is ever invoked.

43 the Functions are recipes! lesson – explained (4) function invocation
import turtle tina=turtle.Turtle() tina.shape('turtle') tina.color('purple') def triangle(): tina.left(60) tina.forward(30) tina.left(120) triangle() Function definition here. The Python interpreter processes this function definition; remembers the name of the function; it will run the code inside the function if the function is ever invoked. Function invocation here. Invokes the function named triangle.

44 the Functions are recipes
the Functions are recipes! lesson – explained (5) this function (triangle) doesn’t have any parameters import turtle tina=turtle.Turtle() tina.shape('turtle') tina.color('purple') def triangle(): tina.left(60) tina.forward(30) tina.left(120) triangle() No parameters listed here in the function definition. No parameter values provided here when the function is invoked.

45 the Functions are recipes
the Functions are recipes! lesson – explained (6) the code block inside a function definition import turtle tina=turtle.Turtle() tina.shape('turtle') tina.color('purple') def triangle(): tina.left(60) tina.forward(30) tina.left(120) triangle() Code block begins with a : on the previous line Code inside the code block must be indented The code block ends when code is unindented. The unindented line is NOT part of the code block in this function.

46 Plan for Today Introduce Python at trinket.io
Introduce the turtle module Define functions in Python Compare blockly and Python Write your own Python programs. Write your own trinkets.

47 blockly vs. Python Moving program
They both have one command after the other. You can have blank lines in Python – anywhere.

48 blockly vs. Python turtle module
Moving program “move forward” repeated in program (zombie takes one step at a time) “forward” method in the turtle module takes one parameter: the number of pixels to move forward. There is also a “backward” method.

49 blockly vs. Python turtle module (cont.)
Moving program “right” method in the turtle module takes one parameter: the number of degrees to turn. There is also a “left” method. Parameter can be negative/positive. “turn” can turn left or right

50 blockly vs. Python function definition import turtle
tina=turtle.Turtle() tina.shape('turtle') tina.color('purple') def triangle(): tina.left(60) tina.forward(30) tina.left(120) triangle() function definition Functions are Recipes! program

51 blockly vs. Python function name import turtle tina=turtle.Turtle()
tina.shape('turtle') tina.color('purple') def triangle(): tina.left(60) tina.forward(30) tina.left(120) triangle() function name Functions are Recipes! program

52 blockly vs. Python function invocation import turtle
tina=turtle.Turtle() tina.shape('turtle') tina.color('purple') def triangle(): tina.left(60) tina.forward(30) tina.left(120) triangle() function invocation Functions are Recipes! program

53 Class Work Work on your own: Functions are recipes! lesson
Make 5 cakes lesson

54 Plan for Today Introduce Python at trinket.io
Introduce the turtle module Define functions in Python Compare blockly and Python Write your own Python programs. Write your own trinkets.

55 punctuation Can you read this? Mary is my sister she’s taller than me she. . has blonde hair she’s younger than I am

56 punctuation (cont.) Mary is my sister she’s taller than me she. . has blonde hair she’s younger than I am A computer would have trouble. It uses rules to decide where commands end. It would find these “sentences”: Mary is my sister she’s taller than me she. a sentence . empty sentence has blonde hair she’s younger than I am not (yet) a sentence because we haven’t encountered a period

57 In Python, some punctuation must be precise (try this)
All statements lined up on left margin (except for block – which is indented). block begins with a : then all statements indented – the same amount. block ends with unindented line.

58 In Python, other punctuation is flexible (try this)
You can have blank lines – anywhere you want them. You can put 1 or more spaces in between parameters and before and after the “=“ sign. (or not)

59 Write your own Python programs – write trinkets see next few slides to see what this looks like
Make sure you’re logged in to trinket go to: home click on tab – with your userid; choose My Trinkets click on “new trinket” in the upper right corner choose Python (not Python 3) When you’re creating your code for this new trinket: change “Untitled” to the name you want for this program type your code in; run it when you want to; save often

60 How to write your own trinkets (Python programs)
Make sure you’re logged in to trinket Right click on your user name (mine is lmd_0977) Choose the one that says My Trinkets

61 Click on the “New Trinket” button
Then … choose Python

62 type your Python program here

63 push this button to run your program
see output here \

64 Programming Challenge
Write a new trinket that draws a rectangular rainbow. Colors are: red, orange, yellow, green, blue, indigo and violet. Goal: define at least one function Advanced goal: define at least one useful parameter in one of your functions


Download ppt "Introduction to Programming: Module #2 Python, Trinket, and Turtle Graphics Lois Delcambre."

Similar presentations


Ads by Google