/
Introduction to  Programming: Module #2 Introduction to  Programming: Module #2

Introduction to Programming: Module #2 - PowerPoint Presentation

natalia-silvester
natalia-silvester . @natalia-silvester
Follow
380 views
Uploaded On 2018-02-26

Introduction to Programming: Module #2 - PPT Presentation

Python Trinket and Turtle Graphics Lois Delcambre First a comment about blockly Most programming languages including those used by professionals have the following constructs ID: 637352

turtle tina function python tina turtle python function write left program import shape functions triangle penup 120 trinket module

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Introduction to Programming: Module #2" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

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

Lois DelcambreSlide2

First … a comment about blocklyMost 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!Slide3

Plan for TodayIntroduce 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.Slide4

https://hourofpython.trinket.io/a-visual-introduction-to-python#/turtles/meet-tina

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)Slide5

go to

trinket.io

click on “Learn” tab – at the top rightSlide6

Click on Start Learning!Slide7

To see the Table of

Contents, click here:Slide8

With Table of

Contents expanded,

you can see the

lessons here:Slide9

We’ll start with “Meet Tina”Slide10

https://

hourofpython.trinket.io

/a-visual-introduction-to-python#/turtles/meet-

tina

A Python programSlide11

https://

hourofpython.trinket.io

/a-visual-introduction-to-python#/turtles/meet-

tina

The button to push – to run the programSlide12

https://

hourofpython.trinket.io

/a-visual-introduction-to-python#/turtles/meet-

tina

The output from this programSlide13

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.Slide14

Plan for TodayIntroduce Python at trinket.ioIntroduce the turtle module

Define functions in Python

Compare

blockly

and Python

Write your own Python programs.Write your own trinkets.Slide15

Meet Tina program

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.penup

()

tina.forward

(20)

tina.write("Why, hello there!")

tina.backward(20)Slide16

Meet Tina program – explained (1)

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.penup

()

tina.forward

(20)

tina.write("Why, hello there!")

tina.backward(20)import allows you touse all of the functionsand methods writtenin the module.

Here we import the

turtle module.Slide17

Meet Tina program – explained (2)

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.penup

()

tina.forward

(20)

tina.write("Why, hello there!")

tina.backward(20)the Turtle method in theturtle module creates anew turtle.

This new turtle becomes

associated with the name

(that the programmer chose)

tina

.Slide18

Meet Tina program – explained (3)

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.penup

()

tina.forward

(20)

tina.write("Why, hello there!")

tina.backward(20)To use a methodin the turtle module:name of the module(turtle) followed by a dot (.)

and then

the method name.

(Turtle).Slide19

Meet Tina program – explained (4)

import turtle

tina

=

turtle.Turtle

()

tina.

shape

('turtle')

tina.

penup

()

tina.

forward(20)tina.write("Why, hello there!")tina.backward(20)

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).Slide20

Parameters for functions/methodsSometimes (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:

T

he 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.Slide21

Parameters for functions/methodsSome functions/methods need them; some don’t

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.penup

()

tina.forward

(20)

tina.write

("Why, hello there!")tina.backward(20)the forward method has

one parameter – how far

to go forward.

the

penup

method has no

parameters. It simply

puts the pen up

.Slide22

Meet Tina program – explained (5)

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.penup

()

tina.forward

(20)

tina.write

("Why, hello there!")tina.backward(20)Every time youinvoke a functionor 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.Slide23

-200 0 200

2

00

-200

Coordinate space for turtle

turtle has a “heading” – starts out East (to the right), as shown

23Slide24

Class workOn your own

the

Moving

lesson Slide25

Saying Hello program explained (1)

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)

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).Slide26

functions some functions are built-in to Pythoninput

: 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 parametersfunctions typically return some sort of valueinput – hands over the text typed by the usergcd – hands over the number that is the greatest common divisor of the two parametersSlide27

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.Slide28

methodstina is a turtle object in the

