Review of Python — Part 1B

Based mostly on the Tutorial, sections 1-4

2 Using the Python Interpreter

2.2.4 The interactive startup file

Initializing Python: PYTHONSTARTUP environment variable, if set, gives the name of a file containing start-up commands for the interpreter

2.2.2 Executable scripts

What is a script?

Making the script executable:

Command-line arguments for scripts

Running with arguments:

./script arg1 arg2 ...

Getting the arguments in the script:

import sys

sys.argv is an array of strings: [''] if just running the Python interpreter (no script); ['script', 'arg1', 'arg2', ...] otherwise, where 'script' is the exact name of the script used in the command to run it, e.g., './script1.py'

→ Example: script.py

3 An Informal Introduction to Python

3.1.2 Strings

Concatenation:

Repetition: s1 * n

Indexing: s1[0], etc.

Slicing:

s1[3:5]
s1[3:]
s1[:5]
s1[:] # (copies the whole of s1)

String Methods and Formatting

String methods are documented in the Python Standard Library documentation for the str type. You can also access this documentation by typing help(str) in Python.

There are two approaches to formatting in Python. One uses % as a formatting operator and is documented under printf-style String Formatting. The newer approach uses the Format class from the string module; see String Formatting. I haven't used the new method much.

Unicode strings

What is Unicode?

In Python 2, you had to prefix Unicode strings with u because strings used ASCII encoding by default: u'text' or u"text". In Python 3, strings are Unicode strings by default, so the u prefix is unnecessary—in fact, it would be a syntax error.

Example:

Python 2 version:
unicode2.py
Python 3 version:
unicode3.py

(Note that for non-ASCII literals, but not for the "\u" escape codes, the file must contain a "coding: " declaration in the first or second line.)

4 More Flow Control Tools

4.5 The pass statement

Control: the do-nothing 'pass' statement

for i in range(10):
    pass
def donothing ():
    pass
donothing() => None

None is a value (!) which means "no value" (!). E.g., it is the value of a function which returns no value. It is usually suppressed by the repl, but you can explicitly print it:

print(donothing())

4.6-4.7 Functions

Functions with fixed argument lists

Default values with optional and keyword arguments

"Catch-all" tuples for optional arguments

"Catch-all" dictionaries for optional keyword arguments

Unpacking tuples or dictionaries when calling a function

→ Example: funargs.py

Anonymous functions using the lambda form:

→ Example: lambdafunctions.py

Suggested Exercises

  1. Given string variables s1 = "a5d", s2 = "7z", and s3 = "elephant", write string expressions using the variables to produce these values:
    1. "a"
    2. "az"
    3. "a5da5da5d"
    4. "ele"
    5. "leph"
    6. "phant"
  2. Write a function with two required arguments, a and b, and an optional argument c with default value 0. All arguments are numbers. The function returns the value (2a)2 + 3b + c.
  3. Call the function …
    1. omitting the optional argument c.
    2. passing the value 5 for c as a positional argument.
    3. passing the value 7 for c as a keyword argument.

→ Solutions