Some Python programs are very big, and programs written in Python can be found in many places.
Some Python programs are very small. Here’s a Python program that’s twenty-six characters long!
print("Welcome to Python")
This Python program contains one command,
print("Welcome to Python"). This command causes the words
Welcome to Python (without the quotes) to be added to the program's output. Press the "Run code" button above to see this.
Here’s a slightly longer Python program. Try to predict what this program will output before you hit “Run code”.
print() that produce text output are a little bit boring!
Instead, we'll learn Python with the help of a turtle.
We won’t actually be interacting with any reptiles. Python is a programming language that has a lot of libraries. For now, you can think of a library as something that gives your Python program new superpowers. One of those libraries, named
turtle, is a turtle drawing library.
In a turtle drawing library, our program gives commands to a meticulous, patient, and obedient turtle. The turtle walks around the canvas dragging a paintbrush behind her.
Drawings that are created by a turtle drawing library are a result of the turtle's movements, viewed from above.
People have written turtle drawing libraries for many languages, not just Python. Turtle drawing has been used to explore programming since the 1960s!
Sometimes, a program can be big enough that it’s hard to see the forest for the trees. When this happens, we’ll use our code tool to help walk you through the important parts of our code.
Even though this program has just three lines, stepping through this line-by-line explanation will help make the process more familiar when you encounter it again later.
from turtle import * forward(300) bye()
This line accesses the
turtle library, a process called importing.
Without this line, we can't use any of the commands that control our turtle.
forward() command is provided by the
turtle library. This command tells the turtle to walk forward 300 steps.
bye() command tells the turtle she is done, and causes the turtle's image to be saved. If you don't run this command, there won't be any picture to see!
forward() commands, the
bye() command doesn't have any arguments. The Python language requires you to include the parentheses anyway.
You can see the notes again by pressing the "Show notes" button that is next to the "Run code" button.
Make sure you can see the animation of the turtle starting in the center and moving! If you don't see the animation, run the code again.
Based on this animation, what direction is the turtle facing when the program begins?
Reading and interpreting code is a difficult but important skill. Take a look at the following code and try to decide on the best English description of what it does.
from turtle import * forward(200) left(90) forward(100) bye()
This program includes a new command,
left(90), that you haven't seen before. Despite the presence of this new command, don’t be afraid to make a guess before you’ve run your code! At the same time, don’t be afraid to run your code to check your guesses.
When we think about reading and communicating Python, we have some decisions to make! Most of the time, the same chunk of code can be explained in different ways. For example, consider this code:
from turtle import * forward(200) left(90) forward(200) left(90) forward(200) left(90) forward(200) bye()
This code could be reasonably described in English in any of the following ways:
Depending on the situation, any of these descriptions might be "good," and any of them might be "bad." Thinking about code is like thinking about other form of human communication: you have to be sensitive to the way that the author understands her code, the way the author intends her readers to understand her code, and the way that you, the reader, want to understand the code.
Code comments are an important tool for reading other people's code, and for helping other people read your own code.
A code comment is a piece of a computer program that is only designed for humans. Python does not look at comments or treat them as commands.
We write a comment in Python by using the pound sign
# followed by some explanatory text. Everything between the
# and the end of that line is a comment.
A code comment, along with helpful use of blank lines, is a good way to take a confusing series of commands and make them clear and contained.
from turtle import * # Draw a hexagon forward(150) left(60) forward(150) left(60) forward(150) left(60) forward(150) left(60) forward(150) left(60) forward(150) bye()
Code comments can also help you understand things you haven’t seen before.
Take a look at the code below. With the help of the comment, try to read the code and understand what it does before you run it.
from turtle import * forward(200) # Make a diameter 200 semicircle, curving down for i in range(100): forward(3.416) right(1.8) forward(200) bye()
Did the output when you pressed “Run code” match what you imagined? If so, that’s terrific! If not, go back and try to re-read the code again based on what you know now.
Reading code is a difficult skill. Even the best programmers needed time and patience to learn how to do read code well.
The chapters of this course will walk you through how to read and write Python. You'll learn about the "vocabulary" of Python: strings, integers, commands, and variables. You'll also learn about Python's "grammar": functions, conditional statements, and loops.