Python is a programming language. That means it’s a way of writing code, instructions that both computers and humans can read. Other languages that you may have heard of include JavaScript, C++, Java, and Ruby.
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!
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”.
Commands like 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! "Turtle drawing" is a way of creating pictures where we act as if our program is giving commands to a meticulous, patient, and obedient turtle. The turtle walks around the canvas dragging a paintbrush behind her.
Our drawings are a result of the turtle's movements, viewed from above.
Python automatically has the ability to print out text with print()
, but Python doesn't automatically have the ability to draw pictures with turtle drawing.
You add new abilities to programs in Python (or in other programming languages) by using libraries. For now, you can think of a library as something that gives your Python program new superpowers. One of those libraries, named turtle
, gives your program the ability to create turtle drawings.
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.
You can see the notes again by pressing the "Show notes" button that is next to the "Run code" button.
The turtle appears in the animation as a small triangle. Be 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.
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:
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 forms 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 hash 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.
Code comments can also help you understand things you haven’t seen before.
Take a look at the code below. It contains new things you haven't seen before, like for
, range
, and i
.
Don't worry about understanding the details of this unfamiliar part of the program right now. Instead, with the help of the comment, try to understand what the code does before you run it.
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 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.