STEP 0: OPEN UP THE PYTHON EDITOR. If python is on your computer already, it's time to get started. On Windows, find IDLE in the start menu.



Similar documents
Turtle Power. Introduction: Python. In this project, you ll learn how to use a turtle to draw awesome shapes and patterns. Activity Checklist

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Welcome to Introduction to programming in Python

Installing Java 5.0 and Eclipse on Mac OS X

Programming in Access VBA

Welcome to CSU The Software Used To Data Conference.

r-one Python Setup 1 Setup 2 Using IDLE 3 Using IDLE with your Robot ENGI 128: Introduction to Engineering Systems Fall, 2014

Week 2 Practical Objects and Turtles

Fractions as Parts of a Group

Computer Programming In QBasic

Introduction to Python

Hello. What s inside? Ready to build a website?

Use fireworks and Bonfire night as a stimulus for programming

Logo for Kids: An Introduction. by Bob DuCharme

Fireworks CS4 Tutorial Part 1: Intro

Notepad++ The COMPSCI 101 Text Editor for Windows. What is a text editor? Install Python 3

The Basics of Robot Mazes Teacher Notes

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

Python Loops and String Manipulation

Computer Science for San Francisco Youth

Python Lists and Loops

Florence School District #1

Chapter 2. Making Shapes

Blender Notes. Introduction to Digital Modelling and Animation in Design Blender Tutorial - week 9 The Game Engine

Introduction to Python

User Manual for Data Backups

CS101 Lecture 24: Thinking in Python: Input and Output Variables and Arithmetic. Aaron Stevens 28 March Overview/Questions

What is Skype? Skype is a free program that uses the latest technology to bring affordable and high-quality voice communications to people.

About Kobo Desktop Downloading and installing Kobo Desktop Installing Kobo Desktop for Windows... 5 Installing Kobo Desktop for Mac...

Code::Blocks Student Manual

REFERENCE GUIDE 1. INTRODUCTION

Smart Board Notebook Software A guide for new Smart Board users

Getting Started in Tinkercad

How to Complete the Online Course Work for an Entry Level Clinic

mouse (or the option key on Macintosh) and move the mouse. You should see that you are able to zoom into and out of the scene.

Introduction to programming moway

Introduction to Python

Python. KS3 Programming Workbook. Name. ICT Teacher Form. Do you speak Parseltongue?

Eldy User's Manual. Contents

Introduction to Google SketchUp (Mac Version)

POLARIS INSTALLATION

SCRATCH PROGRAMMING AND NUMERACY IN SENIOR PRIMARY CLASSES

Hypercosm. Studio.

Introduction to SketchUp

Check current version of Remote Desktop Connection for Mac.. Page 2. Remove Old Version Remote Desktop Connection..Page 8

Student Manual. for Virtual Classroom (Big Blue Button)

Microsoft PowerPoint 2010 Templates and Slide Masters (Level 3)

First Bytes Programming Lab 2

User Guide for Archie and Review Manager 5. The Cochrane Airways Group

Introduction to Microsoft Access XP

MSW LOGO 2. MSW LOGO is a programming language that lets you store a series of instructions on the computer.

Remote Access - Mac OS X

If you know exactly how you want your business forms to look and don t mind detail

The Power Loader GUI

How To Create Your Own Profitable Banners In Minutes!

Chapter 9 Slide Shows

Appendix 1: Adaptable Templates

Generate Android App

Your First Web Page. It all starts with an idea. Create an Azure Web App

Create A Collage Of Warped Photos

How To Program An Nxt Mindstorms On A Computer Or Tablet Computer

PowerPoint 2013: Basic Skills

Intellect Platform - The Workflow Engine Basic HelpDesk Troubleticket System - A102

Microsoft Small Basic. An introduction to Programming

Microsoft Word Track Changes

Welcome to The Grid 2

Focus X2 (Mac version) Quick Start Guide

Programming LEGO NXT Robots using NXC

Introduction to Python

Remote Desktop In OpenSUSE 10.3

GETTING STARTED WITH HANGOUTS... 2 START A HANGOUT... 2 JOIN A HANGOUT... 4 INVITE PEOPLE TO A HANGOUT...

PEMBINA TRAILS SCHOOL DIVISION. Information Technology Department. Inspiration 7.0

GOALS: The goal for this session is: OBJECTIVES: By the end of the lesson participants should be able to: MATERIALS: Instructor ACTVITIES: EVALUATION:

