Additional reading material - not in the book


Names are mutable untyped references

Names are mutable untyped references.
What does this mean?

Look at this exmple.


F2 - namespace and scoping

Do this with Special topic 2.1 in the book, at the end of section 2.2 It also deals with functions, which is in chapter 5

namespace

Look at Special topic 2.1 in the book. It shows several ways to import functions from a module.

First, what is a module? A module is just a file that contains Python code. You create a module when you save your code.

How do you use a module? One of the examples shown in Special topic 2.1 is

import math

That makes everything in the math file available. For example:

result = math.sqrt(2)

math is the module
. (dot) means contains
sqrt is the name of a function in the math module

Many standard modules are shipped with Python, such as math. Anaconda ships the standard modules and several more besides. You can create modules yourseld or find them elsewhere too.

There are a few (around 60 some) fnctions that are built-in and do not need to be imported. They are listed in: library functions

scoping

scope means where you can see something. In a Python function you can see a variable from where it is created to the end of the function. You cannot see variables that are in other functions.


New style formatting

In section 2.5.3 the book shows old style formatting. the old style works, but the new style formatting is replacing it. You should use the new style formatting in this course

The following is a good reference on old and new style formatting. You should learn the first section on basic formatting. There is a lot of stuff in this referenced page. Just read and use the other stuff need if you need it. Python formatting


I - Regular expressions

Clare Nguyen has provided slides for Perl regular expressions. Read these slides in Canvas.

We are using the Python implementation of Perl regular epressions. A brief listing of Perl5 regular expressions is given in: Perl 5 regular expressions summary A longer discussion is given in: Perl 5 regular expressions discussion


C2c - Loop control statements

In the book chapter 4 omits the continue loop control statement. continue is not used much. The following Tutorial Point page gives a summary of the break, continue, else, and pass control statements that can be used with loops. Tutorials Point - loop control


Argument reference passing

First consider the assignment operator.

var1 = 4     # sets var1 to reference the int object with a value of 4

var2 = var1  # sets var2 so it is a reference to the same object as var1
             # not a new object with the same value, but the same object.

Example: reference assignment

Now consider how arguments are passed to functions.

The references are copied from the argument in the calling code to the corresponding parameter in the function called.
Example: argument reference passing


E3 - Keyword, default, and variable length arguments

You may use both positional and keyword arguments when you specify a function. Put the positional arguments first. The Python 3 tutorial discusses this and gives examples. You probably can understand some of this discussion.

Python 3 tutorial on arguments


swap elements in a sequence

This is a better, more modern, way to swap elements in a list or other sequence.

a_list = [0, 1, 2, 3, 4]
a_list[3], a_list[2] = a_list[2], a_list[3]
print (a_list)

# result: [0, 1, 3, 2, 4]

A2c - Complex numbers

First, do you remember what a complex number is? You can review it with Sal Kahn's explaination of how we obtain complex numbers by adding real numbers and imaginary numbers. Introduction to complex numbers, created by Sal Kahn

Let's look at some examples in Python. Note that Python uses j for imaginary numbers, which is common in engineering, rather than i, which is common in mathematics. Stack overflow shows several good examples. complex numbers usage in Python

The Python Standard Library includes the cmath module. The cmath module includes Mathematical functions for complex numbers Python 3 cmath library document


G2 - Overloading operators and methods

operator overloading

This simple example of operator overloading is a good place to start. It is a simple example, but has one problem. When you add two Book objects together it would be good to get an answer that is also a Book object. This example get an answer that is an int object rather than a Book object. operator overloading

A somewhat more extensive place to look is more operator overloading

The text book does not talk about operator overloading, but the slides for the book do. Look at chapter 9 slides

function and metnod overloading

The idea of function and method overloading do not make a lot of sense in Python because function and method overloading deals with the function signature created when a function is declared, but functions and methods are not declared in Python.

There are more advanced techiques that provide Python methods that are rather like overloaded methods, but we are not going to look at this stuff in this course.