Intro to Python Programming If you re using chromebooks at your school, you can use an online editor called Trinket to code in Python, and you ll have an online portfolio of your projects which you can access from any computer! Go to Trinket.io: Sign up for a free account by clicking Sign Up at the top of the page. Once you re signed up your screen should look like this:
Click the big blue button to make a new Trinket and select Python. You ll get a screen with a blank editor (like a word processing document) on the left and an output window on the right:
It s easy to print text output in Python. Just type print hello world (or any text you d like to print) and press the Run arrow. You ll see your output on the right: Getting Started with Turtle Graphics But text output isn t the coolest thing you can do with Python. There was a programming language called Logo which was invented at MIT in the 1960s. It allowed users to draw geometric designs by making turtles walk around the screen using simple commands like
forward and right. Python copied that functionality into its turtle module which is installed by default when you download Python on your computer. Trinket supports all the Python turtle functions that you can find on the Python.org site: https://docs.python.org/3/library/turtle.html#module-turtle Or you can do a web search for python turtle and those docs should be the first link to come up. You ll find dozens of functions you can make the turtles do but we ll only need forward and left for this project. Importing modules People have written lots of code that a programmer could find useful, and they share them in files called modules, many of which are included in the Standard Library you get when you install Python. There are a few different ways to import modules, but the easiest way at this stage is to type from turtle import * to import all the turtle functions. Then you can give a turtle a command and when you click the Run arrow you ll see the results:
The forward function requires a number of steps or pixels you want the turtle to take. You can make the turtle look like a turtle by adding a shape command after the import. You can also change the color this way: Just by learning one more command, left, you can make the turtle draw all kinds of shapes. The left function requires a number of degrees you want the turtle to turn. left(90) makes it turn 90 degrees to the left:
Using only the forward and left functions, you can make the turtle walk around the screen in the shape of a square. You have to repeat your code a bunch of times, and the abbreviations fd and lt come in handy: Loops Every programming language has a way to tell the computer to repeat a block of code a specific number of times. This is called a loop, and it s the most important programming tool. In Python it s easy: for i in range(n): means repeat this code n times. We re repeating forward(100) and left(90) four times, so we can save some typing by using a loop like this:
Notice the code we want to repeat is indented. This is extremely important, since that s how Python knows which lines you want to repeat! If you don t indent a line of code, it won t be repeated, like this example: Since dippity is indented it gets repeated 3 times but doo isn t indented, so it s only printed once. Functions Now that we have all the code necessary to draw a square, let s save all that code to a function. This means that we ll be able to use the function name, like a magic word, in the future instead of typing all the code all over again. In Python you define a function by coming up with a name (I m going with the descriptive name square ) and you tell Python it s a function with the abbreviation def in front of it: Notice all the code inside the square function has to be indented (again if it s already indented!). If you run the program now, it won t draw anything. You ve told the turtle how to draw a square, but you didn t tell it to. Add the last line so the square function executes automatically when you run the program.
The Rosette Now that we ve taught the turtle to make a square, and saved it to the magic word/function square, next we ll tell it to square, then turn left a little, square again, turn left a little, over and over. Sounds like a job for a loop! Change your code to this: When you click Run, you should see a rosette like this: This shows that when you repeat things a lot of times using loops, you can make surprisingly complicated designs with very little code! The Spiral
But our squares are all the same size. What if we wanted to make them different sizes? What would we have to change? Only the forward line. Whatever size we want the square to be (its side length) we just put in the parentheses after the forward command. square(10) will make a square of side length 10 for example. Now you can use a variable to make squares of any size! Add the spiral code below: But when you press Run, nothing happens! Remember, you have to tell the turtle to run the spiral function by adding this line at the very end of the program: Click Run and the turtle will draw a growing, expanding spiral of squares.
You can easily change the base shape to a circle, a triangle or a star. This is a very useful procedure: making parts of your program into a variable (like we did with length) so we can vary it! Have fun making more designs using Python!