CS105 Study Guide

Site: Saylor Academy
Course: CS105: Introduction to Python
Book: CS105 Study Guide
Printed by: Guest user
Date: Thursday, July 18, 2024, 10:52 PM

Navigating this Study Guide


Study Guide Structure

In this study guide, the sections in each unit (1a., 1b., etc.) are the learning outcomes of that unit. 

Beneath each learning outcome are:

  • questions for you to answer independently;
  • a brief summary of the learning outcome topic;
  • and resources related to the learning outcome. 

At the end of each unit, there is also a list of suggested vocabulary words.

 

How to Use this Study Guide

  1. Review the entire course by reading the learning outcome summaries and suggested resources.
  2. Test your understanding of the course information by answering questions related to each unit learning outcome and defining and memorizing the vocabulary words at the end of each unit.

By clicking on the gear button on the top right of the screen, you can print the study guide. Then you can make notes, highlight, and underline as you work.

Through reviewing and completing the study guide, you should gain a deeper understanding of each learning outcome in the course and be better prepared for the final exam!

Unit 1: Introduction to Python 3

1a. Use an integrated development environment (IDE) to write simple programs

  • What is Python?
  • How can an IDE be used when practicing Python?
  • What are some conveniences when using a web-based IDE?

Every programming language must have some means, or environment, for constructing and executing programs. Python development environments are designed for practice and ease of testing so that a user can get immediate feedback and experience immediate results. After practice and testing, Python programs can be packaged and deployed for stand-alone applications using more sophisticated tools beyond what has been applied at this introductory level.

A host of integrated development environments (IDE) exist for practicing Python. Some require installation on a local computer. At the time of this writing, a few popular locally-installable environments include Microsoft Visual Studio and Jupyter Notebook. Other IDEs, such as Repl.it, are internet-based, require no installation, and can be invoked using a web browser. The Repl.it IDE has a command line window allowing for entry, practice, and testing of single Python instructions. In addition, the run window allows you to run and test Python scripts.

To review, see Introducing Python and The Repl.it IDE.

 

1b. Explain the int and float data types

  • What is the int data type and what is int data useful for?
  • What is the float data type and what is float data useful for?
  • What are the differences between the int and float data types?

Computers work by operating on data that is stored in memory. At the memory level, data is generally represented in the form of binary (that is, base 2) numbers. The method used to translate data from the IDE to binary memory data depends upon the data type. Differences between types of data arise precisely because they are represented differently in memory. While the details of data type representation is a subject reserved for more advanced courses, at the beginner programming level, it is still necessary to be aware of how to apply different data types.

There are several different Python data types, including int, float, and str. Since computers are often used to operate on numbers, it is sensible to begin by considering two fundamental Python numerical data types: int and float.

  • The term int stands for integer
  • The term float stands for floating point

It is recommended that, independent of any programming language, you fully understand the mathematical differences between integers and floating point numbers. After this, the programming aspects of using float data versus int data should fall into place.

To review, see Compare and Contrast int vs float and Scientific Notation for Floating Point Numbers.

 

1c. Explain variable assignment

  • What is variable assignment?
  • How does the Python assignment operation a=b differ from the mathematical expression a=b (that is, "a equals b")?
  • What are the important points to consider when choosing a variable name?

At the introductory level, one of the most important operations to understand is variable assignment. The assignment operator = is a powerful tool that allows you to modify the value of a variable. When used incorrectly, it can result in the loss of data due to unintentionally overwriting a value contained in a variable.

There are key elements to mastering variable assignment. First and foremost, data contained on the right side of an equals sign is assigned to the variable referred to on the left side of the equals sign. Always pay close attention to this "right-to-left" assignment convention. Second, choosing a variable name that reflects its intended use can increase the readability of your code. Third, be careful not to choose variable names that conflict with Python keywords. By employing each of these practices, you will have mastered variable assignment.

To review, see Variables and Assignment Statements, Reserved Words and Variable Naming Conventions, and Values, Types, Variable Names, and Keywords.

 

1d. Apply basic Python output using the print instruction

  • Why is the print instruction useful at this phase in the course?
  • What is the syntax for using the print instruction?
  • How can the print instruction be used for outputting multiple instances of data and text?

Assuming a given variable has been initialized, it is possible to output variable values using the Python command line by simply typing the name of the variable. However, when using the Python run window to run Python programs, this is not the case. To reveal the value of a variable, the print instruction can be used.

The print statement can be used to help find coding errors by providing the ability to check values. It can also be used to output the values of several variables at a time along with text data to output helpful messages to the screen. To demonstrate a full understanding of the print instruction, you must be able to accurately predict the screen output when a specific print instruction is invoked. An accurate prediction includes not just the values of variables, but also their arrangement and formatting.

To review, see Using Variables in Python.

 

1e. Explain the string data type

  • What is the str data type and what is str data useful for?
  • How does str data differ from int and float?
  • How can numerical data be converted into string data and vice versa?

The str data type is used to house strings of character data placed between to quotes. For example, '123&*()Python' is a character string of type str. Differences between numerical and string data must be respected or unwanted programming errors can occur. If you want to use string data that represents numerical data as a number, you must first convert it to a numerical form. For instance, the instruction float('3.9') converts the string '3.9' to a numerical floating point value. The instruction int('25') converts the string '25' to an integer value. Conversely, str(5.43) converts the floating point value 5.43 to the string '5.43'. Data type gymnastics of this kind are necessary because data is not always available in the desired form. Therefore, you must sometimes take additional steps to perform the appropriate conversions.

