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

Slides:



Advertisements
Similar presentations
Shapes and Designs Inv. 1.3: Tiling Activity.
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
Getting Started With Alice By Ruthie Tucker under the direction of Prof. Susan Rodger Duke University, July
Yr 9 a computer science Nice to meet you variable challenge! 1.Variable assessment Looping in python 2.Looping assessment Drawing in python using turtle.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Java: Chapter 1 Computer Systems Computer Programming II Aug
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Making a Timer in Alice.
Java: Chapter 1 Computer Systems Computer Programming II.
Using Microsoft Office Picture Manager How Cropping Can Make Your Presentations Look More Professional.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Introduction to Using the Notebook 10 Software for SMART Board Day 2 LIVINGSTON PARISH PUBLIC SCHOOLS Facilitated by S. Waltman.
EQ: How can we learn the basics of formatting a college research paper in Microsoft Word? Mini Unit: Typing a Paper Diogene Date: 4/20/2015 Course: ELA-Grade.
Using the paper provided for this activity, fold your paper so that it is divided into 14 boxes. The bottom 2 boxes should be wider than the others. In.
Variety of JavaScript Examples Please use speaker notes for additional information!
Game Maker – Getting Started What is Game Maker?.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Learning PowerPoint Presenting your ideas as a slide show… …on the computer!
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Intro to Turtle Graphics (Part 2)
Turtle Graphics Lesson 2 1. There are 3 homeworks to complete during the six lessons of this unit. Your teacher will let you know when a homework has.
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
Introduction to Programming: Module 1 Blockly
Chapter 2 Writing Simple Programs
INTRO to PIXLR.com.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Graphics CIS 40 – Introduction to Programming in Python
Whatcha doin'? Aims: To start using Python. To understand loops.
User-Written Functions
encryption and decryption programs
Entering Elementary SBRC Term Grades in Aspen
Interactive Notebooks
PYGAME.
Going Green By Ima Librarian
Teaching Characters to Walk: Learning Methods, Part 1
Unit C: Expressions Lesson 03: Mathematical Properties with Variables
Introduction to Python
CS005 Introduction to Programming
Comparing objects and changing color
Lesson 4 - Challenges.
Computer Programming I
Variables, Expressions, and IO
The things you need to know to get your gradebook going again!!
Agent P, I have been hearing some rumours about a Python Turtle.
Learning to program with Logo
Frozen Graphics Lesson 3.
CS 100: Roadmap to Computing
Introduction to TouchDevelop
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CS 100: Roadmap to Computing
Creating Functions with Parameters
CISC101 Reminders Assn 3 due tomorrow, 7pm.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Introduction to TouchDevelop
Java Programming Control Structures Part 1
Go to =>
A look at Python Programming Language 2018.
Introduction to Object-Oriented Programming in Alice
How to Start This PowerPoint® Tutorial
CISC101 Reminders All assignments are now posted.
Using Logo and Logic This presentation uses a version of Logo called StarLogo available through MIT. It can be downloaded for free and installed on your.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Marty the Robot.
creating a ecosystems model in net logo
Chapter 1 Introducing Small Basic
CISC101 Reminders Assignment 3 due today.
Starter Which of these inventions is: Used most by people in Britain
Using Modules.
Presentation transcript:

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

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!

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.

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) https://hourofpython.trinket.io/a-visual-introduction-to-python#/turtles/meet-tina

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

Click on Start Learning!

To see the Table of Contents, click here:

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

We’ll start with “Meet Tina”

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

The button to push – to run the program https://hourofpython.trinket.io/a-visual-introduction-to-python#/turtles/meet-tina

The output from this program https://hourofpython.trinket.io/a-visual-introduction-to-python#/turtles/meet-tina

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.

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.

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

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)

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)

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)

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)

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.

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)

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)

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

Class work On your own the Moving lesson

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)

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

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.

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.

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)

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)

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)

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.

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)

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)

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)

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)

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)

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

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.

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

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.

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.

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.

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.

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.

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.

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

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.

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

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

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

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

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

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.

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

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

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.

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)

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

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

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

type your Python program here

push this button to run your program see output here \

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