In this course, you'll learn the basics of Python, a programming language that's used by millions of people around the world. Python is a great first programming language to learn, because it's powerful, simple, and flexible. And it's used by professional engineers at places like Google, NASA, and IBM.
Computers don’t yet understand human languages like English or Spanish. That means human operators need to write instructions for a computer to follow in a language that computers can understand. These are called programming languages.
Programming languages start with a set of well-defined terms such as IF or DISPLAY. Programmers can take these terms and put them together to make what is called a programming statement, such as DISPLAY (3 + 4). A collection of programming statements is called a code segment. A Computer Program is a collection of code segments that performs a specific task when run by a computer. So really, when you are using any kind of software, your computer is running a computer program!
Python isn’t the only programming language out there. Dozens and even hundreds of programming languages have been invented over the years. But once you understand one programming language, you’ll be able to learn others easily.
Here is an example of Python code that prints all the numbers from 0 to 9.
The course is organized into a series of Units. Each unit is broken into chapters. You can skip around if you like, but it is recommended that beginners follow this course sequentially: Each challenge builds on the last, and they grow in complexity as the course progresses.
Unit 1. Syntax and Sequencing Learn how to make simple function calls, and how to give instructions in Python. String function calls together to solve linear programming puzzles.
Unit 2. Loops and Patterns Build on your Python knowledge by creating loops. Learn how to include for loops in your programs and how to repeat necessary code.
Unit 3. Conditional Logic In this unit, you will learn how to branch code. How to run specific commands as you run into different problems.
Unit 4. Conditional Loops Combine your knowledge of loops and conditionals to create loops that are even more powerful than just repeating code a set amount of times.
Unit 5. Variables Learn how to store information within programs. Explore how to receive input from users and how to handle the data.
Unit 6. Data Types and Expressions In this unit, investigate the different types of variables that exist in Python and how each one can help you write programs. Learn how to combine variables and data to create expressions.
Unit 7. Turtle Graphics Learn how to draw with Python. Using turtle graphics you will be able to combine all of your knowledge to create beautiful artwork, with code!
New concept: Introduces a new term or idea.
General idea: Shows a diagram of a short Python snippet.
Puzzles: Programming challenges that test your program and give immediate feedback on whether your solution works.
Project: Be creative with these programs - there is more than one way to solve a problem!
Below you will see the Tynker inline code editor. It is divided into two sides.
The Code window, is where you will be typing and editing your code. Throughout this course the Code window will be either editable or read-only (as seen here).
The Preview window, is where you will see the output or product of your code. You will also interact with the running program in the Preview window.
To see what the code does you will need to execute the program. Executing a program means you will send your instructions to the computer, and it will read and act on the instructions provided. In this course, you will do this by clicking the "run" button in the Preview window.
Notice how when you click "run" the Console window appears. This is a special area in the Preview window that will allow you to see your programs output. In this case, the output is, "Welcome to Python." You can open and close the Console window by clicking it at the bottom of the Preview window.
Humans need to be able to communicate with computers. And computers need to give humans like you information back too.
Computer programmers call this idea input and output. You put information inside your computer using input devices like your keyboard or mouse. Even gestures like "shake to undo" on smartphones can be considered input. If you control your phone or computer with voice commands, you guessed it: That microphone is another example of input!
You get information from your computer by watching your screen or listening to the computer's speaker. This is called the output of the computer. Some people even print their documents—this is another example of output.
A value is a unit of information, like a number or basic text. To pass a value to a instruction, you will type it between the parentheses.
Here the value is the number 42
.
A value can also be basic text enclosed in quotation marks ("
).
Use a print()
instruction to display your first name.
The word print
is the name of a function, an action that the computer knows how to perform. Just as humans have certain actions they know to perform like "eat" and "sleep", computers have an extensive set of known actions that they can perform.
A function is a set of instructions that are organized into one action. It is a shortcut, or a way of organizing code into a general tool that you can use again and again.
In Python and many other programming languages, functions are signified by using parentheses ()
that follow the function name. You may see functions also referred to as methods.
Parameters are the values sent to a function, inside the parentheses ()
. These may also be called arguments. Not every function takes parameters, but many do. Parameters allow you to take finer-grained control of the function's behavior.
A function call is an instruction that tells the computer to perform a function with some value.
For example, consider the function call below:
The print()
function takes numbers and text as input. It will then print the given value to the computer's console.
If you used a capital letter, that is, you wrote Print(42)
— your code will not work, because in Python, only print()
has been defined. So type carefully — capitalization matters.
In the code above, the function call tells the computer to perform the print()
function with the value "Code is everywhere!"
. Every computer performs the print()
function by displaying the given value as output.
A sequence of instructions is called a program, and is sometimes referred to as code.
Most programs have more than one instruction. Each instruction is written on a separate line.
When a computer executes a program, it performs each instruction in the program one-by-one.
print()
FunctionPrint the name of your favorite author, artist, and historical figure on separate lines.
Just like with English, Python has its own grammar called syntax. Code that does not follow Python's syntax will produce a syntax error.
Debugging is the process of fixing broken code. Let's take a look at some broken code and see if you can debug it.
Syntax Errors happen when Python can't understand the program, often due to a missing or misplaced symbol. In these puzzles you'll have to debug the faulty code in order to reach the goal.
A function call must have an open and closed parentheses around its arguments.
Fix the function call by reading the error message and making the necessary correction.
Spaces between values, or in function calls, won't affect how the computer interprets the instruction.
A syntax error can often be the result of a misplaced, or missing, symbol.
Fix the function call by reading the error message and making the necessary correction.
A function call requires a valid argument. In this case, hello
is being interpreted as code, not basic text. Add quotation marks around hello
to indicate it should be interpreted as a string.
A function name must be a known action. In this case, display()
is not a known function. Change the name of the function below to print
.
Sometimes a problem with a program is not caught by the computer. In this case, each line is programmed correctly, but the sequence is off.
Fix the order of the print statements to correct the outputted sentence.
Try debugging the boat printed below by fixing any syntax and sequencing errors of the print()
statements.
Add a )
to the first statement, and a "
to the final statement. Then swap the third and fourth print()
statements to make the boat print correctly. This is an example of a logical error: The program will run, but it doesn't display what you want unless you change the order or your code instruction.
Can you fix the program below to print the haiku?
Let's review what you've learned so far.
One of the most common instructions you will give a computer is to display some information. This can be performed in Python with the print()
function.
A value is a unit of information, like a number or basic text encased with "
quotation marks.
A function call is an instruction that tells the computer to perform a function with some value.
When a value is used in a function call, it is called an argument. Arguments are enclosed in parentheses after the function name.
A sequence of instructions is called a program, and is sometimes referred to as code.
Most programs have more than one instruction. Each instruction is written on a separate line.
When a computer executes a program, it performs each instruction in the program one-by-one.
Debugging is the process of fixing broken code.