The Python type command is indispensable for determining the data type of a variable. Assuming you have mastered variable data types, you should be able to accurately predict the output of an instruction such as print(type(a)) for some variable a. Learning how to program involves both analysis and design. Before mastering the program design phase, you must first master the analysis phase. One important facet of program analysis is to be able to predict the end result after a snippet of code runs. Therefore, "accurately predicting" does not mean guessing at what you think may be the output. Instead, it means putting yourself in the reference frame of a computer and mentally executing each instruction as they are read in their proper sequence.

To review, see The Basics of Strings.

 

Unit 1 Vocabulary

Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.

  • integrated development environments (IDE)
  • command line
  • run window
  • data types
  • numerical data types
  • string data
  • int
  • float
  • str
  • variable assignment 
  • assignment operator
  • print

Unit 1 | Study Session Review

Unit 2: Operators

2a. Explain Python arithmetic operators, relational operators, logical operations, and the bool data type

  • How do arithmetic operators differ from relational operators?
  • How do logical operators differ from relational operators?
  • What are the truth tables for the logical AND and logical OR?

Computer languages need a host of arithmetic operators that can emulate any mathematical expression so they can perform computations. Every arithmetic operator provided in Python is of equal importance, and you need to understand each one. Do not fall into the trap of knowing some but not fully understanding others. The same holds true for relational and logical operators.

The bool data type can only take a value of either True or False. Make sure to practice combinations of logical and relational operators in order to predict their outcome. To do this, it is important to know the truth tables for logical operations such as OR and AND:

A

B

A OR B

False

False

False

False

True

True

True

False

True

True

True

True

 

A

B

A AND B

False

False

False

False

True

False

True

False

False

True

True

True


Take care to summarize the meaning of the logical OR and logical AND. It is important to understand the truth tables for these operators, but you should also try to develop an intuitive understanding of their meaning. The logical AND between two values can only evaluate to True if both values are True. The logical OR between two values can only evaluate to False if both values are False. These summary statements can be used as "shortcuts" when evaluating combinations of logical and relational operators.

To review, see Practice With Arithmetic Operators, Practice with Relational and Logical Operators, and The bool Data Type.

 

2b. Explain operator precedence

  • What is PEMDAS?
  • In the absence of parentheses, in what direction should an expression with multiple operators be evaluated?
  • What is the purpose of operator precedence?

The general idea behind operator precedence is that sequences of operations should be consistent with basic mathematical rules of algebraic expressions. This is the point of PEMDAS: parentheses first, then multiplication or division before addition or subtraction. The programming of an arithmetic expression should reflect our expectation of the outcome of the expression. If you are comfortable with evaluating algebraic expressions, the rules for programming should be a natural extension of your basic mathematical training. If this is the case, then programming arithmetic expressions follows in a very logical manner.

The leap to integrating relational and logical operators into operator precedence follows from the rules of Boolean algebra. Since most introductory programming courses do not cover Boolean algebra to any great depth, it is best to memorize the operator precedence for logical and relational operators.

To review, see Practice With Operator Precedence.

 

2c. Construct programs that apply variable types, operators, and operator precedence

  • How can using combinations of operators be useful for programming?
  • Why is it important to be aware of data types when combining various operators?
  • Why is operator precedence a crucial topic when it comes to programming?

