Week 01: Starting Python
Students read chapters 1 and 2 in Sweigart. Some scripts in this folder are based on Sweigart’s — naturally, he has more examples than only these.
This is the students' first look at Python. We use only Python 3.
- Interactively learning Python
- Do not use IDLE to run your scripts
- Chapter review: chapters 1 and 2
Interactively learning Python
We start running simple expressions and statements in the interactive Python interpreter (type
python3 at the bash prompt,
>>> fruit = 'apple' >>> print(fruit) apple >>> fruit = 'pear' >>> print(fruit) pear >>> 5 + 2 7 >>> sum = 5 + 2 >>> print(sum) 7
Until about halfway through chapter 1, Sweigart has us using the interactive Python interpreter, as seen above. Then he switches to writing a program in a file, which he assumes we will run in IDLE. However, we write our code in Atom and run it in Terminal (or PowerShell on Windows).
Do not use IDLE to run your scripts
You can run any of the scripts in this folder in the manner described below.
In the middle of chapter 1 in Automate the Boring Stuff with Python, Sweigart invites you to leave the interactive Python shell (where you have the
>>> prompt) and create a little program in a file.
Do not use IDLE. We are never using IDLE.
Instead, you can use Atom, our trusted code editor. Code is code. You can write any code in Atom. Instead of saving the file with a
.js extension, we save it with a
.py extension when it is a Python file.
Then, how do you run it? Never try to run a file at the
>>> prompt. You need to be at the bash prompt (
$) in Terminal, or the PowerShell prompt if you’re using Windows.
It’s easiest if you are in the same directory where the
.py file was saved. Use your
cd command (Command Line Tips) to get there.
At the bash prompt, type this (using your actual filename, of course):
And it runs!
Here’s where that comes in the chapter:
Here’s what the program looks like in Atom:
Here’s how I ran it in Terminal (I made a folder named
automate for my files from the book):
Chapter review: chapters 1 and 2
These are the takeaways from the first two chapters.
- Use the interactive Python shell to enter basic math expressions and get results (math operators)
23 / 7and
23 // 7
- Exponents, e.g.
2 ** 4
- Order of operations: PEMDAS
- Main data types: string, float, integer
- String concatenation, e.g.
'Alice' + 'Bob'
- String replication, e.g.
'Alice' * 5
- Assign a value to a variable with
- Rules for variable names: No spaces. Can use only letters, numbers, and the underscore (_) character. Cannot begin with a number. (p. 20)
- Case sensitive
- Comments start with
input()function — assign it to a variable:
answer = input()
float()functions — when we insert a numeral into a string, for example (pp. 25–27)
- The Boolean data type
- Comparison operators, e.g. greater than, less than
- Difference between
=(what do they do?)
- Boolean operators —
- Condition and block (p. 37ff) — blocks are indented
- Flow control statements:
ifstatements — they will evaluate to
breakstatements (p. 49)
continuestatements (p. 50)
- Press Control-C to break out of an infinite loop
- What is an infinite loop?
- “When used in conditions,
''(the empty string) are considered False, while all other values are considered True.” (Truthiness, p. 53)
randommodule, from Python’s standard library
- Use the
importstatement to import this module