Customizing your Blackboard Course

Google Drive: Access and organize your files

TABLE OF CONTENTS SURUDESIGNER YEARBOOK TUTORIAL. IMPORTANT: How to search this Tutorial for the exact topic you need.

Website Development Komodo Editor and HTML Intro

A Java Crib Sheet. First: Find the Command Line

Newsletter Storage and Organization

Microsoft Security Essentials Installation and Configuration Guide

Activation of your SeKA account

SMARTstuff. The first rule for using an interactive whiteboard is very simple and of the utmost importance: Have faith in yourself you can do this!

Advanced Programming with LEGO NXT MindStorms

Virtual Desktop on any computer (incl. Desktop Bar Guide)

Eclipse installation, configuration and operation

Photoshop- Image Editing

Samsung Xchange for Mac User Guide. Winter 2013 v2.3

Fruit Machine. Level. Activity Checklist Follow these INSTRUCTIONS one by one. Test Your Project Click on the green flag to TEST your code

GOOGLE DRIVE Google Apps Documents Step-by-Step Guide

Fish Chomp. Level. Activity Checklist Follow these INSTRUCTIONS one by one. Test Your Project Click on the green flag to TEST your code

ClarisWorks 5.0. Graphics

Grade 7/8 Math Circles November 3/4, M.C. Escher and Tessellations

1001ICT Introduction To Programming Lecture Notes

Quick Guide. Passports in Microsoft PowerPoint. Getting Started with PowerPoint. Locating the PowerPoint Folder (PC) Locating PowerPoint (Mac)

Unified Communications Using Microsoft Office Live Meeting 2007

Part 1 Foundations of object orientation

Transcription:

01 Turtle Power Introduction: This term we're going to be learning a computer programming language called Python. The person who created Python named it after his favourite TV show: Monty Python s Flying Circus. Python is used by loads of programmers for many different things. Python is a powerful and clever language, used at YouTube, NASA, CERN and others. If your club has a Raspberry Pi, you can use Python to program it. Many people love Python because it is easy to read (at least compared to some other programming languages). Being able to read code is a very important skill, just as important as writing it. STEP 0: OPEN UP THE PYTHON EDITOR If python is on your computer already, it's time to get started. On Windows, find IDLE in the start menu. On Mac OS X, open up Terminal.app and type idle and press enter. On Linux, open up a Terminal, and type idle and press enter. If you don't have Python yet, don't panic! You can download the latest version of Python from http://www.python.org/. The exact version doesn't really matter but we're using Python 3, so it should start with 3 (not 2). When IDLE starts you will see an Output window called Python Shell. We need to open a new window to write code in. Go to File á Ṃ New Window so you're ready for Step 1. Make sure you have both windows visible.

STEP 1: HELLO, WORLD! 1. Open IDLE, the editor that comes with Python. All our code will be written in this editor. When you open it, you will see an Output window, where errors and results will appear. 2. If you haven't yet, choose File á Ṃ New Window. An empty window will appear, with 'Untitled' in the title bar. You should have two windows open now, one for writing your program, another for showing output. Make sure you write in the right one! 3. Enter the following code into the new window: printả Ỵ HelloẀ WorldỲ Ỵ ấ 4. Double check you're not in the Python Shell window, and Save your code. You can do this by choosing File > Save. When prompted for a filename, enter hello.py, and save the file to your desktop. Then choose Run > Run Module. Congratulations on your first Python program :D (PS! You can tell it to print anything you like, why not change it to say hello to you? Change it to say your name instead) ProTip: * On Windows and Ubuntu, use Ctrl N to create a new shell window, use ctrl S to save your file after you ve finished editing, and press F5 to run your program. On some computers you may need to press a Fn key too. * On Mac OS X, cmd N to create a new shell window, Command S to save your file, and hold down the function (fn) key and press F5 to run your program. STEP 2: HELLO, TURTLE! Next, we're going to have some fun with turtles. A turtle is

