1 + 2
3
Reading: Think Python Chapters 1 and 2
The interpreter lets you type in a short amount of Python code to be immediately executed. It looks like this:
It is like a calculator, but much more powerful. Simple calculator-like math works the way you might expect.
We will show Python in these notes in a similar format:
Math and numbers work in the interpreter, but word-like text doesn’t. Trying it will give you an error.
To work with text in Python, you will need to encapsulate the text in quotation marks, single, or double:
The interpreter always displays the result of the expression you type in (if there is no error). This is different from using a print statement.
The difference between the string "Hello world."
as an expression and the print statement print("Hello world.")
is subtle when using the interpreter: quotation marks at the output.
The interpreter is a text interface to your computer. You type text, and the output displays text.
This is different from the graphical interfaces you are probably used to using, where there are buttons for you to click on, and you can drag and drop files.
Why use a text interface? It is more powerful. You can give your computer very specific instructions.
Graphical interfaces are designed to be easy to use, but they can only give the computer pre-defined instructions. If you want to solve a problem that hasn’t been solved before, you will probably need to use a text interface to succeed.
The editor lets you write full Python programs. A program is a series of instructions that can be saved for future use. The interpreter is a great tool for experimenting, but most of your coding will be done in the editor.
An editor for Python (or any programming language) has several useful features that a normal “text editor” doesn’t have.
Syntax highlighting changes the color of the text based on what it does in the program.
Line numbers help you organize your code, and help you find and fix errors.
Running programs directly from the editor makes your life easy. There’s a way to run programs from outside the editor, too, but we will save that for later.
Here are examples of short Python programs written in two different editors:
Visual Studio Code:
Thonny:
When you run this program, you’ll get printed output displayed at the terminal/shell:
Hello, world.
An important note: Python programs (not in the interpreter) only result in printed output when we instruct them to print with a print statement.
Here’s a program with just an expression, and no print statement:
The program will run with no errors, but there will be no printed output.
Homework problems should always be your individual work. Please review the collaboration policy and ask the course staff if you have questions.
Double check your file names and printed output. These need to be exact matches for you to get credit.
You may submit Homework 1 an unlimited number of times for full credit. You still do need to submit on time– don’t fall behind.
Start with the program below, and write in the comments:
agreed.py
Make sure the program runs without error. If you have some conflict with a course policy or the schedule, please speak with your professor before submitting this.
Submit the program as agreed.py
.
Comments
A Python program consists of logical instructions for the programming language to execute. Sometimes, however, we want to include English (or other natural language) text for human beings to read.
We include this text in comments:
#
.#
starts the line)#
comes after some valid code).