Intro to Python
Share
Explore
Learn Python

What is a program?

This section covers:
What a program is
Syntax and reserved words
How to run Python code
Types of errors
Learning a programming language is similar to learning a foreign language, except instead of communicating with another person, you’re trying to communicate instructions to a computer.
A program is a sequence of instructions that have been crafted to do something.
Just like the English language, programming languages have a set of rules that you must follow.
Programming in Python
Different programming languages have slightly different rules. Today, we will be learning the rules of Python 3.
Syntax (Grammar)
Python has specific rules for how code should be structured. One important rule is that Python uses indentation to specify blocks of code.
Correct syntax
if 10 > 0:
print('Ten is greater than zero!')
else:
print('Ten is not greater than zero!')
Wrong syntax
if 10 > 0:
print('Ten is greater than zero!')
else:
print('Ten is not greater than zero!')
In the next sections, you’ll learn more about how to write syntactically correct Python code.
Reserved Words (Vocabulary)
Python’s vocabulary consists of a set of keywords that have special meanings. We call them reserved words. In the next section, we’ll learn about creating variables, which allow you to add your own words to Python’s vocabulary. You’ll be able to name your variables almost anything you want, but you cannot use any of Python’s reserved words to name your variables.
Here are the 30 reserved words in Python:
and del global not with
as elif if or yield
assert else import pass
break except in raise
class finally is return
continue for lambda try
def from nonlocal while
In the next sections, we’ll learn more about how to use these reserved words.
Python Interpreter
The Python interpreter is a program that reads, parses, and interprets each line of your source code on the fly. There are two ways in which you can run your Python code.
Write your code in a file.
This option is great if you want to save your code so that you can run it later. Python’s interpreter will run all the instructions in the file sequentially.
Write your code in the interactive console.
Using the interactive console is a great way to test out small bits of code that you don’t expect to save. You’ll need to enter each command separately.
⭐️ Step 1: Write and run your first “Hello, World!” program
Write your first program in the main.py file.
Write print('Hello, World!') in line 1 and then hit the green play button to run/execute your code.
You should see Hello, World! printed in the black console below the file.
Change the text in the quotes and run your program again.
Loading…
⭐️ Step 2: Run some code in the interactive console
The black console in the bottom is actually running a Python interpreter. You can type any Python command after the orange caret prompt.
Write print('Hello, World!') after the orange caret prompt and then hit Enter. You should see the program output Hello, World! just like before.
Try running a couple more print commands in the console.
Loading…
Types of Errors
You will almost never write a perfect program on the first try, so it’s important to know what kinds of errors you might see.
Syntax Errors
A syntax error occurs when you violate the “grammar” of Python. You’ll know you made a syntax error if the Python interpreter complains to you.
Example 1: Forgetting to use parentheses
>>> primt 'Hello world!'
File "<stdin>", line 1
primt 'Hello world!'
^
SyntaxError: invalid syntax
Example 2: Using a name Python does not know (primt is a typo of print)
>>> primt('Hello world')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'primt' is not defined
Example 3: Writing a command that Python does not understand
(To create a string, you would need to surround the text with single or double quotes.)
>>> I'm learning Python!
File "<stdin>", line 1
I'm learning Python!
^
SyntaxError: invalid syntax
Other Errors
Even if the syntax of your code is correct and the Python interpreter does not complain, it could still be “buggy” if the program is not doing what you intended. This could be because the statements are in the wrong order (logic error) or the statements simply do not do what you intended (semantic error).
Logic and semantic errors are harder to debug, but is a helpful tool because it visualizes what your program is doing on each step.
Mark the tasks below as done when you are finished, and then move onto the !
Task
Status
1
Write and run your first “Hello, World!” program
Not Started
2
Execute your “Hello, World!” command in the Python interactive console
Not Started
No results from filter

Next Section ➡️

Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.