a tiny robot that draws on your screen, we can tell it to move around using Python commands. 1. Open a new code window (From the File menu) and write the following: 2. Save your program as turtles.py and choose Run > Run Module. See how the turtle moved forward 100 pixels? The turtle has a pen attached, and draws lines as it moves around the screen. ProTip: Python program files should always be given a name ending in '.py'. 3. Let's make the turtle move around the canvas! Try using backwardả distanceấ as well as turning the turtle by using rightả angleấ and leftả angleấ. Eg backwardả Ü Û ấ tells the turtle to move backwards 20 pixels, and rightả Ǘ Û ấ tells the turtle to turn right 90 degrees. You can give more than one instruction at a time, they will be executed in order. rightả Ũ Ü Û ấ leftả Ǘ Û ấ backwardả Ũ Û Û ấ leftả Ǘ Û ấ forwardả Ů Û ấ

Angles and Degrees Play around a bit writing your own shapes, using forward, backward, left, right. Remember, forward and backward move in pixels, but left and right turn in angles. Let's look at a turtle turning right. North Û ằ West ằ East Ü Ǔ Û á á á á á Ľ á á á á á Ǘ Û ằ ằ Ũ Ǖ Û South When the turtle is facing North, turning right 90 degrees, makes it face East, turning 180 degrees makes it face south, and turning 270 degrees makes it face West. If you turn 360 degrees, you end up where you started. What about turning left? North Û ằ West ằ East Ǘ Û á á á á á Ľ á á á á á Ü Ǔ Û ằ ằ Ũ Ǖ Û South When the turtle is facing North, turning left 90 degrees, makes it face West, turning 180 degrees makes it face south, and turning 270 degrees makes it face East. If you turn 360 degrees, you end up where you started. One full turn is always 360 degrees.

What does the code at the beginning of our program do tells Python we want to use the turtle library, a collection of code we can use to draw on the screen. Using a library means we can save time. speedả ấ sets the speed of the turtle, it takes a value between 1 and 11. 11 is the fastest, 1 is the slowest. shapeả ấ We are using the "turtle" shape, but it can also take the values "arrow", "circle", "square", "triangle" or "classic". We will be putting these three instructions at the top of all our programs in this lesson. If you like you can make your turtle a different shape, or go as fast or slow as you want. STEP 3: DRAWING SHAPES! Lets make a square by telling the turtle how to move around. 1. Open up a new file in IDLE, and write the following code in:

rightả Ǘ Û ấ rightả Ǘ Û ấ rightả Ǘ Û ấ rightả Ǘ Û ấ Save your program and choose run > new module. Do you see a square?. The turtle turns 90 degrees four times, and ends up facing back in the same direction as it started. Turning 90, 90, 90 and 90 again, turns the turtle 360 degrees in total. What about a triangle? A triangle has three corners, so we need to turn three times. If we want to end up back in the same direction, we will need to turn a total of 360, like the square. So we will turn 120 degrees, then 120 degrees, and once more. 2. Edit your code to look like the following, to draw a triangle instead:

rightả Ũ Ü Û ấ rightả Ũ Ü Û ấ rightả Ũ Ü Û ấ 3. Run your code. Do you have a triangle? Pick a Colour What's your favourite colour? You can change the colour of the lines using the function pencolor (Python uses American spellings), and you can also change the pen size using pensize : 1. Edit the code from before to look like the next example, by adding in these new commands:

pensizeả Ũ Û ấ pencolorả Ỵ redỵ ấ rightả Ũ Ü Û ấ pencolorả Ỵ blueỵ ấ rightả Ũ Ü Û ấ pencolorả Ỵ greenỵ ấ rightả Ũ Ü Û ấ 2. Run your code, what does it draw on screen? This code draws a thick triangle in three different colors. 3. Try changing the colors in your code, run it, and see what happens. The turtle knows hundreds of different colours, not just red, blue and green, try using your favourite colour! You can also specify colours in hex like we did in CSS. Instead of using pencolorả redấ you could use hex pencolorả Ỵ D FFÛ Û Û Û Ỵ ấ. What colour is #FF4F00? STEP 4: REPEATING YOURSELF (WITH A FOR LOOP) That last program was the same commands over and over again. Instead of writing them down, let's ask the computer to repeat them for us. You should have encountered iteration in Scratch before using the Forever and Repeat / Repeat until blocks. In Python for loops are used when you have a piece of code which you want to

repeat n number of times. In this case we want to repeat the code (that is indented) 4 times (because a square has 4 sides). 1. Open up a new file and type the following in: for count in rangeả Ŭ ấ Ẃ rightả Ǘ Û ấ 2. Save your program and choose: Run > Run module. Notice the program is indented, or pushed to the left under the for loop. Python uses spaces to know which commands to repeat. You can use the Tab key in IDLE to add indents, and use Shift Tab to remove some. 3. Let's see what happens when we only indent forward, and edit your program to look like this one: for count in rangeả Ŭ ấ Ẃ rightả Ǘ Û ấ 4. Notice how forward is indented and right isn't? What do you think this program does? Try running it to find out? Did you get a straight line? Python will repeat forward four times, and then turn right. Python uses spaces to

