Lesson 02: Spam, main(), optional params, keyword args, indexing, slicing, and Spam
This week we will discuss execution modes in Python with special emphasis on the use of a
main()
function to structure your programs.
You will also continue to expand your understanding of function fundamentals. Recall that a function
is a callable comprising a defined block of code that can be invoked to perform a specified
operation. A function definition can specify one or more parameters. Each parameter can also be
provisioned with a default value. In such cases, the caller is not required to pass
a corresponding argument unless a need exists to override the default value. You will also learn
that a caller can pass keyword arguments to a function. A keyword argument binds a value
directly to a specified key (i.e., parameter) in the form key=value
. Keyword arguments
can be passed in any order so long as positional arguments (if any) are passed first.
Team 506 will also show you how to access characters, elements, and items that comprise a string, list, or tuples. Each member of a sequence can be accessed by position if you know its index. Moreover, you can access a subset of a sequence by learning how to utilize Python’s slicing notation.
Finally, we’ll share a bit of history on why Python is named Python.
Readings
Below are a set of readings that you should consider reviewing. Note that the Real Python articles referenced below may require signing up for a free account.
1.0 Required
Eric Matthes, Python Crash Course, 3rd Edition (No Starch Press, 2023).
Chapter 03, Introducing Lists.
Read the entire chapter.
Chapter 08, Functions.
Continue exploring Chapter 08. Read the following sections:
- Passing Arguments
- Keyword Arguments
- Default Values
- Equivalent Function Calls
- Avoidig Argument Errors
- Return Values
- Making an Argument Optional
Lisa Tagliaferri, Understanding Tuples in Python 3 (Digital Ocean, Jan 2017).
💡 An English-language version of Lisa’s How to Code in Python 3 (DigitalOcean, 2018) is
available as a PDF or ePub in the Canvas Files texts
folder.
Lisa’s take on tuples.
Sergii Boiko, Python for Machine Learning: Indexing and Slicing for Lists, Tuples, Strings, and other Sequential Types (3 Oct 2018).
Despite the title, the article limits its examples to list indexing and slicing only. Review the section on indexing, both positive and negative. Then read carefully Sergii’s “Slice Notation” section. The examples he provides are particularly helpful when learning how to slice a list using Python’s slicing notation.
2.0 Recommended
Stephen Gruppetta, Using Python Optional Arguments When Defining Functions (Real Python, August, 2021).
Stephen discusses function design with a focus on optional parameters that are assigned default values.
Trey Hunner, Keyword (Named) Arguments in Python: How to Use Them (treyhunner.com, Apr 2018).
Solid overview of keyword arguments in Python.
Trey Hunner, Python String Methods to Know (Python Morsels, nd).
Trey highlights a dozen string methods that he recommends “committing to memory”.
Joanna Jablonski, Python 3's f-Strings: An Improved String Formatting Syntax (Guide) (RealPython, nd).
Johanna’s article provides a useful summary, emphasisizing in particular the formatted string literal approach, a.k.a “f-strings”.
Charles Severance, Python for Everybody. Exploring data in Python 3 (CreateSpace, 2016).
💡 An English-language version of Chuck’s book is available as a PDF or ePub in the Canvas Files texts
folder. Other translations and formats are available online.
Review the following chapters:
- Chapter 4, “Functions”
- Chapter 6, “Strings”
- Chapter 8, “Lists”
- Chapter 10, “Tuples”
John Sturtz, Lists and Tuples in Python (Real Python, nd).
Review the section on lists while reading closely the section on tuples, particularly tuple assignment, packing, and unpacking.
Lisa Tagliaferri, How to Define Functions in Python 3 (Digital Ocean, June 2016).
Lisa’s article overlaps with the Matthes chapter but review it nonetheless. In particular, have a
look at her section titled “Using main() as a Function”. She discusses the purpose of the main()
function and why defining a main()
function in the programs you write provides you with greater
control over code execution while also enhancing modularity and readability.
Lisa Tagliaferri, Understanding Lists in Python 3 (DigitalOcean, Nov 2016).
Lisa Tagliaferri, How To Use List Methods in Python 3 (DigitalOcean, Nov 2016).
Lisa’s tutorials provide a useful introduction to the list
data type and list methods such as
list.append()
, list.insert()
, list.remove()
, list.pop()
, list.copy()
, and list.sort()
.
Bryan Weber, Defining Main Functions in Python (Real Python, May 2019).
Bryan discusses the main()
function, the relevance of main()
to execution modes in Python, and
main()
function best practices. This article could prove challenging for the novice programmer.
Don’t worry; we will explore the rationale for writing programs that feature a main()
function in
class.
3.0 Reference
Eric Matthes, Beginner’s Python Cheat Sheet - Functions.
Eric Matthes, Beginner’s Python Cheat Sheet - Lists.
w3schools.com, Python Built-in Functions (w3schools.com, nd).
w3schools.com, Python Operators (w3schools.com, nd).
w3schools.com, Python List Methods (w3schools.com, nd).
w3schools.com, Python String methods (w3schools.com, nd).
w3schools.com, Python Tuple Methods (w3schools.com, nd).