It is all well and good to understand operators when applied at the level of evaluating an expression. However, this is a programming course and our goal is to construct programs that apply data types and combinations of operators. Strict attention must be paid to data types and operator precedence in order to guarantee a desired result. For example, the difference between using floating point division (/) and integer division (//) can be substantial. Understanding practical applications of the modulo (%) operator such as finding the divisor of an integer should not be minimized. Finally, you should be able to accurately predict the outcome of a series of instructions involving variables, operators, and operator precedence. For example, given the following combination of arithmetic, logical, and relational operators:

a=True
b=10
c=3.0
y=b%c<=5 and not a or b//5>1
print(y)

you should be equipped to conclude that a True would be printed out to the screen because b//5>1 is True. Even though the AND truth table tells us that b%c<=5 and not a is False, the OR truth table tells us that False or True evaluates to a value of True.

To review, see Operators and Expressions.

 

Unit 2 Vocabulary

Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.

  • arithmetic operators 
  • relational operators
  • logical operators
  • bool data type
  • truth tables
  • operator precedence
  • PEMDAS
  • floating point division
  • integer division
  • modulo operator 

Unit 2 | Study Session Review

Unit 3: Input and Flow Control Statements

3a. Explain the differences between programmer-initialized variables and user input variables

  • What is a programmer-initialized variable?
  • What is a user input variable?
  • How are user input variables and programmer-initialized variables applied when programming?

All programs require the use of variables that must be initialized to some value using the assignment operator (=) in order to use them within a program. After a variable has been initialized it can be modified using the assignment operator if desired.

A programmer-initialized variable is initialized within a program; hence, its value is known simply by looking at the code.

The input command can be used to create a user input variable whose value is not known until it is entered by the user.

Both applications have their place within a program. User input variables are used when data must be entered by someone using the program (such as by entering a password). Programmer-initialized variables are set by the programmer to a specific value (such as a=35 or s='hello').

To review, see The Input Command.

 

3b. Write a program that will take string and numerical data from the keyboard

  • What is the basic syntax of the input command?
  • What data type does the input command return after executing?
  • What care should be taken if numerical input data is desired?

Since the input command is used to allow for user input, it is sensible to provide a message to let the user know what is required for the input (such as indata=input('Enter your name: ')). The input command will suspend program execution until the user hits the return key. After this, the destination variable will contain the contents of what was entered by the user.

The input command returns data of type str. If numerical data is required from the user input, it must be converted to the desired data type. For example:

x=float(input('Enter the number of seconds: '))

would convert the input data to a floating point value. If the conversion step is neglected, then instructions following the input statement that assume numerical data as the input would be operating on string data. This would most likely result in some type of exception.

To review, see The Input Command.

 

3c. Write conditional statements using logical operators

  • What is the syntax for an if statement?
  • How are if statements used?
  • How can else and elif clauses be of use?

if statements are extremely important when it comes to computing because they allow decisions to be made. 'if' decisions are made by evaluating Boolean expressions. This is a major reason why expertise with relational and logical expressions is so necessary. If a Boolean condition evaluates to a value of True, then the code contained within an if statement will execute; otherwise, it will not.

Python uses indentation to indicate all instructions to be executed in the event that the if condition is True. Furthermore, if statements look like:

if condition:

Indented Python code to execute if the condition is True

Always remember to put a colon (:) after the if condition.

In addition to the if, Python allows for else and elif clauses to be created if multiple conditions and decision outcomes are required. When putting together elif statements, it must be understood that only one potentiality will execute in the event that some elif condition is True. Therefore, when encountering a complex looking series of elif statements, most of the code will not execute. Only the path containing a True condition will be taken and the others will be ignored.

To review, see if, else, and elif Statements.

 

3d. Write for loops and while loops using logical operators and the range function for flow control

  • How are for loops used?
  • How are while loops used?
  • What is the difference between a for loop and a while loop?

Loops are a helpful programming construct because they efficiently allow for a series of operations to be performed over a desired set of iterations. for loops generally iterate over a set range of values. while loops generally iterate while a specified logical condition evaluates to a value of True. Both types have their place within the Python programming language.

The syntax for for loops and while loops is similar to the if statement in that the colon (:) is used to indicate the loop statement and indentation is used to house instructions contained within the loop. Predicting how a variable changes as a loop iterates must be part of your skill set as it indicates an understanding of how a loop is working. Accurately predicting how a loop is working means that you are able to mentally execute each iteration and, in some sense, visualize or track how the value of a variable changes during or at the end of each iteration.

To review, see Using "while" Loops and Using "for" Loops.

 

3e. Explain how break, continue, and pass statements are used in loops

  • How is the break instruction used?
  • How is the continue instruction used?
  • How is the pass instruction used?

When writing loops, it may be necessary to interrupt the program flow based upon some condition being satisfied. The break statement allows you to exit a loop without having to iterate to its terminating condition. Be careful when applying the break statement within an inner loop of a nested loop, since only the inner loop will terminate and the outer levels will continue to execute.

The continue statement, at the time it executes, will disregard any remaining instructions within a loop and flow to the next iteration of the loop currently being executed. continue statements and break statements are commonly used in conjunction with if statements such that, when a condition is satisfied, the break or the continue statement can be invoked.

The pass instruction effectively does nothing. It is often used to take the place of code not yet integrated into a program. It allows you to construct, for example, the skeleton of the loop with the internal loop details reserved for later development.

To review, see "break", "continue", and "pass" Statements.

 

Unit 3 Vocabulary

Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.

  • programmer-initialized variable
  • user input variable
  • input command
  • iterations
  • for loops
  • while loops
  • break statement 
  • continue statement 
  • pass instruction 

Unit 3 | Study Session Review

Unit 4: Data Structures I – Lists and Strings

4a. Explain lists and indexing

  • What is a list?
  • How are elements within a list accessed?
  • What are some useful applications of lists?

A list is a Python container that houses a mutable sequence of values. The term sequence means that the elements contained within the list are ordered and can be referred to using an index. In contrast, Python data structures such as sets and dictionaries are not sequences.

Lists can contain elements that are of any valid Python data type. List elements start with an index value of 0. Therefore, if a list contains 15 elements, element indices range from 0 to 14. It is extremely important that you master how to index elements within a list.

An empty list (containing zero elements) can be created with a command such as

alist = list()

or

alist = [ ]

using the standard left square bracket-right square bracket notation to build the list container.

Lists can be used to house records that contain different data types. For example, a list could contain information such as a name (of data type str), salary (of data type float), room number (of data type int), and so on. Because of the indexing property, lists can be applied in tandem with loops in order to generate a computation using the list elements.

To review, see Creating Lists and Indexing.

 

4b. Write simple programs that apply list and string methods

  • What is the syntax for invoking a list or a string method?
  • What does the append method do?
  • What does the join method do?

The true beauty of Python is that every variable referred to in a program is an object derived from a specific class. This means that every object is endowed with the methods defined within the object's class. The syntax for referring to a method uses the dot notation. For example:

alist=[5,7,9,11]
alist.append(30)

will call the append method in order to append a value of 30 to the variable alist.

Most Python editors within an IDE will enable you to view methods associated with a given object after typing the dot after the variable name. This feature can be used to help you remember the set of methods and can help you to complete typing a given command. For example, in Repl.it, typing

z='a string'
z.

will generate a temporary window listing the set of possible built-in string methods. For example, the find method will search for a substring contained within the string variable. The join method will join together strings in order to form a new string. As you write more Python programs, you will naturally become familiar with the host of built-in Python methods associated with strings and lists.

To review, see Indexing, List Methods, and String Methods.

 

4c. Explain and apply slicing

  • What is slicing?
  • What is the syntax for slicing?
  • How can slicing be applied within a Python program?

Slicing is an efficient way of indexing elements within a Python sequenced container. For a given list variable alist, it is more efficient to write alist[3:8] rather than individually referring to elements as alist[3], alist[4], alist[5], alist[6], alist[7].

The general syntax for applying slicing to a list is:

listname[start index: stop index: step]

where the step refers to how many elements to skip over to reach the next element being referred to. In addition, Python index convention causes the slice to terminate at one less than the stop index. This is a feature that beginner programmers must pay close attention to.

Slicing allows for easy indexing of large numbers of elements. Assuming the initialization:

alist = [3,5,7,9,11,13,15,17,19]

these two snippets of code are equivalent:

zlist=[ ]
for i in range(2,6):
    zlist.append(val)

and

zlist=[ ]
zlist=alist[2:6]

Clearly, slicing has eliminated the need for the loop.

To review, see Slicing.

 

4d. Write programs that plot and visualize list data

  • What is the import command?
  • What is matplotlib?
  • What is the syntax for plotting one list of data against another list of data?

Python is an amazing language because there are so many modules that are available for computations that go far beyond the basic language. In order to include these modules and access their methods within a given Python program, the import command must be invoked. matplotlib is an example of a module that can very readily enable the plotting of data. To use the matplotlib module, you need to invoke the command

import matplotlib

The matplotlib module is equipped with a number of methods that allow for the rendering of data and annotating graphs. A basic use case of this module is to plot the data contained in numerical lists. A sequence of commands such as:

import matplotlib.pyplot as plt
x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
z=[2,4,6,8,10,12,14,16,18,20]
plt.plot(x,z)
plt.show()

will plot the data contained in list z against the data in list x.

To review, see The matplotlib Library.

 

Unit 4 Vocabulary

Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.

  • list
  • container 
  • mutable
  • sequence
  • elements
  • index
  • slicing
  • modules
  • matplotlib
  • import

Unit 4 | Study Session Review

Unit 5: Functions

5a. Analyze situations where using functions could improve a program

  • How are functions used?
  • When should a function be created?
  • What is the difference between a user-defined function and a built-in function?

A function is created when a given task is performed or a set of instructions are utilized at several different points throughout a program. If this happens, you should consider wrapping the task or set of instructions into a function or a method. Functions and methods can be called and used over and over again at any location within a program without having to reproduce the code. The code resides in the function definition. All that needs to happen is to execute an instruction that refers to or 'calls' the function. Using functions in this way makes programs more readable and more efficient.

There are two main types of functions: built-in and user-defined. Python built-in functions are provided as part of the language. Functions such as min, max, sum, and len are examples of built-in functions. User-defined functions are written by the programmer and require adhering to the rules of the Python language syntax which makes provision for creating new functions.

To review, see Creating Functions.

 

5b. Explain how functions are defined and how to use function syntax

  • What are the syntax rules for creating a function?
  • What are the syntax rules for calling a function?
  • What Python instruction allows for providing output from the instruction?

There are specific rules for creating user-defined functions:

  • The Python keyword def is used to indicate the definition line of a function.
  • def is followed by the name of the function.
  • Input variables are separated by a comma and contained within parentheses following the function name.
  • If there are no input variables, then left-right parentheses must still be used with no arguments in between.
  • The right parenthesis is followed by a colon (similar to if and loop statements).
  • Instructions contained within a function are indented and obey typical rules for indentation.
  • Any data to be output by the function is made available to the calling function using a return

There are specific rules for calling a function:

  • The function name must be used.
  • Input variables are contained within parentheses following the function name and are separated by a comma.
  • If the function requires no input variables, then left-right parentheses must still be used with no arguments in between.

To review, check all examples of functions introduced in this course to be sure that the above rules have been adhered to and you are completely certain of their application.

To review, see Creating Functions.

 

5c. Use functions to better organize programs written in Units 2-4

  • What are some examples of user-defined functions applicable to Units 2-4?
  • What are some algorithms identified in these units that can be phrased within a function?
  • Why are programs better organized after phrasing code introduced in Units 2-4 within a function?

The three major examples of algorithms we introduced were finding the minimum value contained within a numerical list, finding the maximum value contained within a numerical list, and summing the values contained within a numerical list. Python already has built-in functions to perform these operations (min, max, and sum). However, it is best to know how to create user-defined functions to perform these operations in the event that you need to modify them in some way. They also are simple examples for learning how to program.

After you practice with the syntax for creating such user-defined functions, it should be clear that it is much more efficient to have, for example, a callable sum instruction. Such an instruction can be called anywhere within a program. The need to reproduce the actual code several times is eliminated. This is the beauty of creating user-defined functions. Programmers are given the power to create their own user-defined, reusable instructions that enable clearer program organization.

To review, see Creating Functions.

 

5d. Apply functions and methods available from the Python math library

  • What is the syntax for invoking the math module?
  • What are some useful methods contained within the math module?
  • What is the syntax for calling a method from the math module?

The math module contains a host of methods necessary for computation. Invoking the math module requires the import math instruction. After this, methods can be called using the dot notation. For example, math.sqrt(5), math.log10(50), and math.cos(math.pi) are all valid method calls.

The math module can be used to make computations similar to that of a calculator such as trigonometric functions, square roots, powers, exponentials, and logarithms. As part of your basic skill set, you must be able to take a simple equation and translate it into Python code by applying methods from the math module. The basic guiding premise is that, if a calculation can be entered into a calculator, it is possible to translate it into Python code.

To review, see Trigonometry Review and The "math" Module.

 

5e. Apply functions and methods for generating and applying random numbers

  • What is the syntax for invoking the random module?
  • What are some useful methods contained within the random module?
  • What is the syntax for calling a method from the random module?

The random module is useful to this end and can be invoked using the import random instruction. As with other Python modules, method calls follow the same dot notation convention used in object-oriented programming. Examples of random method calls are random.seed(30), random.random() , random.randint(0,20) and random.shuffle(z) (where z is a list). In order to shorten the instruction length, the random module is often imported using the as qualifier:

import random as rn

The previous examples would then look like: rn.seed(30), rn.random() , rn.randint(0,20) and rn.shuffle(z).

The random module can be used to generate random numbers for simulating random events. The most common model at the beginner level is the uniform random distribution, which can be applied using the random method. Many more advanced distributions are also available such as the normal distribution (or Gaussian distribution) using the gauss method. It is also possible to generate random integers over a specified range using the randint method. Another useful method is shuffle, which can randomly shuffle elements contained within a list. Advanced understanding of probability and statistics not addressed in this course will allow you to explore deeper programming aspects of the random module.

To review, see Random Numbers.

 

Unit 5 Vocabulary

This vocabulary list includes terms listed above that students need to know to successfully complete the final exam for the course.

  • function
  • built-in functions
  • user-defined functions
  • def
  • input variables 
  • indentation
  • output
  • return
  • import
  • math module
  • random module
  • uniform random distribution
  • normal distribution (or Gaussian distribution)

Unit 5 | Study Session Review

Unit 6: Basic Data Structures II – Tuples, Sets, and Dictionaries

6a. Explain the difference between lists and tuples

  • What is a tuple?
  • How are lists and tuples similar?
  • How do lists and tuples differ from one another?

A tuple is an ordered, immutable Python container. This means its elements can be referred to using an index (similar to a list), but once initialized to a set of values, its elements cannot be modified (i.e. they are immutable). This is the main difference between tuples and lists. In other words,

a=[2,3,4]
a[1]=12

is allowed because lists are mutable; however, after creating the tuple

b=(2,3,4)

the instruction

b[1]=12

will generate a TypeError exception because tuples are immutable.

In some sense, tuples allow you to have a list of data that remains constant. Although the syntax for accessing tuple elements is the same as that of a Python list, the set of methods associated with lists and tuples are different (with only a very small degree of overlap). For instance, because of immutability, the append method is not available to use with tuples. Tuples are useful when you want to order data elements so that they can be indexed, but you do not want the data to be changed. For example, if a set of birthdates were to be stored, a tuple could be a sensible choice to house this type of data.

To review, see Tuples and Sets.

 

6b. Explain sets and apply set operations

  • What is a set?
  • How do sets differ from lists and tuples?
  • What is the syntax to create a set?

A set is an unordered, mutable Python container. Mutable means that a set can be modified. Unordered means that elements cannot be accessed using an index. In addition, a set does not contain duplicate elements. The properties associated with different Python built-in containers can be summarized in a table:

Container

Ordered

Mutable

List

yes

yes

Tuple

yes

no

Set

no

yes


An empty set can be initialized using an instruction such as

a=set()

(notice that you should not use a={ } to initialize a set because this indicates an empty dictionary).

A non-empty set can be initialized with an instruction such as

a={3,4,5,6}

After creating a set, the remove and add methods can be useful for removing elements from or adding elements to a set.

Understanding basic set operations such as membership, non-membership, union, intersection and set difference is fundamental to applying Python set operations and set methods. The syntax in and not in can be used to determine set membership. The methods union, intersection, and difference naturally correspond to their associated set operation.

To review, see Tuples and Sets.

 

6c. Create dictionaries using dict

  • What is a dictionary?
  • How do dictionaries differ from lists and sets?
  • What is the syntax to create a dictionary?

A dictionary is an unordered, immutable Python container whose elements consist of key-value pairs. The immutability property needs a bit of clarification as it refers to the key and not the value. Values can be modified, but keys cannot. In addition, key:value pairs can be added to or deleted from a dictionary.

An empty dictionary can be created in one of two ways using an instruction such as:

a=dict()

or

a={ }

In addition, a dictionary can be initialized using an instruction such as:

a={5:22, 'x':34, 'l':'mno'}

Similar to a set, the key:value pairs in a dictionary are not ordered. In addition, the container syntax using curly brackets is similar to a set. Therefore, the usage context must resolve any confusion in the syntax.

Once a key name is chosen, it cannot be modified. This is because the key acts like an index into the dictionary. So, even though elements in a dictionary are not ordered, the key allows for referencing values with square bracket syntax similar to a list or a tuple. For example, values in the dictionary

a={1:'Tom', 2:'Frank', 3:'Robert'}

would be referred to using a[1] (referring to the value 'Tom'), a[2] (referring to the value 'Frank'), or a[3] (referring to the value 'Robert').

Finally, it is important to know how to either add or remove key:value pairs from a dictionary. key:value pairs can be added by using the assignment operator (=) or the update method and they can be removed using the pop method. To summarize, creating dictionaries and modifying their contents is fundamental to applying Python dictionaries.

To review, see Dictionaries.

 

6d. Write programs that apply tuples, sets, and dictionaries

  • How can loops be used with dictionaries, tuples, and sets?
  • How can functions be used with dictionaries, tuples, and sets?
  • How do sets and dictionaries differ from using tuples within a loop?

A major application of using containers is to access and iterate through their elements using a loop. Tuples and lists are ordered, hence, if desired, loops can be iterated based upon their indices. The beauty of Python is that the built-in containers list, tuple, set, and dict can all be iterated upon by referencing their elements. For example, the instruction

for x in a:

could be used to iterate assuming the variable a is a valid list, tuple, set or dictionary. For a dictionary, it must be clear from the context that the iterator variable x would refer to a dictionary key (and not the value). To review, the key acts like an index; therefore, instructions such as

for x in a:
    print(a[x])

would output values from key:value pairs (assuming the variable a is a dictionary).

When writing functions, any valid data type, either built-in or user-defined, can be used as a valid input to or output from a function. This is an important principle for programming languages. Ideally, once the syntax is in place, it should be generalizable, without modification, to all data types. The for loop example above highlights this principle. The same holds true for functions. It is the responsibility of the programmer to understand the context of the data type being used in order to properly apply programming constructs such as loops and functions.

To review, see Dictionaries and Loops.

 

Unit 6 Vocabulary

Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.

  • tuple 
  • immutable
  • index
  • list
  • set
  • mutable
  • containers
  • dictionary
  • key-value pairs

Unit 6 | Study Session Review

Unit 7: File Handling

7a. Use file handling and file handling modes to read and write to text files

  • What are the basic steps required for handling a file?
  • What Python methods accomplish these basic steps?
  • What is the Python syntax for reading from and writing to a file?

To perform file handling, regardless of the programming language, there are some basic steps that must take place. First, a file must be opened by referring to the correct path, file name, and file extension. Once opened, the desired file handling mode (such as read or write) must be specified. Finally, once a program is finished accessing a given file, it must be closed.

These steps translate almost directly into the method calls that implement them. The open method opens a file along with choosing the file handling mode. Read ("r") and write ("w") are the most basic ways of performing desired operations. Finally, the close method closes the file.

A basic instruction sequence for a file read, assuming filename.txt exists, would look like:

myfile=open('filename.txt','r')
x=myfile.read()
myfile.close()

If the file handling mode is omitted, the default mode for the open method is read ('r').

A basic instruction sequence for writing text to a file, would look like:

wtxt='12345'
myfile=open('filename.txt','w')
myfile.write(wtxt)
myfile.close()

Both of these examples highlight how to open a file, how to read from or write to a file, and how to close a file. All file handling operations utilize this basic structure.

To review, see Syntax and Usage.

 

7b. Write programs that use file handling modes, such as reading from, writing to, appending, and creating files

  • What is the syntax for creating a file?
  • What are the main differences between writing and appending?\
  • What are some basic instruction sequences useful for file handling?

In addition to writing to a file, the ability to append data to a file must also be a possibility. It is important to understand the basic similarities and differences between writing and appending.

  • If a file does not exist, the open method will
    • Generate a FileNotFoundError exception if read mode ('r') is used
    • Create the file if either write ('w') or append ('a') mode is used

So, both the write and append modes have the ability to create a file. Once created, the file is empty and contains no data. While the newly created file is still opened, either mode 'w' or 'a' will allow for writing multiple instances of data to the file and allow multiple calls of the write method.

When dealing with files that already exist, then some care must be taken.

  • If a file does exist, the open method will
    • Create an empty file if 'w' is used, thus, obliterating any previous contents contained in the file
    • Allow for appending data to the file (if the 'a' file handling mode is used) using the write method.

For example, if filename.txt does not exist, then

wtxt1='12345'
wtxt2='6789'
myfile=open('filename.txt','w')
myfile.write(wtxt1)
myfile.write('\n')
myfile.write(wtxt2)
myfile.close()

and

wtxt1='12345'
wtxt2='6789'
myfile=open('filename.txt','a')
myfile.write(wtxt1)
myfile.write('\n')
myfile.write(wtxt2)
myfile.close()

are equivalent. On the other hand, if filename.txt does exist, then

myfile=open('filename.txt','w')

will create a new empty file while

myfile=open('filename.txt','a')

will not destroy the existing contents and data will be appended using the write method.

To review, see Syntax and Usage.

 

7c. Write programs that using file handling methods

  • Name some applications where file handling can be useful.
  • What methods can be helpful when parsing data read from a file?
  • How can loops be used to help read file data?

 File handling is extremely useful for reading large amounts of data stored in a file. Your word processor and spreadsheet application continually perform file handling in order to save and open documents. When reading large amounts of data, it may become necessary to read or write data a single line at a time rather than using one single read or write. For example, using a for loop such as:

myfile = open('filename.txt')
for line in myfile:
    print(line)
myfile.close()

will read and print out each line contained within the file 'filename.txt'. The readline method is also useful for reading single lines as well. Finally, if a line of text is read that has multiple values that need to be converted to numerical data, the split method is indispensable. For example, if a file source.txt contains the data:

2 4
3 5
4 6

the following code

myfile = open('source.txt')
for line in myfile:
    line = line.rstrip()
    a=line.split(' ')
    print(int(a[0])*int(a[1]))
myfile.close()

would generate the output

8
15
24

In this example, the split method splits each line using the whitespace character ' '.

To review, see Syntax and Usage.

 

7d. Apply file handling to the data analysis and visualization programs written in Units 3-6

  • How can file handling be useful for data visualization?
  • What file handling methods can be applied when handling large amounts of data?
  • What basic Python data structure can be used to house data read from a file?

Data visualization is yet another application where file handling can be important when the amount of data to be rendered is large and keyboard entry is not a possibility. Depending on the ordering of how the data is stored, methods such as readline and split can be very useful for parsing data into lists and then can be plotted using methods from matplotlib.

So-called 'space-delimited data' is configured assuming each value on a given line is separated by a blank space ' '. This is partially true for data files with the extension '.csv' (commonly used for storing spreadsheet data). For example,

10 33
22 64
34 15
39 12
74 95

is an example of space-delimited data. Assuming matplotlib is imported, the following set of instructions could be used to create lists from this data stored in a file 'filename.txt' that could be plotted:

x=[ ]
y=[ ]
myfile = open('filename.txt')
for line in myfile:
    line = line.rstrip()
    z=line.split(' ')
    x.append(float(z[0]))
    y.append(float(z[1]))
myfile.close()

To review, see Data Visualization from a Data File.

 

Unit 7 Vocabulary

Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.

  • read
  • write
  • append
  • split method
  • matplotlib

Unit 7 | Study Session Review

Unit 8: Regular Expressions

8a. Explain why and how regular expressions are used

  • What is a regular expression and what is the re module?
  • What is the compile method used for?
  • Name some special characters useful for forming regular expression,

A regular expression is a string containing combinations of characters that represent patterns contained within other strings. Once constructed, a regular expression search pattern can be used to search other strings for the pattern. The re module contains a number of methods useful for applying regular expressions in Python. As part of this module, the compile method is useful because it converts a regular expression pattern to a highly efficient form for searching. Other important methods include match and search.

There are many special characters for regular expressions that should be reviewed. But, at the introductory level, it is important to master certain special characters useful for forming regular expressions such as ., *,+, [ ], and \. The . stands for an arbitrary character, the * stands for zero or more repetitions, the + stands for one or more repetitions, the [ ] stands for a class of characters, and the \ acts as a control character to indicate an re special character is to be used as an actual character to be searched for. As you master these, you can add others to your arsenal to form more sophisticated searches.

To review, see Syntax and Usage.

 

8b. Use regular expressions to construct search patterns to match a string or set of strings

  • What does the search method do?\
  • What does the match method do?
  • Write some simple regular expressions that can be used to search with.

It is important to know the difference between re.search() and re.match(). re.match() only looks for a pattern match at the beginning of a string. re.search() looks for the first match within a string (which could be at the beginning of a string). These are two of the most basic methods for searching with regular expressions.

Learning regular expressions is like learning a new language and much practice and reinforcement is required. Consider the regular expression

'.*\^\d\s[b-p]*'

and consider the set of possible strings that could match this pattern. The .* means zero or an arbitrary number of repetitions of any character. The \^ means look for a ^ (notice, the \^ means do not use ^ as a special character). The \d means look for a decimal digit and \s means whitespace. Finally, [b-p]* means zero or an arbitrary number of repetitions of any character from b to p in the English alphabet. Convince yourself that any of the following strings

s='asdfgh^9 '
s='asdfgh^9 bcdmno'
s='^9 bcdmno'

would generate a match for this regular expression

pattern='.*\^\d\s[b-p]*'
print(re.search(pattern, s))

To review, see Delving Deeper.

 

8c. Solve common tasks by using regular expressions to match patterns

  • How do we iterate regular expression pattern searches through a string?
  • How does the finditer method work?
  • What useful information is available from the finditer method?

A major theme regarding Python programming is, once a problem can be solved for a single pass, how can it be extended to multiple passes? For example, once a for loop could be used to iterate on a list, you saw a natural extension of the loop statement in order to iterate on a dictionary. A similar progression follows for re searches; however, an re iterator must be made available. The finditer method solves this problem so that the expected Python for loop structure remains intact.

The finditer method iterates on a string from left to right and non-overlapping matches are flagged in the order that they are found. Useful information such as the start and stop indices can be determined via the appropriate method calls. For example,

for match in re.finditer(pattern, text):
    s = match.start()
    e = match.end()
    print(text[s:e])

can search for non-overlapping, multiple occurrences of a pattern within a string. In addition, the start and end methods give you the opportunity to record the locations of the match within the string. Most importantly, the finditer iterator preserves our expectation of how the for statement should work in Python.

To review, see Delving Deeper.

 

Unit 8 Vocabulary

Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.

  • regular expression
  • re module 
  • compile method
  • special characters
  • finditer method

Unit 8 | Study Session Review

Unit 9: Exception Handling

9a. Explain how exceptions are implemented capture programming errors

  • What are exceptions?
  • Why is it necessary to catch programming errors?
  • Explain how the try and except statements are used.

Ideally, after a program is written, it should be completely debugged, meaning that it should not generate errors. In reality, for practical applications with unpredictable user input, this is not the case. When programming, you should be continually addressing factors that could lead to a program crash. If not, then program users will experience program crashes. Python provides a consistent structure known as exception handling and the error that occurs is referred to as an exception. For instance,

a='asdfasdf'
sum(a)

generates a TypeError exception because the sum function is expecting numerical data. Python is designed to catch exceptions. However, if no exception handler exists, the exception will result in the termination of program execution; obviously, an unwanted state of affairs.

The most basic type of exception handler involves two statements: try and except. The indented clause following the try statement is the code that is intended to execute. If an error occurs while attempting to execute the try clause, an indented except clause will execute in response to an exception (assuming the appropriate exception has been identified). Python is equipped with powerful tools for exception handling, but the basic flow is similar to if elif conditional statements.

To review, see Example: Try and Except and Example: ZeroDivisionError.

 

9b. Write programs that handle errors by using try, except, and finally statements

  • Explain how the else and finally statements are used.
  • What are some typical built-in Python exceptions one should know?
  • How are multiple exceptions handled?

In addition to try and except, an else clause can be included in the event that an exception does not occur. Finally (no pun intended), the finally statement can be used to house all code intended to execute whether or not an exception has occurred. Your expertise with if else elif statements should directly translate into writing try except else finally exception handlers.

A blanket except statement without referring to any specific exception will catch any error; however, this scenario makes it difficult to write a useful exception handler as the specific error has not been identified. The Python language has made provision for several built-in exceptions that you should know:

  • IndexError: illegal index into an ordered contained such as a list or a tuple
  • KeyError: illegal key into a dictionary
  • TypeError: a function gets argument of improper type
  • ValueError: a function gets argument of correct type but improper value
  • ZeroDivisionError: division by zero
  • NameError: a variable is not found in local or global scope

In addition, multiple exceptions using multiple except clauses are handled in the order they appear in the exception handler.

To review, see Example: Else and Finally and Exceptions Lesson.

 

Unit 9 Vocabulary

This vocabulary list includes terms listed above that students need to know to successfully complete the final exam for the course.

  • debugged 
  • program crash
  • exception handling
  • exception
  • try statement 
  • except clause 
  • else clause
  • finally statement 
  • IndexError
  • KeyError
  • TypeError
  • ValueError
  • ZeroDivisionError
  • NameError

Unit 9 | Study Session Review

Unit 10: Object-Oriented Programming (OOP)

10a. Explain the differences between procedural, structured, and object-oriented programming

  • What is procedural programming?
  • How does OOP differ from procedural programming?
  • What is structured programming?

When it comes to programming techniques, it is best to arrange instructions into groups that reflect the desired computation. Arranging code so that it is modular is fundamental to structured programming. In this way, a program is broken down into a series of small modular steps, each of which solves a specific aspect of a larger problem. When these small modular steps and tasks are phrased in the form of functions and procedures, this is known as procedural programming.

Object-oriented programming differs from procedural programming. Instead of having multiple functions defined without regard to their inherent relationships, OOP arranges and groups together related data attributes and methods into a class. OOP takes a more global view when creating a program to solve a problem. A problem is first analyzed to determine how data and methods should be related. From this analysis, classes can be formed and the data attributes and methods can be categorized within each class definition.

To review, see Structured Programming and Procedural Programming and Procedural Programming versus Object-Oriented Programming.

 

10b. Explain how classes, objects, and instances are used in object-oriented programming

  • What information does a class definition contain?
  • How are instances, objects, and classes related?
  • How do the fundamental components of a class relate to objects?

A class definition in Python contains data attributes and methods that are related to the class. An instance of a class is known as an object. Creating an instance of a class is known as ‘instantiation’. Multiple objects of a given class can be instantiated. Each object keeps track of its own data attributes defined by the class. In addition, each object can use the methods defined within its class to operate on data.

An object can call a method from its own class. In order to do this, the dot notation must be used. In addition, input data to a class method can be used to modify a data attribute associated with a specific object. Therefore, OOP allows for each object to carry around its own data and access class methods to operate on and perform computations with that data. http://greenteapress.com/thinkpython2/html/thinkpython2018.html

To review, see Basic Terminology, Creating Classes and Methods, and Going Deeper.

 

10c. Implement simple programs that use classes, objects, and instances

  • What does the __init__ method do?
  • Explain how self is used.
  • What is a magic method and how are they useful for OOP in Python?

When an object is instantiated, if the __init__ method is defined within the class definition, it is automatically called. The __init__ method is generally used to initialize an object’s data attributes. More sophisticated uses could involve including exception handlers and memory management modules. When any method call takes place, by Python convention, the method makes reference to the associated object using self. The first argument in a method definition that modifies attribute data must be self. Therefore, the number of input parameters to a class method definition will differ from the method call by one parameter because self is implied and is not needed in the object’s method call.

Magic methods (also called dunder methods for ‘double underscore’) give the programmer an immense amount of power. This is because they can be used to generalize Python built-in operators and functions typically applied to built-in data types. For example, magic methods such as __str__ allow a programmer to define a print function for the class. Arithmetic magic methods such as __add__ allow a programmer to define what the + operator means as a class operation. Magic methods such as __ge__ and __eq__ allow a programmer to define relational operators for the class. These are but a few examples of how flexible Python can be when it comes to creating classes.

To review, see Going Deeper.

 

Unit 10 Vocabulary

This vocabulary list includes terms listed above that students need to know to successfully complete the final exam for the course.

  • structured programming
  • procedural programming
  • object-oriented programming
  • class
  • instantiated
  • object
  • magic methods
  • Init method 
  • self

Unit 10 | Study Session Review

CS105 Course Summary