Getting Started with Python¶
Programs are made of instructions called statements. In this lecutre you’ll learn your first Python statements and how to use the Jupyter Notebook to experiment with and run programs. Instructions perform an action on inputs and produce a result. In the example below
Hello World are the input to the function.
Copy this code into the cell below:
Execute the cell by pressing
Ctrl-Enter or clicking the “Play” button above:
Cells can be edited. Edit the cell above to change “Hello World” to your name and re-run the cell. If you’re just learning to program it’s important to see how the action and the input relate to one and other. The statement begins with the action and the input is in parentheses
). Statements are run one at a time, from top to bottom. The first statement of a program is always on the first line, just like the first word of a book. The program below performs the print action multiple
times with different input.
print("Roses are red") print("Violets are blue")
There is a core set of Python statements that are always available, including the
packages. Anyone can create a Python package and make it available for other people to use. Jupyter, for example, is a Python package. The
import statement makes a package available to a program. This statement imports the
p4e.drawing package and makes
Turtle available to our notebook.
from p4e.drawing import Turtle
Copy and run the instruction.
from p4e.drawing import Turtle
import a package it’s available as long this notebook is open. If you close and open the notebook again you have to re-run the
import statement. Now that we have a turtle available we can create a new
Turtle and show it on the screen.
# Create a new Turtle named tu tu = Turtle() # Show the turtle. display(tu)
Let’s drive the turtle around. The turtle respons to commands. Here are a few to get started:
tu.draw(100) # Move forward 100 pixels tu.turn(90) # Turn left 90 degrees tu.clear() # Clear the arena and go back to the middle
Code, Run, Debug¶
No one ever types a program perfectly on the first try. No one. Ever. After typing all of the code run the program to see if it works. It can be discouraging when a program that seems right doesn’t work for some reason. Please remember that being a master programmer means having made every mistake so many times that you can quickly spot problems. If you’re just beginning you are building that experience and it can be frustrating.
The debugger is an essential tool to help you understand what your program is really doing. Look in the upper right corner of this tab and you’ll see a bug icon with a little switch next to it. Turn the switch on. Did you notice a change in the cell above? The change is subtle, but when the switch is on there’s a little more space on the left margin. Click to the left of the number “1” and a red dot appears.
With the red dot showing run the contents of the cell. In class I show you how to use the “Step” buttons in the debugger to step through your program one instruction at a time. When you become skilled as a programmer you will be able to this process in your head.In class I show you how to use the “Step” buttons in the debugger to step through your program one instruction at a time. When you become skilled as a programmer you will be able to this process in your head.
Step by Step Instructions¶
Each statement in Python is a small part of a bigger program. Programs perform high-level tasks like, “go to the grocery store.” They complete the task one step at a time using statements that perform smaller, more specific tasks like, “Turn right on Pacific Avenue.” The
p4e.drawing module has the
Turtle object that we can use to draw lines in the notebook.
Let’s consider an algorithm to drive the Turtle from Cabrillo to AJ’s. Here’s a link to directions in Google Maps.
Here’s a picture of the dirctions:
Step 1: Write the Algorithm in Comments¶
Before you begin a program think about the steps you need to perform the task. Write them in comments.
# Drive 0.3 Miles # Turn right # Drive 0.4 miles # Turn left # Drive 66 feet
Step 2: Write a Little Code¶
When you think you have the steps start coding. Write your code in order and after every step test to see if it’s working like you expect. Don’t try to write all the code at once.
# Create a Turtle tu = Turtle() # Show the turtle display(tu) # Drive 0.3 Miles or 30 pixels. tu.draw(30) # Turn 90 degrees to the right. tu.turn(90)
You may have to adjust your plan as you go but it’s important to start with an algorithm in mind. Add code to the cell above until it matches the following:
# Create a Turtle tu = Turtle() # Show the turtle display(tu) # Drive 0.3 Miles tu.draw(30) # Turn right tu.turn(90) # Drive 0.4 miles tu.draw(40) # Turn left tu.turn(-90) # Drive 66 feet tu.draw(10)
Copy this code into the cell below and run it.
There’s still a lot to learn. Do you see how program are made of instructions that run one at a time? Do you know how to turn on the debugger and step through your program slowly?