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.
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).
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 .html
or .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):
python3 myfilename.py
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):
These are the takeaways from the first two chapters.
- Use the interactive Python shell to enter basic math expressions and get results (math operators)
- Compare
23 / 7
and23 // 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
#
print()
functioninput()
function — assign it to a variable:answer = input()
len()
functionstr()
,int()
, andfloat()
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
==
and=
(what do they do?) - Boolean operators —
and
,or
,not
- Condition and block (p. 37ff) — blocks are indented
- Flow control statements:
if
statements — they will evaluate toTrue
orFalse
else
andelif
statementswhile
loopsbreak
statements (p. 49)continue
statements (p. 50)for
loops withrange()
- Press Control-C to break out of an infinite loop
- What is an infinite loop?
- “When used in conditions,
0
,0.0
, and''
(the empty string) are considered False, while all other values are considered True.” (Truthiness, p. 53) - The
random
module, from Python’s standard library - Use the
import
statement to import this module - Use
random.randint()