-
Expressions:
- Operands and operators.
- Operator precedence and associativity.
- Non-container data types:
int
,
float
,
string
,
date
,
datetime
.
- Interactive use of IDLE.
-
Control structure:
-
Loops with
while
,
for
,
break
,
continue
,
else
.
- Loop until the user provides valid input.
- Loop until the end of a list or until the end of input.
- Loop a certain number of times by looping through the integers in a
range
.
-
Conditional statements with
if
,
elif
,
else
to steer the computer in one of several directions.
Abbreviations:
conditional
expressions,
assert
.
-
Raise and catch exceptions:
raise
,
try
,
except
.
-
Packaging:
-
Package statements in a
function.
Function definition,
arguments (positional and
keyword),
default argument value,
argument
unpacking,
return value.
Global vs. local variables.
-
Package functions and attributes in a class.
Class definition.
-
Package classes and functions in a module.
The
__name__
variable.
Import modules with
pip3
.
-
Sources of input:
- from the keyboard with
input
- from a file on the disk with
open
- from a URL with
urllib.request.urlopen
- from a child process via a pipe with
os.popen
- from the operating system’s environment variables with
os.environ
-
Destinations of output:
-
Standard output and standard error output with
print
,
or to an open file
-
Exit status with
sys.exit
- Graphical output with
tkinter
-
Formats of input:
-
CSV (comma separated values) format
-
JSON (JavaScript Object Notation) format
-
lines of text in none of the above formats
-
SQLite database file
-
binary (non-textual) input: gif, jpg, png.
-
Sequences
and other containers:
- numbers in a
range
- characters in a
string; indices.
- items in a
list;
indices.
- items in a
tuple;
indices.
- keys and values in a
dictionary
- dictionary
as a sparse list
- keys in a
set
- attributes
in an
object
- items
yielded
by a
generator
- Examples of processing
(using the word “list” to stand for various kinds of container):
-
Print each item in a list.
-
Pick a random value from a list.
-
Search for a given value in a list.
-
Find the largest or smallest value.
- Split a string into a list (of lines, of words, of characters).
- Create and
copy
a list;
list
comprehensions.
- Create a list of lists; rows and columns of data.
- Sum a list.
- Sort a list.
- Discard duplicates from a list.
- Find the union, intersection, and difference of lists.
- Convert between different types of lists
(e.g, convert string of characters into list of characters)
- Convert a list of bytes read from input into a list of characters.
-
A rare example of a loop without a list:
compute the value of
π.
Distinguishing features of Python
-
Syntax.
- Python uses indentation,
rather than a non-whitespace token such as
}
,
to delimit a
suite
(a block of statements or a control structure).
-
A Python function can accept
keyword
arguments
as well as the more conventional
positional
arguments.
-
A Python function can accept an argument that is a
lambda
function.
-
Control structure.
-
EAFP
error checking
(using
try
and
except
)
is favored over
LBYL
error checking
(using
if
).
-
for
and
while
loops can have an
else
clause.
-
for
loops are favored over
while
loops.
A
for
loop can loop through anything that is
iterable.
-
Data structures.
-
tuple
more efficient than a
list
.
- Often what we require is merely an iterable,
rather than specifically a
list
.
- Create list with a
list
comprehension
instead of repeatedly appending to an initially empty list.
Dictionary comprehension, set comprehension.
- Generator
function
containing a
yield
statement instead of a
return
statement, to avoid creating a
list
.
- Generator
expression
as an abbreviation for a generator function that consists of a yield statement
(possibly in an if statement) in a for loop.
-
Semantics.
- Many Python variables contain a
reference
to a value,
rather than the value itself.
Two or more variables can refer to the same value.