# 2.6. Summary¶

The same typographical conventions will be used as in Summary.

1. Object notation

1. When the name of a type of object is used as a function call, it is called a constructor, and a new object of that type is constructed and implicitly returned (no return statement). The meanings of any parameters to the constructor depend on the type. [Constructors]

2. object.methodName( parameters )

Objects have special operations associated with them, called methods. They are functions automatically applied to the object before the dot. Further parameters may be expected, depending on the particular method. [Object Orientation]

2. String (str) indexing and methods

See Summary for string literals and symbolic string operations.

1. String Indexing. [String Indices]

stringReference [ intExpression ]

Individual characters in a string may be chosen. If the string has length L, then the indices start from 0 for the initial character and go to L-1 for the rightmost character. Negative indices may also be used to count from the right end, -1 for the rightmost character through -L for the leftmost character. Strings are immutable, so individual characters may be read, but not set.

2. String Slices [String Slices]

stringReference [ start : pastEnd ]
stringReference [ : pastEnd ]
stringReference [ start : ]
stringReference [ : ]

A substring or slice of 0 or more consecutive characters of a string may be referred to by specifying a starting index and the index one past the last character of the substring. If the starting or ending index is left out Python uses 0 and the length of the string respectively. Python assumes indices that would be beyond an end of the string actually mean the end of the string.

3. String Methods: Assume s refers to a string

1. s.upper()

Returns an uppercase version of the string s. [Object Orientation]

2. s.lower()

Returns a lowercase version of the string s. [Object Orientation]

3. s.count( sub )

Returns the number of repetitions of the substring sub inside s. [Object Orientation]

4. s.find( sub )
s.find( sub , start )
s.find( sub , start , end )

Returns the index in s of the first character of the first occurrence of the substring sub within the part of the string s indicated, respectively the whole string s, s[ start : ], or s[ start : end ], where start and end have integer values. [Object Orientation]

5. s.split()
s.split( sep )

The first version splits s at any sequence of whitespace (blanks, newlines, tabs) and returns the remaining parts of s as a list. If a string sep is specified, it is the separator that gets removed from between the parts of the list. [split]

6. sep.join( sequence )

Return a new string obtained by joining together the sequence of strings into one string, interleaving the string sep between sequence elements. [join]

7. Further string methods are discussed in the Python Reference Manual, Section 2.3.6.1, String Methods. [Further Exploration]

3. Sets

A set is a collection of elements with no repetitions. It can be used as a sequence in a for loop. A set constructor can take any other sequence as a parameter, and convert the sequence to a set (with no repetitions). Nonempty set literals are enclosed in braces. [Sets]

4. List method append

aList.append( element )

Add an arbitrary element to the end of the list aList, mutating the list, not returning any list. [Appending to a List]

5. Files [Files]

1. open( nameInFileSystem )
open( nameInFileSystem , 'r' )

returns a file object for reading, where nameInFileSystem must be a string referring to an existing file.

2. open( nameInFileSystem , 'w')

returns a file object for writing, where the string nameInFileSystem will be the name of the file. If it did not exist before, it is created. If it did exist before, all previous contents are erased.

3. If infile is a file opened for reading, and outfile is a file opened for writing, then

infile.read()

returns the entire file contents of the file as a string.

infile.close()

closes the file in the operating system (generally not needed, unless the file is going to be modified later, while your program is still running).

outfile.write( stringExpression )

writes the string to the file, with no extra newline.

outfile.close()

closes the file in the operating system (important to make sure the whole file gets written and to allow other access to the file).

6. Mutable objects [Issues with Mutable Objects] Care must be taken whenever a second name is assigned to a mutable object. It is an alias for the original name, and refers to the exact same object. A mutating method applied to either name changes the one object referred to by both names. Many types of mutable object have ways to make a copy that is a distinct object. Zelle’s graphical objects have the clone method. A copy of a list may be made with a full slice: someList[:]. Then direct mutations to one list (like appending an element) do not affect the other list, but still, each list is indirectly changed if a common mutable element in the lists is changed.

7. Graphics

A systematic reference to Zelle’s graphics package, graphics.py, is at http://mcsp.wartburg.edu/zelle/python/graphics/graphics/index.html.

1. Introductory examples of using graphics.py are in [A Graphics Introduction], [Sample Graphics Programs], and [Entry Objects]

2. Windows operating system .pyw

In windows, a graphical program that take no console input and generates no console output, may be given the extension .pyw to suppress the generation of a console window. [A Windows Operating System Specialization: .pyw]

3. Event-driven programs

Graphical programs are typically event-driven, meaning the next operation done by the program can be in response to a large number of possible operations, from the keyboard or mouse for instance, without the program knowing which kind of event will come next. For simplicity, this approach is pretty well hidden under Zelle’s graphics package, allowing the illusion of simpler sequential programming. [Graphics.py vs. Event Driven Graphics]

4. Custom computer colors are expressed in terms of the amounts of red, green, and blue. [Custom Colors]

These techniques extend those listed in the summary of the previous chapter. [Summary]

1. Sophisticated operations with substrings require careful setting of variables used as an index. [Index Variables]

2. There are a number of techniques to assist creative programming, including pseudo-code and gradual generalization from concrete examples. [Creative Problem Solving Steps]

3. Animation: a loop involving small moves followed by a short delay (assumes the time module is imported): [Animation]

loop heading :
move all objects a small step in the proper direction
time.sleep( delay )
4. Example of a practical successive modification loop: [A Function to Ease the Creation of Mad Libs]

5. Examples of encapsulating ideas in functions and reusing them: [A Function to Ease the Creation of Mad Libs], [The Revised Mad Lib Program], [Animation]

6. Random results can be introduced into a program using the random module. [Random Colors]