group commands together, like scratch uses blocks. Python will complain if the code isn't properly lined up. 5. Let's change it back to the earlier program, and get it to run a square, but instead of using numbers in the code, we can assign names. This makes it easier to see what the program is doing, and also helps us to stop repeating ourselves. Edit the file to look like this: sides Ḻ Ŭ length Ḻ Ũ Û Û angle Ḻ Ǘ Û for count in rangeả sidesấ Ẃ forwardả lengthấ rightả angleấ 6. Save your program and choose: Run > Run module. Challenge: Drawing the other shapes Can you draw the following shapes by changing the values? a triangle? (three sides) a pentagon? (five sides) a hexagon? (six sides) an octagon? (eight sides) Remember, a Triangle has three sides, and turns 120 degrees. Turning 120 degrees for each corner means we turn 360 degrees in total. For a Square, we turn 90 degrees for four corners, which also adds up to 360 degrees.

If you are turning six times, how much should you turn so it adds up to 360? Try out numbers and see what shapes you get. STEP 5: TURN, TURN, TURN Instead of us working out the angle, why don't we let the computer do it for us. Python can let you do some operations on numbers, like addition, subtraction, multiplication and division. We could write sides Ḻ Ŭ Ľ Ũ instead of 5, or sides Ḻ Ŭ á Ũ instead of 3. For multiplication, Python uses ặ, and for division, we write ắ. If we need to turn 360 degrees in total, we can work out the angle we'll need. For a square, Ū Ű Û ắ Ŭ is 90, for a Triangle, Ū Ű Û ắ Ū is 120. 1. Change your program to calculate the angle as following. sides Ḻ Ŭ length Ḻ Ü Û angle Ḻ Ū Ű Û ắ sides for count in rangeả sidesấ Ẃ forwardả lengthấ rightả angleấ 2. Now change the number of sides, does Python get it right? Try drawing a hexagon (6 sides), or any number of sides you want!

STEP 6: SOLID SHAPE 1. We can ask the turtle to fill in shapes with a colour, by using beginạ fillả ấ and endạ fillả ấ. Change your code to add these commands in: sides Ḻ Ŭ length Ḻ Ü Û fillcolorả Ẏ redẏ ấ pencolorả Ẏ redẏ ấ beginạ fillả ấ angle Ḻ Ū Ű Û ắ sides for count in rangeả sidesấ Ẃ forwardả lengthấ rightả angleấ endạ fillả ấ Like with pencolor, fillcolor sets the color the turtle uses to fill in the shapes you draw. This draws a red square with a red outline. You use beginạ fillả ấ to tell the turtle you're drawing a shape to colour in, and endạ fillả ấ to say you've finished it. 2. Try changing the colours, sides and lengths to see what shapes you can draw! STEP 7: PEN GOES UP, PEN GOES DOWN

If you want to move the turtle without leaving a trail behind it, you can use penupả ấ and pendownả ấ to turn drawing on and off. 1. In a new file, try the following: pencolorả Ẏ redẏ ấ for count in rangeả Ü Û ấ Ẃ penupả ấ forwardả Ũ Û ấ pendownả ấ forwardả Ü Û ấ 2. This should draw a dashed line across your screen. Run it and see! Home, home on the screen. One last little trick. homeả ấ returns the turtle to the starting position. clearả ấ wipes the screen clean, and resetả ấ moves the turtle and clears the screen. STEP 8: GO WILD! You can go forwardả ấ, backwardả ấ, leftả ấ, rightả ấ, you can loop using for count in rangeả Ŭ ấ, change colours, change speed and even fill in shapes! Can you draw a house, a bird? a snake? a cat? a dog? a lion? Combine the shapes together and see what you can draw. Can you draw a robot?

These projects are for use within the UK. You are required to register your club in the UK and required by law to have child protection checks and insurance. Registering your club with us means Code Club UK can support you with the running of your club. More information is available on our website at http://www.codeclub.org.uk/. This coursework is developed in the open on GitHub, https://github.com/codeclub/python curriculum come and join us!