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 print is the action and the words Hello World are the input to the function.

Copy this code into the cell below:

print("Hello World")

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 ( and ). 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")
[ ]:

Add print instructions to the cell above to complete the poem. Do you see how they are executed in order?

Comments

A comment is a part of a program that’s ignored by Python. Comments are meant for the humans. Comments explain what code is doing to someone who is trying to read it. Comments make code more clear and easy to read. Comments begin with the hash # character. Here’s an example:

# The next line prints "Hello World"
print("Hello World")

Copy the code into this cell:

[ ]:

Notice that the comment is a different color! Try placing the # before the print function an re-running the cell. What happens when you comment out the print statement? If you’re just getting started with programming put a comment before every line of code that explains what the next line does. The commens will help you remember instructions.

Importing Packages

There is a core set of Python statements that are always available, including the print function. To use the full power of Python programs access add-on functions inside of 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

Once you 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:

Directions to AJ's

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.

[ ]:

Conclusion

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?