Week 1: Hello World

Reading: Think Python Chapters 1 and 2

Notes

Setup

The Interpreter

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:

1 + 2
3
4/2 + 3
5.0

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:

'Hello, world.'
'Hello, world.'

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.

print("Hello, world.")
Hello, world.

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.

Text Interfaces

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

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.

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:

  • Comments start with a pound sign #.
  • Comments can be an entire line (if the # starts the line)
  • Comments can be inline (if the # comes after some valid code).
comments.py
print("Hello")
# this is a comment, it doesn't change how the code runs
print("world.") # this is also a comment

Practice

Practice Problem 1.1

Practice Problem 1.1

Make a small change to the following program so that the printed output is:

I have no
comment
no_comment.py
print("I have no")
# print("comment") 

Hint: The # on the second line is causing valid Python code to be ignored.

Practice Problem 1.2

Practice Problem 1.2

There’s a syntax error in the program below. Fix it so that the printed output is:

This program had a bug
had_bug.py
print("This program had a bug)

Practice Problem 1.2

Practice Problem 1.2

There’s an error in the program below. Fix it so that the printed output is:

Isn't this one tricky?
tricky.py
print('Isn't this one tricky?')

Homework

  • 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.

Homework Problem 1.1

Homework Problem 1.1 (50 pts)

Install an editor, write and save the below program, and make sure it runs with this output:

Hello, world!
hello_1012.py
print("Hello, world!")

Submit the program as hello_1012.py.

Homework Problem 1.2

Homework Problem 1.2 (50 pts)

Start with the program below, and write in the comments:

  • Affirmation that you have read the entire syllabus
  • Affirmation that you understand the grading scale
  • Affirmation that you understand the collaboration policy
  • Affirmation that you can attend both midterm exams in person
  • What you hope to get out of this course
agreed.py
# your comment goes here
# it will probably span many lines
print("I agree to adhere to all course policies and to the course schedule.")

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.