Meet Tina

program, for example

thus, we can invoke all turtle methods for

tina

tina.forwardtina.penup

etc.Slide29

Saying Hello program explained (2)

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)

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).Slide30

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)Slide31

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)Slide32

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.Slide33

Assignment statements in Python (2)

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)An assignmentstatement 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!Slide34

Assignment statements in Python (3)

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)

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.Slide35

Assignment statements in Python (4)

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)

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.Slide36

Assignment statements in Python (5)

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)

Now that the

variable (

your_name

)

has a value, we

can use that variable

whenever we want

to used the value.Slide37

Saying Hello program explained (5)

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)

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>!Slide38

Class workOn 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 lessonWe’ll cover

Repeating with Loops and Lists

tomorrow

Skip:

Lists of numbers, Loops of Lists, Changing Colors lessons

On your own:

Turtles are Objects

lessonTina and Tommy’s Colors lessonSlide39

Plan for TodayIntroduce Python at trinket.ioIntroduce the turtle module

Define functions in Python

Compare

blockly

and Python

Write your own Python programs.Write your own trinkets.Slide40

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) tina.forward(30)

tina.left

(120)

tina.forward

(30)

triangle()Slide41

the Functions are recipes! lesson – explained (2)def to define a function

Function definition here.

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.color

('purple')

def

triangle

():

tina.left

(60)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

triangle()

T

he name of the function here.Slide42

the Functions are recipes! lesson – explained (3)def to define a function

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.

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.color

('purple')

def

triangle

():

tina.left

(60)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

triangle()Slide43

the Functions are recipes! lesson – explained (4)function invocation

Function invocation here.

Invokes the function named

triangle.

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.color

('purple')

def

triangle():

tina.left

(60)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

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.Slide44

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)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

triangle()

No parameters listed here

in the function definition.

No parameter values provided

here when the function is

invoked.Slide45

the Functions are recipes! lesson – explained (6)the code block inside a function definition

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.

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.color

('purple')

def

triangle():

tina.left

(60)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

triangle()Slide46

Plan for TodayIntroduce Python at trinket.ioIntroduce the turtle module

Define functions in Python

Compare

blockly

and Python

Write your own Python programs.Write your own trinkets.Slide47

blockly vs. Python

They both have one command after the other.

You can have blank lines in Python – anywhere.

Moving

programSlide48

blockly vs. Python turtle module

“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.

Moving

programSlide49

blockly vs. Python turtle module (cont.)

“turn”

can turn left or right

“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.

Moving

programSlide50

blockly

vs. Python

50

function

definition

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.color

('purple')

def

triangle():

tina.left

(60)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

triangle()

Functions are Recipes!

programSlide51

blockly

vs. Python

51

function

name

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.color

('purple')

def

triangle():

tina.left

(60)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

triangle()

Functions are Recipes!

programSlide52

blockly

vs. Python

52

function

invocation

import turtle

tina

=

turtle.Turtle

()

tina.shape

('turtle')

tina.color

('purple')

def

triangle():

tina.left

(60)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

tina.left

(120)

tina.forward

(30)

triangle()

Functions are Recipes!

programSlide53

Class WorkWork on your own:Functions are recipes! lesson

Make 5 cakes

lessonSlide54

Plan for TodayIntroduce Python at trinket.ioIntroduce the turtle module

Define functions in Python

Compare

blockly

and Python

Write your own Python programs.Write your own trinkets.Slide55

punctuation Can you read this?

Mary is my sister she’s taller than me she. . has blonde hair she’s younger than I amSlide56

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 periodSlide57

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.Slide58

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)Slide59

Write your own Python programs – write trinketssee 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 oftenSlide60

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 TrinketsSlide61

Click on the “New Trinket” button

Then

… choose PythonSlide62

type your

Python program hereSlide63

push this button to run your program

\

see output hereSlide64

Programming ChallengeWrite 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 parameterin one of your functions