Python I Compiled Notes
Python I Compiled Notes
Notes
Python I Notes Page 2
Table of Contents
Python Features Overview ................................................................................................... 11
Key Features........................................................................................................................ 11
Python’s Programming Features ................................................................................................... 15
Python.org .......................................................................................................................... 16
About ........................................................................................................................................... 17
Downloads ................................................................................................................................... 18
Documentation ............................................................................................................................ 19
Community Section ...................................................................................................................... 20
Other Web Resources ................................................................................................................... 21
Languages ........................................................................................................................... 24
Variable............................................................................................................................... 25
Scenario 1............................................................................................................................ 25
Scenario 2............................................................................................................................ 25
Scenario 3............................................................................................................................ 25
Variable Naming ........................................................................................................................... 27
Program 1............................................................................................................................ 29
Program 2............................................................................................................................ 29
Assignment Operator.................................................................................................................... 31
Python’s Math Operators.............................................................................................................. 31
Symbol ................................................................................................................................ 31
Operation ............................................................................................................................ 31
Comments ............................................................................................................................................37
Line Continuation Character .......................................................................................................... 38
Syntax ................................................................................................................................. 40
The Print Function ............................................................................................................... 41
The Bin Statement ........................................................................................................................ 42
In Anaconda .................................................................................................................................44
Overview ............................................................................................................................. 46
Boolean Expressions ..................................................................................................................... 48
Python I Notes Page 3
Operator ............................................................................................................................. 54
Meaning .............................................................................................................................. 54
Boolean Variables ......................................................................................................................... 58
Introduction ........................................................................................................................ 59
Indexing of a String ....................................................................................................................... 60
Len Function .................................................................................................................................62
Concatenation .............................................................................................................................. 63
String Slicing .................................................................................................................................63
Statement ........................................................................................................................... 74
Output ................................................................................................................................ 74
Python Code ........................................................................................................................ 75
Result .................................................................................................................................. 75
Python Code ........................................................................................................................ 75
Python I Notes Page 4
Result .................................................................................................................................. 75
Python Code ........................................................................................................................ 76
Result .................................................................................................................................. 76
Python Code ........................................................................................................................ 76
Result .................................................................................................................................. 76
Python Code ........................................................................................................................ 77
Result .................................................................................................................................. 77
Python Code ........................................................................................................................ 78
Result .................................................................................................................................. 78
Python Code ........................................................................................................................ 79
Result .................................................................................................................................. 79
Python’s Escape Characters........................................................................................................... 79
The Overuse of Exception Handling to Control the Flow of a Program ................................. 236
Pros to the use of Exception Handling Control the Flow ...................................................... 237
Overview ........................................................................................................................... 239
Overview ........................................................................................................................... 243
Theoretical Approach to Efficiency .............................................................................................. 245
Linear Search .............................................................................................................................. 245
• If the search value is not on the list, then n searches have to be performed. ..........................245
• If the search value is not on the list, then n/2 searches on average have to be performed. ...245
What is C? .................................................................................................................................. 248
• Although there are more clever ways to implement database lookups, I always programmed it
in the simplest manner. ......................................................................................................................257
• If you are developing your own algorithms, then the keep-it-simple idea may not hold but try
to follow this principle as much as possible. ......................................................................................257
• Another pair of eyes also means that you set aside the program and take a break from the
examination. .......................................................................................................................................258
• Work on some other project or get a good night’s sleep! Sometimes then the problem is
obvious. ..............................................................................................................................................258
Functions........................................................................................................................... 294
Measures of Spread ........................................................................................................... 294
Don’t recreate the wheel! ........................................................................................................... 294
Python was designed for simplicity but a programming language needs to have
certain basic programming features. Some of the key features are listed in the
next section. For a full discussion of these features, visit python.org:
https://wiki.python.org/moin/BeginnersGuide/Overview
Key Features
a = 1000
b = 0.05
c=a*b
print(“sales tax was “, c)
sales = 1000
tax_rate = 0.05
Python I Notes Page 12
x [□x 6?40]
Based on their reaction, these individuals did not even know what Perl or
sockets were used for. I found sample ASP code for sending an email (not
a library) and had the website working in twenty minutes. From a
business standpoint, they would have liked the idea to have come up six
hours ago due to potentially losing customers. Libraries are very similar to
this example. A company would prefer you to use a pre-constructed
library versus writing the code from scratch. Libraries may be a bit tricky
to set up in your environment but, once they are, it can drastically reduce
the development time of your project!
5. Python’s interpretative nature allows easy testing and debugging.
Compiled languages are often difficult to test and debug, mainly because
code must be compiled before it can be tested. Syntax errors are fairly
easy to identify and correct. Logic errors, on the other hand, can be
challenging as well as testing. Python’s interpretative nature allows a line-
by-line testing and debugging.
6. Easy integration of modules implemented in a compiled language. It can
be a challenge to bring in new languages into an organization. Consider if
all of your systems are written in C including the payroll component.
Consider if the desire is to add a new feature into Payroll but have it
programming in Python. If there is no way to interface programming
languages, the new feature will have to be done in C. Without an
appropriate interface, there would be two options: 1) Rewrite the entire
system in Python (costly and not practical) or 2) Write the new feature in
C (which is why many companies continue to program in the same archaic
language).
Python I Notes Page 14
http://www.cioinsight.com/it-strategy/application-
development/slideshows/nine-advantages-of-open-source-software.html
Python I Notes Page 15
1. Many basic data types, including numbers (floating point, complex, and
unlimited-length long integers), strings, lists, and dictionaries.
2. Object-oriented programming, including classes and multiple
inheritances.
3. Modules and packages to allow re-use of code.
4. Exception handling, including raising and catching exceptions.
5. Data types are strongly and dynamically typed.
6. Generators and list comprehensions.
7. Excellent memory management that is automatically handled.
The above items can be found in other programming languages. The website is
pointing out these features to emphasize that, while Python has many features
that simplify coding, it also has the same programming features as more complex
languages.
Python I Notes Page 16
Python.org
Python documentation and resources can be found on the main Python website:
www.python.org
Keep in mind that this website might change on a regular basis but the main
resources that are presented here should be present somewhere on the site.
Python I Notes Page 17
About
The About section has some starting information on this website and the
resources available, but the quotes section is the most interesting part of this
section. Here is a quote from https://www.python.org/about/quotes/: “Python is
fast enough for our site and allows us to produce maintainable features in record
times, with a minimum of developers,” said Cuong Do, Software Architect,
YouTube.com. There are many interesting quotes on this website to point out the
power of Python.
Python I Notes Page 18
Downloads
In this course, Anaconda software will be used but, if you wish to install all the
libraries by hand, then the download section will give you the basic Python
software to get started. In this section, you will find many different releases of the
software. Realize that old releases are kept on the site because some of the
newer releases may have software bugs.
Python I Notes Page 19
Documentation
This is a very useful section for Python documentation. For this course, the Python
3.x Docs will be your best resource. If you are struggling with the syntax or how to
use a particular function, then this page should be your starting point. Sometimes
doing a web search on www.google.com will help with syntax, but the code found
might be written in Python 2.x and, unfortunately, Python 3.x is not backward
compatible, thus the code may not work. This can be frustrating. Watch your
versions of documentation and examples, whether it is 2.x or 3.x.
Python I Notes Page 20
Community Section
Open source does not necessarily mean that the software is well designed or
supported. SourceForge (http://sourceforge.net/) is a repository of open source
projects. Many of these projects (software) are not updated on a regular basis
and do not have community support. Imagine a software product that is being
used in a business on a regular basis. If there is a software issue, then it can be
frustrating if there is no support. One huge advantage of Python is that the
software and many of the libraries are well supported. If you have difficulty, you
can post your problem in the Community section and get help. Once you develop
a comfort level, be sure to support others by contributing!
Python I Notes Page 21
Perhaps the C code is running on a Windows server and the Java piece needs to
be on the server’s intranet. The first major hurdle is that these programming
languages do not seamlessly interface with each other: C cannot call Java
functions, and Java cannot call C functions, without modification. Another
potential problem is the hardware component, which can be tricky to solve, as
certain languages are designed to run on specific types of machines. This is
especially true for newer language system requirements, and companies often
ignore this problem by only using newer languages on brand new systems. One
particular system, used by a company, had a module of their purchasing system
written in Paradox for DOS. The system did not interface with any other system
due to this lack of integration. For twenty years this system was used “as is” since
Python I Notes Page 23
the company did not want to rewrite it from scratch. In 2012, the Paradox for DOS
system finally disappeared as the company purchased/installed SAP (Systems,
Applications & Products in Data Processing).
Visual Studio was a major attempt at solving this integration dilemma. The hope
was that programmers could program in whatever language they wish and then
these pieces of code would be able to communicate. Instead of compiling to
machine language, it would be compiled to Microsoft Intermediate Language
(MSIL) but it would require the .NET framework on the target platform in order to
run successfully. This original concept has changed over time and many benefits
and flaws of Microsoft’s product could be listed, but that is beyond the scope of
this class. Visual Studio had many other goals besides the integration with other
programming languages.
Python was designed to have the hooks for allowing an easier integration. Keep in
mind that this is still not a simple task. The following URL contains information on
some of the projects in the work on simplifying integration with other languages:
https://wiki.python.org/moin/IntegratingPythonWithOtherLanguages
Python I Notes Page 24
Languages
• C • Perl
• C++ • PHP
• Delphi • R
• Fortran • Objective-C
• Lisp • Tcl
• Prolog • Lua
• Java • OCaml
• C# / .NET • Eiffel
• Haskell
Some of these open source projects are established and supported, while others
are weak. For example, RPy has not seen a release since October 21, 2012, as of
this writing. Compare that to the Jython project, which had a release just three
months ago as of this writing.
Keep in mind that integration is not an easy task and, if you need to integrate
various programming languages with Python, then do the appropriate research
and choose a “mature”, supported, open source project. It will save you many
headaches in the long run.
Python I Notes Page 25
Variable
Example:
This declares the variable called salary and assigns it a value. The type of data on
the right side of the equals determines the data type (integer, float, etc.).
Compare these three scenarios:
The type of this The type of this The type of this variable is
integer decimals)
Python I Notes Page 26
This can be confusing, but a programmer must put thought into the data type of
the variable and the operations.
Example:
Perhaps the programmer wants to perform integer division. In this case, example
3 divided by 2 would give 1. But the result that is obtained in this program is 1.5.
This will be examined in more detail later in the course, but realize that
assumptions about data and types should be verified with proper testing!
Python I Notes Page 27
Variable Naming
Example:
Example:
This gives the following error message “TypeError: 'int' object is not
callable”. Best practices of programming indicate not to use any variable
name close to a keyword. It makes your program very confusing.
Python I Notes Page 28
Program 1 Program 2
The print statement could include a The poor usage of variable names
bit more about the purpose of the causes the purpose of this program
program, but its goal is still clear. to remain cryptic. Even still, it
accomplishes the same task as the
program on the left.
Assignment Operator
Example:
tax = salary * tax_rate
This code retrieves the values of salary and tax_rate, then multiplies them
together, assigning the resulting value to the variable called tax. The right-hand
side of the assignment operator follows the order of operations. The left-hand
side of the assignment operator must be variable!
Coding Tip: If you do not remember the order
Python’s Math Operators of operations (many programmers do
not remember), then use parenthesis to
indicate an operation that should be
done first.
Symbol Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder
Python I Notes Page 32
** Exponent
Example:
Formula for adding sales tax to the order amount
If you are concerned that the program will first add order_amt + order_amt, and
then multiply it by sales_tax_rate. You can then rewrite the program like so:
Example:
Total = order amount + (order amount * sales tax rate)
The parenthesis will make sure that it multiplies order_amt * sales_tax_rate first!
Python I Notes Page 33
In case you are curious, the original statement was fine. Order of operations
handles multiplication before addition. A business would probably prefer extra
parenthesis to avoid errors. Programming or math errors could potentially cost a
company millions of dollars. As an example, the Mars Climate Orbiter went into
the atmosphere of Mars and was destroyed due to a simple English/metric
conversion error. The estimated cost of this error was 193.1 million dollars.
Python I Notes Page 34
Example:
Example:
Consider this interest formula:
A = P * (1 + r / n)**(n * t)
Python I Notes Page 35
Coding tip: Do not assume that since the functionality of the current
version of the programming language is designed in a certain
manner it will always function in the same manner in future
versions. For example, consider the following code:
Whenever possible, control the flow of the program using parenthesis or type
conversions, which will be examined in due course. If the Python program that
you are running is for short-term use, these considerations do not typically have
to be examined closely; however, if the program is for long-term use, any changes
that are introduced in the new versions implemented by the computer
department may significantly impact your programs and you should perform
comprehensive testing after the upgrade. This is not the same as upgrading to a
new version of a word processor!
How do I know this? I was surprised Python had changed integer division between
2.x and 3.x. I performed a Google search of “how to do integer division in 3.0?”
and my first result gave me the answer. Do not assume, as a programmer, that
you are expected to know all the aspects of programming. You must be willing to
explore all the resources available. This does not mean that you contact your
instructor every time that you have a question. I once had a computer “expert”
explain what an “expert” represents in any field. It simply means someone who
knows more than you. Certifications in certain computer fields may indicate a
Python I Notes Page 37
level of expertise but do not mean the individual knows everything. A better view
of a true “expert” is that they know where to find the answer!
Comments
Comments are not run by the interpreter but are included in the program to
document important items that will help the programmer (or any programmer) to
understand the purpose of the code at a later time. A single line comment is
indicated by a #, such as this:
Example:
Multi-line comments can be created by using three double quotes to denote the
start of the comment area and three double quotes to denote where it ends.
Example:
Python I Notes Page 38
This is from Anaconda. Note that the single line comment is grayed out, while the
multiple line comment is in green. This particular comment is code that I do not
want the Python interpreter to run. This illustrates the most common use of
multi-line comments. Instead of deleting the code, it can be commented out so
you can refer to the old code or, perhaps, use it if the new code does not work. It
can be an invaluable testing and debugging strategy, which we will look at later in
this course.
Sometimes the line becomes too long and it is beneficial to use the line
continuation character, which is a backslash (\).
num1 = 2
num2 = 8
num3 = 10
num4 = 20
print("The first number is ", num1, " and the second number
is ", num2, " and the third number is ", num3, " and the fourth
number is ", num4)
Python I Notes Page 39
num1 = 2
num2 = 8
num3 = 10
num4 = 20
print("The first number is ", num1, \
" and the second number is ", num2, \
" and the third number is ", num3, \
" and the fourth number is ", num4)
Python I Notes Page 40
Syntax
Syntax is the form that the various language elements can take. It describes
exactly how to write the commands. Consider this syntax form for the print
statement from the documentation pages of Python (located at
https://docs.python.org/3.4/index.html):
These are sometimes difficult to read. The more that you program in Python and
refer to the syntax, the more these will make sense.
Python I Notes Page 41
Notice in the syntax of the print function that it has an option for sep. This is what
is known as a keyword argument, and defines the separator between the two
items. By default, this is a space, but anything can be specified as in this
modification:
While this may not seem to make sense, consider if we wished to put the data in
CSV format (comma separated value), then we would want commas between the
data and would change the code to look like:
happy,days
sad,days
Notice that the “end” option is not specified, but defaults to “\n,” which causes
the new line to occur, equivalent to hitting the Enter button in a text editor.
Without any testing, the syntax told us that the function will “convert an integer
to a binary string.” Since it said “string,” the %s format specifier was used. The
result was:
Python I Notes Page 43
The result was not entirely expected. With an input of 153, the answer desired
was 10011001 but it placed an 0b on the front, which is Python's way of
specifying that the number is binary (rather than the integer ten million eleven
thousand and one). This 0b, of course, can be removed using string functions if
desired.
The syntax can be invaluable in helping figure out how to write a statement. In
Python 2.x there was no print function, as it was actually a print statement. It
could be that you will be faced with working with a different version of the
interpreter and the syntax will help guide you on the changes.
Python I Notes Page 44
In Anaconda
Anaconda has a nice feature. When you type in the Python language element, a
popup with the appropriate syntax will be displayed:
Python I Notes Page 45
However, this is only semi-useful since it disappears from the screen. If you place
your cursor on the Python language element and hit CTRL-I, the syntax will appear
in the development environment as in this example:
Not all Python editors have this feature so it is important to be able to access the
documentation on the Python website.
Python I Notes Page 46
Overview
This particular code above could be simplified. This simplification can be seen in
this diagram:
Python I Notes Page 47
This if-then-else structure allows the driver to analyze the situation and branch
accordingly. This same concept holds true in a Python program.
if condition:
statement
statement
etc.
If the total sales achieved by a salesperson exceeds $70,000, this person will
receive a bonus of 10% of their sales, or else this person will not receive a bonus.
Python I Notes Page 48
Boolean Expressions
Relational Meaning
Operator
== Equals
!= Not Equals
Item 1 and Item 2 are typically either variables or numbers. Remember that
the concept of the Boolean expression is that it is either true or false, which
means that at least one of the items has to be a variable. It would not make
Python I Notes Page 49
any sense for both to be a number, as this would result in it being always true
or always false.
Example:
Revised Bonus Calculation Program
Pause for a few minutes and consider what is wrong with this code.
If the tot_sales is greater than $100,000, then it performs the first if statement
BUT also performs the second if statement, which re-calculates the bonus at the
10% level. Now we could re-arrange the if statements such so that the code could
work but there is an easier method.
if condition:
statement
statement
etc.
else:
statement
statement
etc.
Python checks the first condition and, if true, will execute the indented code
underneath it. If false, it will skip to, and execute, the code contained underneath
else.
This decision structure works well in many circumstances, such as the following
coding example in which a company wishes to pay time and a half for any hours
over 40 hours. Assume that hours and pay rate are integers.
Example:
Python I Notes Page 51
While this structure is useful in many cases, Python provides an extension that
will help in the coding issues presented above:
if condition_1:
statement Note: This form only has one elif
but Python allows as many
statement as is necessary for your
code! Remember that only
etc. one block of code will be
executed here, that is, if
condition_1 is true, only
elif condition_2: that chunk of code will be
executed and the rest will
statement be skipped. The elif is
equivalent to an “else if” in
statement other programming
languages.
etc.
else:
statement
statement
etc.
Python I Notes Page 52
Now let’s revisit our code above. Consider if a company wishes to give a 20%
bonus for a salesperson who makes more than $100,000 and a 10% bonus for a
salesperson who makes more than $70,000. Assume sales are integers.
Example:
Fixed Revised Bonus Calculation Program
Python I Notes Page 53
if condition_1:
if condition_1a: Coding Tip: If possible, keep computer programs simple.
Embedded decision structures can be beneficial but
statement one indentation issue can cause code to run
incorrectly. Remember, the spacing determines
statement which part of the decision structure that the
statement belongs under.
etc.
else
statement
statement
etc.
else:
statement
statement
etc.
Python I Notes Page 54
Logical Operators
Often in programming, the basic idea is to keep it simple, which works out well
since that is also one of Python’s main features! Logical operators allow us to
reduce the need for complicated nested decision structures.
Operator Meaning
Consider if a company wishes to give a 20% bonus for a salesperson who makes
more than $50,000 and less than $100,000. Assume that sales are integers.
Example:
Python I Notes Page 56
This code could be accomplished with a nested structure but again the idea is to
keep it simple!
Example:
If num is equal to zero then we would not want 9 / num to occur. It would cause
the incomputable division by zero. The “and” structure will check num != 0 first
which, if false, will not even check the second Boolean expression, since BOTH
have to be true for the “and” operator. This is short-circuit programming. Usually,
there are much simpler ways to program the above code (with more lines of
code) and short-circuit evaluation should be avoided.
Python I Notes Page 58
Boolean Variables
Example:
Example:
Python I Notes Page 59
Introduction
h
a
p
p
y
The code iterates through each character of the string, storing the character in a
variable called ch. Many programming languages require a more complex manner
of traversing through a string character by character. This for loop would allow
character by character manipulation/calculation. Consider this example that
counts how many a’s are in a string:
Python I Notes Page 60
Indexing of a String
Even though the length of this string is 5, the indexing goes from 0 to 4. To
reference a particular letter in this string, the indexing would follow this form:
variable[index]. For example, to access the a in happy the program would need to
access in the following manner:
Python I Notes Page 61
Using indexing, the same result can be achieved as the first example with the
following code:
The for loop will be covered in more detail later on, but this loops through x,
equaling the values from 0 to 4 (one less than the number that you see in the
range function). The result would be:
h
a
p
p
y
Indexing can also occur from the end of the string in this manner:
Python I Notes Page 62
The code to access the a from the end of the string would look like:
Len Function
In some cases, the length of the string is not known to begin with, but can be
determined with the len function. Consider this example to count how many a’s
are in the string input by the user:
Notice that, in this problem, len(str1) dynamically determines the length of the
string at runtime. The result is the following:
Please enter the following text: happy days are here again
The number of a’s is 5
Python I Notes Page 63
Concatenation
String variables can be concatenated in the same manner that was presented with
the print function. Consider this code:
String Slicing
The capability exists to slice part of the string out. This is similar to the mid or
substr function in other programming languages. The general form is:
variable[begin:end]
Where begin is the starting index of the string to be extracted and end is the
ending index. The string that will be extracted starts at the starting index and goes
to one less than the ending index. Consider the following code:
Python I Notes Page 64
Note that the datestr[0:2] pulls the substring that goes from index = 0 to index = 1
(one less than the ending index). This code has some serious issues with it.
Consider if the date that is entered would be 1/1/2014, what do you think would
happen? We will talk about ways to handle this issue shortly.
Other variations of the slicing exist and are demonstrated with these examples:
happy
again
The in operator tests whether one string is in another string. For example,
consider the following code:
Python I Notes Page 66
The result of this function is true or false so, oftentimes, it is used in an “if”
statement! The result of this code is:
The not in operator works in a similar manner, but checks to see if the string does
not contain the search string.
Python I Notes Page 67
These are not functions but methods, so they follow the variable name as in this
example:
Python I Notes Page 68
Where begin is the starting index of the string to be extracted and end is the
ending index. The string that will be extracted starts at the starting index and goes
to one less than the ending index. Consider the following code:
As before, these are methods and should follow the variable name. Many times in
comparisons and searches through strings the programmer does not wish the
wrong case to cause issues. If the data was going to be stored in a database, the
free-form entry of data can cause database issues. For example, one of my first
database applications asked for the state. I allowed it to be entered in freeform.
For one state like Kansas, there were many forms entered in: KS, Ks, ks, kansas,
Python I Notes Page 70
Kansas, etc. I realized my mistake early and corrected the issue. Uppercase versus
lowercase will cause major headaches in databases! A simple method like upper()
can change all the letters to uppercase.
The code needs to be changed to prevent errors when a single digit is entered for
the month or day. Here is the modified code:
Python I Notes Page 72
The input function allows your Python program to receive input from the user of
the program.
General format
variable = input(prompt)
The prompt is a string that is displayed to the user of the program prompting the
Example:
input. The input function returns a string value and attempts to place it into the
variable. This example prompts the user for their first name, then places it into a
variable called name.
The only type that is returned is a string type so an appropriate conversion must
be done if another type is desired.
Python I Notes Page 73
The following example inputs two integers, multiplies them together, and prints
the answer.
The int( ) surrounding the input function converts it from String to Integer. If an
Example:
As seen in earlier sections, the print function allows your Python program to print
various types of information to the screen.
Statement Output
1. Either the double quotes or single quotes can surround the text message
that is to be printed to the screen.
2. Different text can be separated by a comma within one print statement but
it puts a space automatically between the text. Typically, in programming,
we would expect the last print statement to return the result
“Haveagoodday” but spaces are automatically put between text.
Python I Notes Page 75
The reason why the print function allows either the double quotes or single
quotes is because the programmer might actually want to have either one inside
the text. For example, if the following string is to be printed: I’m home – then this
could be accomplished with the following code:
I’m home
A single quote was necessary inside the string so we used double quotes to
surround all the text.
Consider if we wanted to print: The author said “Good Job!” – then the following
code would work:
The double quotes were necessary inside the string so we used single quotes to
surround all the text. The Python programmer may have to use a combination of
these items if the desire is to have the string contain both the double quotes and
single quotes. Another technique is to use triple double quotes or triple single
quotes, as in this example:
Python I Notes Page 76
The
author’s
quote was
“Good Job!”
The triple quotes allow the programmer to print items on multiple lines, as in this
example:
Obviously, this is beneficial if the programmer has many lines of text to print to
the screen.
3.0
Good job!
These three examples show printing variables containing integers, floats, and
strings
19
0.3333333333333333
With the basic print function, the formatting may not be the desired format
(perhaps you wanted 0.33 as the result of the second statement).
The print function can also print a combination string of literals, variables, and
expressions.
Python I Notes Page 79
1 divided by 3
is equal to
0.3333333333
333333
As seen in earlier sections, the print function allows your Python program to print
various types of information to the screen.
Example:
The \n repeated three times will cause “job!” to be printed three lines down. The
regular print function only goes down one line, as seen below.
Example:
Example:
As can be seen, the formatting is poor. Consider the use of \t in the print
functions:
Example:
Python I Notes Page 82
For a text-based output, the \t and proper use of spaces allow an acceptable
display. Do not use spaces alone to control output. Different fonts will display
differently!
Python I Notes Page 83
The plus sign (+) can be used in a print function to concatenate two strings. The
purpose of this can be seen in the following examples:
comp uter
computer
Example:
gives the following syntax error: TypeError: Can't convert 'int' object to str
Example:
implicitly
The data type of num would need to be converted within the print function, as in
this code:
Python I Notes Page 85
The str(num) returns the value of num as a string value that can then be
concatenated with “happy” and “days.” If the type of num is already a string then
there is no need to use the str function:
Example:
Example:
Obviously, this is not the desired result. The problem in this Python program is
that the numbers were not changed from strings to integers. Consider this
change:
Example:
The int function converts the input from the user from a string type to an integer
type and now gives the desired result:
Keep in mind that the lack of syntax errors does not mean you do not have other
errors in your program!
Python I Notes Page 87
Python I Notes Page 88
String Formatting
Any programming language should have more functionality than what was
presented above. Python is no different. It includes two components: format
Example:
specifier and string format operator. The format specifier is embedded into the
string portion of the print function. The string format operator connects the
number to be formatted (although it does not have to be just a number, as we
will see in the examples).
In this example, the value of the variable num1 will be placed in the location of
the format specifier using the format indicated.
Python I Notes Page 89
The %f is the format specifier for a floating point number. The .2 will round the
number to two decimal places. In order to round the number to three decimal
places, %.3f would be used. The decimal point in the format specifier specifies the
“actual” decimal point.
Consider the following examples to better understand the formatting for floating
point values.
places)
The %d is the format specifier for an integer number. The d represents the
decimal integer. This does not mean that it has decimals since an integer does
not. The %d does not have as many options as the floating point specifier.
The answer is 4
The answer is 4
The %s is the format specifier for strings. The s represents string. Consider these
examples:
Example:
Notice in this example that there are three format specifiers and, in the
parenthesis, there are three values (num1,num2,num1/num2).
Notice the num1/num2 is an expression. The values following the string format
operator can be variable(s), number(s), or expression(s). If there is more than one
value, then it must have parenthesis around it, as in the above example.
Python I Notes Page 94
Notes: Looping
Looping Structure
multiplication of each and every number beforehand. It would work but what if
the user wanted to calculate 1,001 factorial? Clearly, this would be a tedious task.
Consider another example of a program that prints a shipping label for a single
customer. It might work perfectly and give the result. But then imagine if your
company is as large as Amazon which, according to Business Insider, sold 27
million items on Cyber Monday! Your shipping program would need to be run 27
million times to handle just that one day.
Background
In the past, much of the looping done was with what are known as goto
statements. Going back to factorials, an example program to calculate 5 factorial
might look like:
Python I Notes Page 96
While this initial “looping” helped solve the problem of “single pass” processing, it
led to many unstructured programs. Allowing programs to jump anywhere in the
code, while useful in some regards, leads to very messy programs as they get
larger. Bugs become hard to identify because there is no clear way of telling
where the execution will end up next. My first programming job on a Unisys
mainframe used this type of looping. The programming language was COBOL and,
while it had rudimentary looping constructs, the standards of the company
required the programmers to use labels and gotos. As you can imagine, it was not
enjoyable.
Two main constructs exist in looping: a for loop, and a while loop. Their English-
language counterparts give hints as to how they operate. Usually, the statement’s
construct tells:
A for loop allows looping but in a very confined manner. Consider the following
code to calculate 5 factorial:
The program iterates through the range of one and six, then multiplies the
number by the previous result (stored in the fact variable). The for loop iterates
through a set collection of values (in this case, the range of numbers beginning at
one and less than 6).
The second construct is a while loop. It iterates a chunk of code while a condition
is true. As soon as the condition becomes false, the indented code is not
executed, and the program continues execution with the remaining code.
Consider the same code to calculate 5 factorial:
Python I Notes Page 98
Many programmers prefer the while construct since it allows complete control;
that is, the three pieces of information listed above are clearly defined. There are
some downsides to the while construct. It is easier to program an infinite loop
where the program continues running forever or until the program halts with an
error. Consider the above code if the increment was left off for num:
This program runs for an infinite amount of time, as the value of num will always
be less than six!
This example illustrates how complete control can be a bit trickier, which can be a
benefit or a hindrance in certain cases, in that there are more variables or
conditions to manage.
Python I Notes Page 99
Overview
In the early days of computing, RAM and hard-drive memory were scarce. As late
as 1995, I remember struggling to fit network and system drivers into 640
kilobytes of memory. I would have to rearrange the order of the drivers to get
them to work. These were the DOS days when programming had to be done
much more carefully. Variables were re-used to save space and unused variables
were unheard of.
they ran that five-hour process. It often had to be run at night so that users of the
system would not notice the impact.
When programming for a business, coding efficiency is often less of a priority than
how quickly results can be produced. Programmers tend to want a certain level of
“purity” in their programs. I have found myself wanting to redo logic after I got it
working, just because I knew there was redundancy in the program. Sometimes, it
takes the business to stop the programmer.
Many formulas exist to analyze efficiency. Two seconds does not sound like much
if it is applied to the total processing time, but consider if it was 2 seconds per
customer record. If we had a business that had 10,000,000 customer records,
then we are talking about 20,000,000 seconds or approximately 231 days. All of a
sudden, that “two seconds” is very significant!
Python I Notes Page 102
Python Example
20
24
225
2223
5
23
5
6
The correct way is to do the prime factorization of each number and then cancel
what the numerator and denominator have in common.
Python I Notes Page 103
With Python, we can use brute force processing to start at 20 (the numerator)
and work our way backward to ascertain if both the numerator and denominator
are both divisible by the same number:
This program actually goes down to division by two, which is unnecessary, as four
is the least common factor and the program could stop there. But accounting for
that would require the programming to be more complex, and the time it saves
would not even be noticeable.
Python I Notes Page 104
General Form:
while condition:
statement
statement
etc.
The condition is checked prior to performing the statements. Once the condition
is no longer true, the program execution continues past the last statement in the
while loop. The condition is a Boolean expression. Consider this example that
adds the numbers from 1 to 10:
Often, while loops have a preceding line to initialize the variable that is being
checked. In this case, num is set equal to 1. Next, the while statement will check
Python I Notes Page 105
the Boolean condition. Somewhere within the while loop, the variable will have
some kind of math operation performed on it.
These three elements are crucial to the successful implementation of the while
loop. All three of these components will take various forms but must be in the
program. Consider the example of finding n-factorial:
Python I Notes Page 106
Both of these above examples added one, but any math calculation could be
done. Perhaps you’re evaluating a large collection of inputs, three values at a
time. In that case, you would likely want to increment num by 3, rather than 1.
The Anaconda system helps points out issues and recognizes that the initialization
was not done.
Python I Notes Page 107
Notice that there are no errors pointed out by the system. Once the program is
run, however, the following occurs:
The program never comes back since num is never incremented. The loop
continues indefinitely. Remember, when programming a while loop, to have all
three elements and that the condition will eventually be false!
Python I Notes Page 108
For Loops
The for loop is a count-controlled structure. This means that it will be run for a set
number of iterations. This total number of iterations might be hard-coded or
determined at runtime. Consider this example, if we want to program the
binomial probability formula:
n!
P( x) p x (1 p) n x
(n x)! x!
The n! would need a for loop since it is count controlled. For example 5! would
need to be iterated five times, as in:
5x4x3x2x1
Python I Notes Page 109
General Form
The values in the general form do not have to be strictly numbered; perhaps
you’d like several different strings to be printed in succession:
Example:
Printing colors with a for loop
It would be tedious to add all the numbers from 0 to 20 by applying the general
form. Our for loop code would look like:
fornum in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20]:
But what if you had to iterate over 100 numbers? Or 100,000? Python provides
the range function to simplify coding.
General form using the range function with an end number variable:
Note: The usage of 21 in the range function to have the values go from
0 to 20
Example:
General Form using the range function from start number to end number:
This will iterate the variable from start number to end number – 1.
Example:
Adding all the numbers from 10 to 12
sum = 0
for num in range(10, 13):
sum = sum + num
print('sum = ', sum)
Python I Notes Page 114
Example Code:
Adding all the numbers in a range input by the user
sum = 0
print('This program sums all the numbers in the range
specified')
start_string = input('Input starting number:')
start_num = int(start_string)
end_string = input('Input ending number:')
end_num = int(end_string)
for num in range(start_num,end_num + 1):
sum = sum + num
print('sum = ', sum)
Python I Notes Page 115
Note: The above example was simple. In coding,
sometimes it is easier to code a simple version first.
For example, if the desired result was the sum of the
numbers from 5 to 10,000, it would be very difficult
to know the answer prior to writing the program. It
may be a better idea to write the program to add the
numbers from 10 to 12, which is much simpler to
check. Once it is known that the test case works,
then the code can be changed to ‘fornum in range
(5,10001)’ and the result can be trusted. This is not
always the case since the sum may exceed the limits
of the data type.
General Form using range from start number to end number, with a step
number:
Note: The start number in the general form does not have to be
the smaller number, so can be the larger number. Some
ExampleCode:
algorithms require the for loop to decrement the variable in
Adding
eachalliteration.
the even numbers from 4 to 10
sum = 0
for num in range(4, 11, 2):
print (num)
sum = sum + num
print('sum = ', sum)
This will iterate the variable from start number to end number – 1, changing the
value of the variable by the step number for each iteration.
Python I Notes Page 117
Oftentimes, the exact same manner a process is done by hand is the correct way
to implement in a Python program. If the problem was to simplify the fraction
12/20, the appropriate step would be to think of the largest number that divides
evenly into both the numerator and denominator.
Example:
Simplifying a Fraction
numerator = 12
denominator = 20
fornum in range(denominator,2,-1):
if denominator % num == 0 and numerator % num == 0:
denominator = denominator / num
numerator = numerator / num
print(int(numerator),'/',int(denominator))
Python I Notes Page 118
This code starts with the largest number and goes down to 2, checking to see if
the numerator and denominator are divisible by the value of num; if so, then it
divides both the numerator and denominator by that number. The answer that
this code gives is 3/4. On the other hand, if the for loop is written as “for num in
range (2, denominator):” then the result is 6/10. This program would be very
difficult, if not impossible if the start number is less than the end number in the
general form.
The List datatype has not been covered yet, but when combined with for loops
provide powerful mechanisms. Many programming languages refer to lists as
arrays. Consider a list of numbers: 10, 12, 17, and 15. These can be put on a
Python list using this assignment: pylist = [10, 12, 17, 15]. Recall that arrays are
indexed starting at zero and this is true with lists in Python also.
Example Code:
Adding Numbers to a List
Index Value
0 10
1 12
Note: The indexing of the
numbers in the list
goes from 0 to 3:
2 17
3 15
In future topics, the for loop will be invaluable in simplifying the code.
Python I Notes Page 120
Overview
As you already know from loops, sometimes code needs to be run more than
once. But what if the loops themselves need to be executed more than once? For
example, consider the case in which our program should calculate two factorials,
as in the permutation formula from statistics.
Using only loops, the program to accomplish this formula would be:
As can be seen, the same factorial logic is being done twice, but with different
parameters. The code above is fairly short but consider if this same code has to be
Python I Notes Page 121
In the above example, the factorial code is contained within a function, fact().
Rather than having to write several loops, the fact() function can
be called instead. Let’s examine the various components:
Python I Notes Page 122
The name of the function can be whatever name you choose, but follow the same
naming conventions as variables. In this example, there is only one parameter
passed into the function, but you can have any number of parameters, or even
none.
The return statement returns a value to the location where the function is called,
assuming it is a function that returns a value. Consider this example, which
calculates the two answers for the quadratic formula:
Since this function has no return statement, the line that calls the function has no
variable assignment. Notice that the variables in the main part of the program are
a, b, and c but the parameters were called pa, pb, and pc. The parameters can be
called by any name. It is the values of the variables in the main body of the code
(a, b, c) that gets passed to the parameter variables defined in the function
(pa,pb,pc). The concept behind this is referred to as variable scope, which will be
covered more in depth later.
Python I Notes Page 123
This code uses the same variable names as the main body of the code. This is
often done but can lead to confusion. It is best to use different variable names in
the function, even though it is possible to use the same variable names.
Python does not limit the programmer to returning one value, it can return many
values. Here is the quadratic formula program redesigned to return the answers
to the main program and then print them:
Python I Notes Page 124
Python has its own set of built-in functions that can be called. The program up
above actually called one of these functions, math.sqrt(). There is even a factorial
function that would have simplified our code in the while loop. It is important to
become familiar with the functions built into the libraries of Python. There is no
need to re-invent the wheel if somebody else has already written that function!
Anaconda has a nice feature for libraries that are imported. Consider this
example:
Python I Notes Page 125
Once the import is placed into the program code, you gain access to all of that
library’s functions. Calling the library name, followed by a dot, will display all of
the available functions.
While this list of functions is useful, the programmer will probably have to do
some additional research on the usage and meaning of each function.
Python I Notes Page 126
Overview
Typically, variables can be accessed in the piece of code where they are used. The
question becomes whether variables defined in the main program could be used
in a function and vice versa.
Local Variables
A variable declared in a function is called a local variable. This means it can only
be accessed by that function. Consider this example:
Notice that the variable num is defined in the function, but the program is trying
to print it in the main code body. We have our indicator on the left saying there is
a problem. Let’s examine what happens if we ignore this and try to run the
program anyway:
Python I Notes Page 127
The first print function returns the number (3) that we would expect. The print
function in the main program returns a bizarre result. Do not ignore the error
indicators or you might get bad results!
This is the reason that the return statement was implemented within functions. It
allows communication between the function and the main program. Consider this
rewrite of the program:
Notice how the variable declared in the function test was renamed. It could have
been left named num but this is not considered good programming practice; it is
too easy to confuse the two variables. Now, when this program is run, the
following is the result:
Python I Notes Page 128
Keep in mind that, once a function is completed, then the variable is no longer
accessible even by the same function. Consider this example:
You might expect that it will print 5 the first time the test function is called, and 6
the second time it is called, but here are the results:
Python I Notes Page 129
The first number prints correct, but then we get an error on the second pass:
“UnboundLocalError: local variable ‘testnum’ referenced before assignment”.
Remember that a local variable is only accessible within while that function is
running. After it finishes the first run of the function, then the variable testnum is
no longer available! In the above code, it is only created when p == 1 is true.
Remember, proper communication between the function and main program can
be done with returned values. Consider this rewrite of the above code:
Python I Notes Page 130
Now that particular test function can be called as many times in the program and
it will work (nonsensical as the program appears to be). Our result with this
correct code is as follows:
Python I Notes Page 131
Global Variables
Variables declared in the main program (global variables) can be accessed directly
within a function, but many consider it bad programming practice. Consider this
example of the quadratic formula:
This program works but notice that the function is using the variables (a, b, and c)
directly from the main program. This is a topic that will probably be debated
forever but many programmers consider it bad practice to use global variables
directly in this manner. An alternative way to program this is listed here:
Python I Notes Page 132
Even when using the same variable names for the global variables and for the
parameters, there is no doubt where the variables are coming from. Using the
global variables like in the first example:
A programmer coming in would look at this code and question where a, b, and c
are coming from, but if they are passed in a parameter to the function, there is no
doubt it has simplified coding.
The key point is to pass everything into the function that you will need for that
function!
Python I Notes Page 133
Overview
This lecture will define the different function types and give an example of each
type. Subsequent lecture notes will further define these.
Definitions
Methods Example
Later in the course, you will learn how to design your own objects for
manipulation and data storage.
Python I Notes Page 138
Overview
Global functions are functions that are defined so that they can be called from
anywhere in the Python program. Consider this example:
Both the interest function and the test function are global functions. Notice that
the interest function is called from the main program as well as from within the
test function.
Python I Notes Page 139
It is easy to identify a local function since it is called from within a global function,
as in this example:
Notice that simple spacing turned the test function into a local function, which
cannot be called from the main problem (note the error indicator). When writing
your Python programs be careful of the spacing, since it will make a huge
difference in some cases!
Python I Notes Page 140
Overview
Local functions can be contained within a global function. These are functions
that you would not normally want the main program to be able to call. Consider
this example:
Python I Notes Page 141
In this simplistic example, the functions add and subtract would not need to be
called from the main function:
These are simply supporting functions for the function called test to complete its
task. Consider this following example that has a function to figure the coefficient
of variation based on a sample:
Python I Notes Page 142
The local functions “mean” and “std” are merely supporting functions for the
global function CV to use to calculate the coefficient of variation. We do not
necessarily want these called from the main program. Consider the following
change that tries to call these local functions:
Python I Notes Page 143
Notice that the Anaconda system warns us that there is a problem with calling
mean and std directly! Once the program is run, the following message appears:
Python I Notes Page 144
Now, in a program, it might make sense to WANT to call the mean and std
function. If so, we would want to convert these to global functions, as in this
change:
Notice that all the functions (mean, std, and CV) are now global functions and can
be called from anywhere in the program, even within another function.
1. They are supporting functions that should NOT be called from the main
program.
2. The programmers wish to free up the memory once the function is done
(remember the variables will no longer be accessible).
Python I Notes Page 145
Overview
Normal functions provide much power, but there might be certain circumstances
in which a lambda function might be used. First, let’s compare a regular function
to a lambda function:
Regular Function
This code calculates the coefficient of variation from statistics. The scipy library
allows access to the functions std (population standard deviation) and mean
(population mean).
Lambda Function
Python I Notes Page 146
While we will not see it in this course, certain graphical constructs require an
argument that is a function. Instead of creating a function that is only used one
time (which is contrary to the concept of functions), a lambda function can be
used as the argument.
The same naming rules that apply to regular functions apply to Lambda functions.
As with regular functions, multiple parameters can be passed in; however, only
one value can be returned. One tutorial described the main purpose of Lambda
functions as for building callback handlers.
Python I Notes Page 147
Notes: Methods
Overview
student.study
student.attend_class
student.graduate
These are the generic definitions for any student. Consider some parameters that
might be passed into these methods:
student.study(hours)
student.attend(class, date, time)
student.graduate(date, degree)
a student named Bill. Consider these examples with a specific instance and
detailed parameters:
Bill.study(3)
Bill.attend(“Python Programming”,9/15/2014,08:00)
Bill.graduate(5/15/2015, "Computer Science")
This simply prints our numbers. Now consider in the Anaconda system if we start
typing a and then a decimal:
Python I Notes Page 149
The various pre-defined methods for a list object appear. Many of them are
pretty obvious. For example, the append method would add a new number to our
list:
Notice that a help popup will appear once we start typing the first parenthesis. It
tells us the arguments (or parameters, as we refer to them) that are possible
inputs. Not all of these are required. When the cursor hovers over append and
CTRL-I is pressed, the following will appear:
The Python documentation might present a list of the methods and their
explanation that is easier to browse:
Python I Notes Page 150
If our program is modified to append a new number to the list, the Python
program would look like:
Elements that do not seem like they have any methods associated with them may
have some defined. However, consider this example:
If the cursor hovers over the method name and CTRL-I is performed, the following
message appears:
Do not assume that every method will have documentation in the Anaconda
system. The Python documentation, as well as other resources, might provide
that important missing piece.
Various items will appear once the decimal is entered. Consider this example:
Python I Notes Page 152
Overview
Names and DocStrings are elements used mainly for help and documentation
purposes. Consider the following example:
The comment enclosed in triple quotes is called the docstring. It is more than just
simple documentation. Notice what happens when the programmer types the
function name and the first parenthesis:
It displays the names of the parameters (arguments) required for this function.
Now put your cursor over the function name:
Python I Notes Page 154
Notice that this text matches our docstring! We can also access the
documentation by using syntax in the example from the previous section, similar
to:
print(interest_continuous.__doc__)
This is just a brief overview of this concept of docstrings. This topic will be
explored in greater detail once objects are covered.
Python I Notes Page 155
Notes: Recursion
Overview
Recursion is a tricky concept. The idea of recursion is that the function calls itself
over and over to accomplish some task.
First Pass
Pass 1: On the first call to the fact function, n = 5 and the else is performed
(plugging 5 in for n):
return 5 * fact (5 – 1)
In the return statement, the fact (5 – 1) becomes fact (4) and calls the function
again before returning anything.
Second Pass
Pass 2: On the second call to the fact function, n = 4 and the else is performed
(plugging 4 in for n):
return 4 * fact (4 – 1)
…and so on…
Once the last pass is called and n = 1, the values are plugged in and the following
value is returned to the main program:
5*4*3*2*1
Python I Notes Page 157
If a print function is placed into the program, we will see this program strip off the
last characters as it calls itself over and over:
Python I Notes Page 158
The basic idea in programming is to keep it simple. If you can program the
algorithm in a simple manner, using for and while loop constructs, this is
preferable to recursion. Recursion is not simple and, as a result, may introduce
unintended errors. Avoid recursion if possible!
Python I Notes Page 159
Notes: Modules
Overview
Creating a Module
In a separate Python file, we will write the definitions for the two functions
dealing with compound interest. We will save this file as compound_interest.py:
Python I Notes Page 160
General Form:
Notice that the .py file extension is not necessary for the Python module name.
Also notice again that the Anaconda environment brings up the parameters
(arguments) once the initial parenthesis is typed:
Even though longer variable names make the module not as concise, it helps
show the programmer what each variable is responsible for. Putting your cursor
over interest_continuous() in the line ans1 = interest_continuous() and press
CTRL-I, you will see the following:
Python I Notes Page 161
This is the same as the docstring that was input into the module. In real life, this
documentation should be very detailed, even including examples of usage. The
above example just brought in one of our functions, but both can be brought into
the program:
Now it might appear at first glance that it is still necessary to remember the
function names, but remember what happens once you type in the decimal:
The Anaconda system brings up all the global functions defined in that module.
Both types of imports can be used with other libraries. Consider this square root
example:
Instead of referencing the library name before the function, the function can be
referred to directly:
Python can save data in two different types of format: text and binary. The data
stored in a textual format can be opened and viewed with an editor. Data stored
in binary cannot be viewed in this manner since it is stored in binary format. This
course will focus on textual. The current trend is for non-database files to be
stored in a text-based format. Even Microsoft Office has adopted this strategy.
Consider the following Word document:
Python I Notes Page 164
If it is open in Notepad:
We could explore this further and see that the Word document is actually a
zipped folder of XML files, which can be viewed in an editor as in this file:
This textual-based format allows for easier transferring of data, as well as less
corruption occurring. A programmer could change one byte (corrupt) in a binary
file and the binary file would potentially be unusable. A textual format would
Python I Notes Page 166
need much more corruption for this to occur (in theory, it all depends on how the
original programmer programmed it).
The file_variable will allow the programmer to access the file object. The same
naming conventions used in variables can be used for the filename, which is a
string. Extensions are a good idea to identify the type of file! A mode is a string to
indicate how the file is to be used: ‘r’ for read only, ‘w’ for writing (file is either
created or for an existing file, then all data is removed), and ‘a’ for appending
(new data is written at the end).
Syntax:
file_variable = open(filename, mode)
Examples:
F1 = open(‘sales.txt’, ’r’)
F2 = open(‘purch.txt’, ‘w’)
Python I Notes Page 167
Example:
file_variable.close()
Python I Notes Page 168
file_variable is the variable that was referenced in the opening of the file. String
can be a literal string or a string variable. The \n will go to the next line in the file.
Remember that str will convert another data type to string:
Example:
num = 3
sales = 13.53
name=’ABC Company’
F1.write(str(num) + ‘,’ + str(sales) + ‘,’ + name + ‘\n’)
Python I Notes Page 169
Syntax:
string_variable = file_variable.read()
Syntax:
string_variable = file_variable.readline()
This method does read the file line-by-line, but would require the programmer to
know how many lines are in the file. For example, if there are five lines in the file,
the following code would extract that information:
Python I Notes Page 170
Example:
infile = open(‘video.txt’, ‘r’)
s1 = infile.readline()
s2 = infile.readline()
s3 = infile.readline()
s4 = infile.readline()
s5 = infile.readline()
infile.close()
Python I Notes Page 171
Unfortunately, most of the time the programmer does not know how many lines
there are in the file. This problem will be discussed later in this lesson.
Also the ‘\n’ that is at the end may occasionally cause problems. This can be easily
stripped out with the following code:
Example:
s1 = infile.readline()
s1 = s1.rstrip(‘\n’)
Once the readline hits the end of the file, it will be set equal to ‘’ indicating the
Example:
infile = open(‘sales.txt’, ‘r’)
str1 = infile.readline()
while str1 != ‘’:
print(str1)
str1 = infile.readline()
infile.close()
end of the file. The while loop is probably the simplest way to detect the end of
the file.
Python I Notes Page 172
Example:
infile = open(‘sales.txt’, ‘r’)
for str1 in infile:
print(str1)
infile.close()
Record Structure
Records are often stored in a single line in a file. This can be a predefined
structure.
Example:
ABCCompany 35 123.45
111111111122222222223
123456789012345678901234567890
The programmer might decide that columns 1 through to 12 are for the company
name, columns 13 through to 18 are for the customer number, and columns 19
through to 30 are for the sales amt. Remember that strings are indexed starting at
zero so, for a programmer, this structure would need to be viewed in this manner:
Python I Notes Page 173
Example:
ABCCompany 35 123.45
11111111112222222222
012345678901234567890123456789
The positions would change! Using string methods the information can be
extracted, extra spaces removed, and a data conversion to the appropriate data
type can be done.
Sometimes the data may be stored in CSV format and may look like this:
Example:
“ABC Company”,35,123.45
While not as simple as a fixed column format, the information can be extracted
using string methods. Even simpler, there is a library that would allow easier
extraction of this data, as will be shown in a demonstration video.
Python I Notes Page 174
It can be tricky to modify an existing file. The following scenarios present the
methodology to accomplish the given tasks.
In this case, simply open the file up with append mode and it is handled
automatically.
1. Open the file for reading and the temporary one for writing.
2. Loop through the file until you reach the record to be deleted. As you are
looping, write the records straight from the file to the temporary file.
3. Ignore the record to be deleted (do not write it to the temporary file).
Python I Notes Page 175
4. Continue looping through the file, writing the remaining records to the
temporary file.
5. Delete the file.
6. Rename the temporary file as the name of the file.
1. Open the file for reading and the temporary one for writing/reading.
2. Loop through the temporary file until you reach the record to be changed.
As you are looping, write the records straight from the temporary file to
the file.
3. Extract/change the information from the line as read from the temporary
file.
4. Write this new record to the file.
5. Continue looping through the temporary file, writing the remaining
records to the file.
Python I Notes Page 176
Overview
The most basic data structure in Python is the sequence. Python has several built-
in types of sequences; the most common ones are strings, lists, and tuples.
• Lists are the most versatile sequence type. The elements of a list can be
any object, and lists are mutable. Mutable objects can change their value
but keep their id(). Elements in a list can be reassigned or removed, and
new elements can be inserted.
• Tuples are like lists, but they are immutable. An immutable object has a
fixed value. If a new value is needed, a new object has to be created.
• Strings are a special type of sequence that can only store characters, and
they have a special notation. However, all of the sequence operations
described below can also be used on strings.
Sequence Operators
There are standard sequence operations that are used in Python, which include:
• Slicing
• Length
• Concatenation of Sequences
• Repetitions
Python I Notes Page 177
• Indexing
Indexing
Indexes are numbered from 0 to n-1, where n is the number of items (or
characters) that is defined in the string, list or tuple.
Example:
str[3]
returns: ‘l’
sStr[-1]
returns: ‘d’
Python I Notes Page 178
Slicing
Python makes it very easy to slice apart a string, list or tuple by using its slice
operator. A Slice is used to extract a subset of a sequence.
• If not provided:
• start defaults to 0
• end defaults to the length of the list
• increment defaults to 1
• The first colon is always necessary (to indicate Python should make a slice
rather than access a single element), but if the increment isn’t specified
the second colon can be omitted.
Python I Notes Page 179
Syntax:
mySlice[start:end:increment]
Examples:
s='abcd'
s[::2]
returns: 'ac'
s[::-1]
returns: 'dcba'
Length
Example:
Concatenation
Combines two or more sequences of the same type into a new sequence object.
Uses the + operator.
Example:
firstname = "Homer"
surname = "Simpson"
name = firstname + " " + surname
print name
returns: Homer Simpson
Example:
abc = ["a","b","c","d","e"]
"a" in abc
result: True
"x" in str
Result: False
Python I Notes Page 183
Repetitions
Example:
To repeat a string 4 times, you can do:
(3) 3 * "xyz-"
result: 'xyz-xyz-xyz-'
(4) 3 * ["a","b","c"]
Result: ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
Python I Notes Page 184
Notes: Lists
Lists Overview
A list can store multiple data items. For example, it can store 100 numbers. In
your previous programming courses, “lists” were called “arrays.” Many of the
same topics from arrays apply to Python lists.
Lists are objects which are covered later in this course, but some of the
terminologies are mentioned in this lesson.
Python I Notes Page 185
Creating Lists
• Lists are “constructed” using the “list” keyword.
L1 = [];
L2 = [1,2,3,4];
The examples above were limited to just integers but any data type can be used in
a list, as shown below.
Example:
L1 = [“Bill”, “Sally”, “Henry”];
L2 = list(“henry”);
Example 2:
L2 = list("henry");
print(L2);
L1 is fairly obvious but L2 requires a little bit more explanation. The string “henry”
is passed into the list and each letter is split up as an element.
The “list” in front of (“henry”) is what provides the conversion from the string to a
list. Strings will not be covered in much depth in these lecture notes since it was
covered previously. Remember that we treated strings as an array of characters
and could do various functions with this idea, such as slicing.
Python I Notes Page 187
List Indexing
Just as with arrays and strings, lists are indexed starting at 0. The string “happy”
would have the following indexing.
h = position 0
a = position 1
p = position 2
p = position 3
y = position 4
This is the same concept that was demonstrated with strings and in your previous
studies of arrays. The indexing always goes from 0 to the 1, less than the length of
the string or list.
Index Operator
Example:
L1 = [3,5,7,9];
To access individual items in the list, the index operator can be used.
Even though brackets indicate a list, to access the individual entries in the list also
requires brackets. Consider this example, as seen in previous programming
courses of a simplistic sort:
Example:
L1 = [5,2,1,8];
for p1 in range(0,3):
for p2 in range(0,3):
if L1[p2] > L1[p2 + 1]:
temp = L1[p2];
L1[p2] = L1[p2 + 1];
L1[p2 + 1] = temp;
print(L1);
Note: The use of the brackets is used to refer to the entries inside
the list.
L1 = [];
resp = "Y";
whileresp != "N":
resp = input("Input next number, N to indicate no more
numbers: ");
ifresp != "N":
L1.append(int(resp));
Note: The
total use of the append method.
= 0;
for x in range(len(L1)):
total = total + L1[x];
Remember that lists are objects which are different to the concept of “arrays”. If
print("The sum was", total);
you find yourself wondering how to do something with lists, then try entering the
list name followed by a decimal:
Python I Notes Page 190
The benefits of a robust IDE is that the need for memorization of different
programming topics is minimized.
Out of Bounds
One very common error is to try to access outside of the “bounds” for the list.
L1 = [];
L1[0] = 5;
Note:
L1 - Creates an empty list
L2 – Creates an out of bounds error
A logical assumption is that the second line would add a new element to the
list and dynamically increase the size. It does not. It gives an out of bounds
error. The proper code would be:
Example:
L1 = [];
L1.append(5);
When working with lists, a programmer will have less difficulty if they remember
that lists are objects!
Python I Notes Page 192
For Loop
As seen in the earlier example, a for loop must go from 0 to one less than the
length of the list! This can be simplified in Python using the following code:
Example:
for index in range(len(L1)):
print(L1[index]);
Remember what the range function does and it handles this situation perfectly for
lists!
Python I Notes Page 193
For each pass through the loop, u will contain the value of the next element in the
Example
L1 = [3, 5, 7, 9];
for u in L1:
print(u);
Many of the same programming topics that were covered in strings also apply to
lists, which makes sense if you think of a string as a list of characters.
List Slicing
This will return a list starting at the index = start and ending at the index = end –
1:
Python I Notes Page 194
Example:
L1 = [1, 5, 13, 15, 21];
L1[2 : 4] would return [13, 15]
Note: This returns a list that could then be assigned to another list
variable.
Leaving off the start or end will extra all list entries from the beginning of the list
or the end of the list.
+ Operator
Example:
L1 = [1, 2, 3];
L2 = [4, 5, 6];
L3 = L1 + L2;
L3 would not contain [1, 2, 3, 4, 5, 6]
* Operator
Example:
L1 = [1, 2, 3];
L2 = 2 * L1;
L2 would not contain [1, 2, 3, 1, 2, 3]
Keep in mind that this does multiply everything in a list by a number but also
replicates the list!
Python I Notes Page 196
in/not in Operator
Example:
L1 = [1, 2, 3];
if 2 in L1:
print(“2 was on the list!”);
The in operator provides a much easier interface than writing a search routine to
search through the list.
List Methods
As has been mentioned, a list is an object that has various methods associated
with it. Here is a table of the methods:
Python I Notes Page 197
Sometimes the programmer may find it of benefit to split a string up into a list, to
take advantage of the methods of the list object.
Example:
colors = “red blue yellow”.split();
The split method for a string will break a string into a list based upon the
delimiter.
Consider the last line of code that uses the pipe symbol as a delimiter:
Example:
colors = "red|blue|yellow".split("|");
print(colors);
As can be seen, this is a very powerful feature. Consider the following code:
Python I Notes Page 198
Example:
L1 = "3|'david'|5".split("|");
print(L1);
sum = L1[0] + L1[2];
print (sum);
Since 3 and 5 are numbers, then we expect that our sum would equal 8 but the
result gives:
Example:
s = input ("input 5 numbers with spaces between them");
L1 = s.split();
L1Numbers = [eval(x) for x in L1];
print(L1Numbers);
Python I Notes Page 199
Copying a List
Example:
L1 = L2;
This may appear to copy one list to another but instead creates references that
will complicate the Python program.
The procedure for passing in a list/returning a list versus a variable is the same,
but lists by default come in as mutable (which means the function can change the
value of the list directly). Sometimes creating a function that does not know the
type (in the design mode of the Anaconda system) causes the methods to be
unavailable. Consider this code once I press the decimal:
No methods appear as the function does not know the data type. This does not
mean that you cannot use the methods of the function, just that you will not get
that extra help from the IDE.
Python I Notes Page 201
def sort(L):
L.sort();
L = [5, 2, 1, 3];
sort(L);
print(L);
As can be seen, it works fine but I had to remember the sort method of the list.
Notice how confusing this code example would be. I have a function called sort
and a method called sort.
Now we have a function, method, and parameter called sort. Very confusing. A
better way to write this code example would be:
deflistsort(L1):
L1.sort();
L = [5, 2, 1, 3];
listsort(L);
print(L);
Multidimensional Lists
Lists can be embedded into another list for multidimensional lists (arrays):
Example:
numbers = [
[1, 3, 5, 7],
[9, 11, 13, 15]
];
Example:
numbers = [
[1, 3, 5, 7],
[9, 11, 13, 15]
];
Notes: Tuples
Overview
A tuple is a Python sequence object that is similar to a list. The main differences
between a list and a tuple are:
Creating Tuples
Syntax:
tuple = ([data]);
Examples:
tuple (‘abc’)
returns (‘a’,’b’,’c’)
tuple ([1,2,3])
returns (1,2,3)
tuple ();
returns nothing
tuple (1,);
Tuple Operations
We learned about the Sequence Operations in the previous lecture. The following
is the list of Tuple operations and how they apply to Tuples.
Operation Meaning
Operation Meaning
Operation Examples:
Length
Tuplen((1, 2, 3))
result 3
Concatenation
Tupconc(1, 2, 3) + (4, 5, 6)
result (1, 2, 3, 4, 5, 6)
Repetition
Tup Rep ('Hi!',) * 4
result ('Hi!', 'Hi!', 'Hi!', 'Hi!')
Membership
TupMemb3 in (1, 2, 3)
Result True
Iteration
Tup Iterationfor x in (1, 2, 3): print x,
result 1 2 3
Python I Notes Page 209
Because tuples are sequences, indexing and slicing work the same way for tuples
as they do for strings and lists.
• Tuples indices are zero-based, just like a list, so the first element of a non-
empty tuple is always t[0].
• Negative indices count from the end of the tuple, just as with a list:
Examples:
Assuming following input: Data = (‘test”, ‘Test’, ‘TEST’)
Data[2]
result = TEST
Data[-2]
result = ‘Test’
Data[1:]
result = [‘Test’, ‘TEST’]
Example 2:
Months = (‘Jan’,‘Feb’,‘Mar’,‘Apr’,‘May’,‘June’, \
‘Jul’,’Aug’,’Sep’,’Oct’,’Nov’,’Dec’)
Note: The \ symbol allows the first line to carry over to the next
line. It allows for readability.
The code is then organized in a numbered list starting with zero, in the order the
values were entered. The list Months would look like this:
Index Value
0 Jan
1 Feb
2 Mar
3 Apr
4 May
Python I Notes Page 211
5 Jun
6 Jul
7 Aug
8 Sep
9 Oct
10 Nov
11 Dec
No Enclosing Delimiters
Examples
print'abc',-4.24e93,18+6.6j,'xyz'; x, y =1,2;
result: abc-4.24e+93(18+6.6j) xyz
print"Value of x , y : ",x,y;
result: Value of x , y :12
i.e. brackets for lists, parentheses for tuples, etc., default to tuples, as indicated in
these short examples.
Python I Notes Page 212
Updating Tuples
Tuples are immutable, which means you cannot update or change the values of
tuple elements. You are able to take portions of existing tuples to create new
tuples, as the following example demonstrates:
tup1 =(12,34.56);
tup2 =('abc','xyz');
tup1[0] = 100;
Removing individual tuple elements is not possible. There is, of course, nothing
wrong with putting together another tuple with the undesired elements
discarded. To explicitly remove an entire tuple, just use the del statement:
Python I Notes Page 213
Example:
tup=(python,'physics',2015,2016);
printtup;
deltup;
print"Deleting tuple : "
printtup;
result: ('python', 'chemistry', 2015, 2016)
Deleting tuple:
Traceback (most recent call last):
File "test.python", line 9, in <module>
printtup;
NameError: name 'tup' is not defined
Higher order functions imply that function names can be passed into functions.
This feature is powerful in certain mathematical and engineering applications. The
average programmer would not see the benefit of using this feature.
Consider the composition of functions from college algebra. Given the following
functions: f ( x) 8x 2 and g ( x) 9 x 3 , the composition of f and g is given by the
following notation: f g . In order to solve this problem, one function needs to be
passed into another function, as in these steps:
f g
f (g)
89 x 3 2
72 x 24 2
72 x 26
Consider another example of solving equations that are quadratic in form. Such as
the following example:
2 1
x 3 5x 3 6 0
u 2 5u 6 0
Python has a powerful library called SimPy. The goal of this library is to have a full-
featured computer algebra system (CAS). Higher-order functions allow for a
simpler development of certain mathematical topics.
Example:
def f(x): Note: The calling
within the
print
return 8 * x + 2 function:
composition(
def g(x): f,g,4).
return 9 * x + 3
def composition(f1,f2,val):
return f1(f2(val))
print(composition(f,g,4)) # f(g(4))
The f and g are actual function names. Within the composition function, the
return statement has:
f1(f2(val))
Python I Notes Page 216
defprintresults(n1,n2,f):
print("answer is ", f(n1,n2));
printresults(8,12,sympy.gcd);
printresults(8,12,sympy.lcm);
printresults(8,12,sympy.primefactors);
Typically, the idea in programming is to keep it simple, so the feature exists if the
algorithm is tricky and can only be done with higher order functions or
mathematical programming, then you can apply the concept. Otherwise, it is best
to avoid higher order functions and use a few more lines of code to make the
program easier to read.
Python I Notes Page 217
Overview
Dictionaries are objects that provide a key/value lookup capability. Typically these
are values that are often accessed in a Python program and usually never change.
For example, a state lookup might use dictionaries.
These are static values that typically never change or are loaded in at runtime; it is
assumed that no change will occur during the time that the program is up and
running.
• While sales tax may change, it would never change in the middle of the
day.
• Assuming that the program is run in the morning, then it would load in the
sales tax at startup and always have the current rate.
Obviously, a value that changes often, such as store sales, would be a poor choice
for a dictionary lookup. Assuming your system has some kind of database
capability, then database lookups are usually the preferred choice. A huge
dictionary in a Python program would consume memory and be inefficient.
Python I Notes Page 218
Dictionary: Creation
Synatax:
dictionaryname = {key:value, key:value, etc.}
Examples:
states = {“KS”:”Kansas”, “MO”:”Missouri”};
rate = {};
Syntax:
Dictionaryname[key] = value
Examples:
states[“OK”] = “Oklahoma”;
states[“MO”] = “Missouri”;
These examples will look for the appropriate key (“OK”, “MO”, etc.).
• If it finds that key then it will change the current value to the specified
value.
• For example: states[“OK”] = “Oklahoma”; looks for the key “OK” and if it
finds it then it replaces the value with “Oklahoma”.
• If it does not find the key, then it adds the key/value combination of
“OK”/”Oklahoma” (without the quotations, of course).
These do not have to be hardcoded but could be loaded in from a file, database,
or user input. Imagine a website that had a parts inquiry screen. The viewer of
that webpage could input a part and it would return the quantity on hand or
perhaps the price. Dictionaries would allow this lookup. Though obviously a large
retailer with a million items on their webpage would not use dictionaries but a full
database system.
Python I Notes Page 220
Syntax:
delDictionaryname[key]
Example:
del states[“OK”];
Deleting entries is fairly self-explanatory. Keep in mind that, while these examples
are hardcoding the state, the Python program could be driven off from user input!
Syntax:
Dictionaryname[key]
Example:
print(states[“OK”]);
str = states[“KS”];
Dictionary values can be retrieved and used in other functions, or in various parts
of a Python program.
• The print state looks up the key value “OK” and returns the value to the
print function.
Python I Notes Page 221
Example:
states = {"OK":"Okahoma","KS":"Kansas"};
print(states["OK"]);
str = states["KS"];
print(states["MO"]);
“MO” is not in the dictionary and returns the following error message:
File "//exchsrv2003/Docs$/HaysD357147/My
Documents/Python Scripts/untitled3.py", line 11, in
<module>
print(states["MO"]);
KeyError: 'MO'
Python I Notes Page 222
Example:
for key in states:
print(states[key]);
Note: There is no hardcoding of the keys, but the for loop simply
goes through all the “keys” and prints the values.
Syntax
len(dictionaryname)
In some cases, the size of the dictionary should be monitored to make sure that it
is not becoming too large.
• If some issue is arising, then the computer program could email the
support team to let them know what is occurring.
• Sounds kind of silly, but parts of the production floor were very resistant to
change and many of our programs that were installed were not being
used.
Example
states = {“KS”:”Kansas”,”MO”:”Missouri”};
if “KS” in states:
print(“Found Kansas!”);
It is not a bad idea to check to see if the key exists in the database prior to using it
in the program. This avoids key errors or having to implement exception handling
for this issue.
Python I Notes Page 224
Dictionary: Methods
Most of these entries are self-explanatory. The entry on the right side of the colon
is the data type returned. For example, the clear() method returns nothing.
Python I Notes Page 225
Overview
General ledger entries are read into a Python program and loaded into the
database. The record structure should look like these examples:
• Open the input file, delete or correct that line, and run the program again?
• Fix the problem temporarily and then code exception handling into the
program.
At one company where I worked, the programmer would temporarily fix the
problem and get the program running again. No effort was put into solving the
reason that it happened. This was not laziness because of the priority of other
projects. The company made a determination that the correction of this “error”
Python I Notes Page 226
System-defined
Assertions
Assertions are simple one-line exception handling meant to check the status of a
Boolean condition, print the result, and then terminate the program.
Example:
num = “two”
n = int(num)
• The first line creates a string variable called num and assigns the value
“two” to the variable.
• The value “two” is not an integer, so the program would terminate with a
system error.
• The Python program might give a user-friendly message about the data
type and then terminate, or perhaps ask the user to re-enter the number.
Common Errors
Some common locations for exception handling in a program are during data
conversions, file functions, and database access.
The reason for this specific focus is that these are where the majority of the errors
occur. Access to outside components can bring in unexpected results. The main
reason is that these components are outside the control of the Python program.
Outside Components
The following are situations of outside components that can cause errors:
In many cases, this method does not work. For example, how can you simulate a
database reaching the limits of the size? In many cases, the programmer must use
standard exception handling code and individually handle those cases when they
occur. This is perhaps the exception handling code that is recommended by the
vendor of the software product. In some cases, it is simple enough that it does
Python I Notes Page 230
not need to be tested. Unfortunately, this idea causes many headaches. Code
simple enough for no testing often fails but sometimes faith is necessary, as in the
database example above.
Example:
General Form:
try
<code>
exceptExceptionType as ex:
<handler>
else:
<code>
finally:
<code>
Python I Notes Page 231
The indented code following the try is performed. If a system error occurs, then
the program immediately goes down to the lines containing “except.” The general
form above only has one except, but the Python program may have numerous
except – one for each system error. The “ex” is a variable name that allows access
to further detailed information. The form could just have “except” with no
exceptiontype. The else clause handles all the exception handling that is not
covered by the exceptiontypes. The finally clause is code that occurs after the
particular clause is performed.
Example:
try:
num = int(input(“Enter a Number:”))
print(num)
exceptNameError as e1:
print(“Exception occurred: “, e1)
This particular example checks for a “NameError” and handles the error. Keep in
mind that the appropriate error must be checked. If the programmer wanted to
check to see if the data type was correct, then the error that should be checked is
“ValueError.”
Python I Notes Page 232
Try:
num = int(input("Enter a Number:"))
print(num)
exceptValueError as e1:
print("Exception occurred: ", e1)
If the programmer just wishes to generically handle all errors, then the error code
can be left off, as in this example:
Try:
num = int(input("Enter a Number:"))
print(num)
except:
print("Exception occurred ")
Example
If your program is expecting a file to be read in, then check for the
existence of the file prior to opening it.
Python I Notes Page 233
In the early days of Internet access, a program was written to send out emails for
customer leads. If the Internet connection was down, the program would store
the emails in a database and keep checking the status of the Internet connection.
This exception handling was necessary at that time due to the uptime of the
Internet connection.
• Find and write all emails that were improperly formed into a file on the
computer (not even into a database table!).
• The file eventually became too large and crashed the program.
• The exception handling was done right but the handling of the bad data
was not considered.
Python I Notes Page 234
Try:
F1 = open(filename, 'r')
for str1 in F1:
print(str1)
F1.close()
exceptFileNotFoundError as E1:
print("File is missing " + filename + "Err: " + E1.strerror)
choice = input("Do you wish to create the file? (Y or N): ")
if choice == "Y":
F1 = open(filename,'w')
F1.close()
This example will be discussed in the demonstration, but the idea is that we are
treating the exception handling like a simple if statement:
Python I Notes Page 235
This code does not seem to reflect a goto situation but more of an if-else
condition. The usage of exception handling should not cause “astonishment.”
It might be tempting to use exception handling for clever coding, but remember
that another programmer may have to look at your code in the future. Perhaps
even yourself!
“Keep it Simple”
When a Python script comes across a situation it is not “coded” to handle, it raises
an exception. First, we need to understand what an exception is. An exception is
an event that occurs during the execution of a program, which interrupts the
otherwise normal flow of the program’s instructions. When the script raises an
exception, it has to be handled immediately or the script will terminate. It is,
therefore, considered as “bad programming” to use exception handling to control
the flow of your program.
Exceptions should be reserved for exceptional situations, not for everyday normal
flow control. Most programmers agree that you should use exception handling for
handling unanticipated/exceptional situations, not as normal program flow.
Python I Notes Page 237
Usually, exceptions are useful for allowing the programmer to concisely state two
control flows, which are independent of each other, in the same piece of code:
one if the error happens and one if the error doesn’t occur.
There are a few occasions when it is “more” acceptable to use exceptions than
others. For instance, during a busy day, you encounter a situation you don’t have
time to deal with right now. You don’t have enough information to deal with the
issue correctly the first time. In this case, if writing in a try/except works to bring
everything back online and working, all well and good. However, you should write
quality documentation on what you did and why, to allow for you or someone
else to further investigate and correct the problem later.
Python I Notes Page 238
Notes: Assertions
Overview
Assertions are very short pieces of code used to check the internal status of your
Python program.
• They might check the status of a dictionary entry or the type of a variable.
Syntax:
assert <Boolean condition>, “programmer written error
message”
Example:
class MyDB:
def __init__(self):
self._id2name_map = {}
self._name2id_map = {}
• This is the idea of asserts: they check a simple condition and terminate if
the condition is “false.”
Example:
def add(self, id, name):
assert type(id) is IntType, "id is not an integer: %r" % id
assert type(name) is StringType, "name is not a string: %r" %
name
Overview
A Real-Life Example:
I took a report that cost an internal customer $500 per run and
reduced it to $2 per run. I felt good about my work, but when I
went to another customer, who was more knowledgeable on
business concepts, and told him what I had done and wanted to
examine their programs for efficiency improvements, he asked me
if the mainframe was leased. I replied that it was. He then asked if
the leased price had gone down. It had not. He pointed out that I
merely caused the cost of the CPU cycle to increase and that I did
Python I Notes Page 244
not save the company $250,000 since we were still paying the
same price to the vendor. He was correct! He said that sometimes
a company getting their result faster might result in profit but,
otherwise, my efforts were not beneficial. He was not being mean
but helping me to understand. He said that he had other projects
that he would rather I work on, which would be profitable for the
company. It taught me a valuable lesson that efficiency has to be
viewed in terms of the larger picture of the company.
• 5 seconds to wait for a process that is run only on Monday mornings would
be acceptable!
about in 5 years’ time, when there are 10,000,000 records in the database?
Proper consideration of efficiency might prevent major headaches in the future!
It is difficult to anticipate what the effects will be in the future since they have
never occurred before! A theoretical approach allows for a measure of the
efficiency. The big O notation is a function that measures the complexity of the
algorithm based on input size.
Linear Search
• Each number is compared to the search value.
Example:
for i in range(10):
sum = sum + i;
for i in range(n):
for i in range(30):
sum = sum + i;
Python I Notes Page 247
𝑇(𝑛) = 𝟏𝟎 × 𝒄 + 𝟑𝟎 + 𝒄 × 𝒏 = 0(𝑛)
𝟑𝟎 + 𝒄 × 𝒏 refers to the second main for loop which loops from 0 to n-1
• It goes from 0 to 29 or 30 iterations for each value of the outer for loop.
• This explains 30 + 𝑐 × 𝑛
for i in range(n+1):
for j in range(n):
for k in range(n+5):
What is C?
The question becomes: what is c and how to figure it? This is a hard concept.
• What are the typical applications that will be running when the Python
program is run?
These are just some of the considerations that must be addressed. Some analysis
just assumes a perfect world with nothing else running, as there are too many
variables to consider. Many users of computers have purchased new computers
and noticed the startup time decreasing significantly when logging into their
computers in the same operating system. Some of this can be explained by
programs running at startup on the old computer but not on the new computer.
Much of the improvement is due to increased hardware speed.
Python I Notes Page 249
Overview
Black Box Testing is a testing method where no knowledge of the inner workings
or code is known. The easiest way to think of this concept is for a user to use the
computer program. This testing might be done by a user of the system or by the
actual programmer. Either way, the tester assumes no knowledge of the program,
just the interface.
The black box refers to the tester not being able to see inside the
box.
The tester will then see if the program works according to the specifications and
how it reacts in various scenarios.
Python I Notes Page 250
Examples:
What happens when I put in a negative number?
What happens when I put a letter in the program?
What happens when I put in too large of a number?
What happens when I put in a decimal number?
It is important that the tester document the exact inputs and the result that
occurred so that the programmer can recreate the problem.
• All-pairs testing
• Equivalence partitioning
• Cause-effect graph
• Error guessing
Python I Notes Page 251
Not all the techniques will be used in every Black Box Testing scenario. The
criticalness of the application will help determine the depth of the testing.
Overview
The glass box name implies that the tester can see “inside the box”
and know all the details of the programs, as well as the
supporting components such as files, databases, etc.
For example, a Python program might have an intricate “if” structure with many
decision paths. Proper testing would make sure that each decision path functions
as required.
Python I Notes Page 253
Example:
if grade >= 90:
print(“You received an A”);
elif grade >= 80:
print(“You received a B”);
elif grade >= 70:
print(“You received a C”);
And so on
It would be important for the tester to have test cases to check all the different
branches of the if structure. For example, the tester might input 92, 83, 75, 62,
53, etc., to check all the different paths possible in the if structure.
Perhaps the application is based on a database. The tester might want to check all
the conditions that would happen if something is wrong with the database. This is
easier said than done. How do you simulate corrupting a database? Sometimes
the error conditions that happen cannot be explained and, as a consequence,
cannot be recreated.
Although many individuals will indicate that testing should be thorough and
complete, this is not actually the case. The criticalness of the application, delivery
date, support issues and many other factors help to dictate the depth of the
testing.
Python I Notes Page 254
Consider how much testing is required for an ATM program compared to a simple
Excel VBA macro for one user.
Improper testing on the ATM program could cost a bank a huge loss, while
improper testing on the VBA macro may be a minor loss. Keep in mind that a VBA
macro error could be very costly, depending upon the criticalness of the
spreadsheet!
Python I Notes Page 255
• Branch testing
• Statement coverage
• Decision coverage
• Path testing
Most programmers do most of the above techniques but do not actually write
them down.
Overview
The Python program is not working correctly; what can the programmer do?
Example:
if sales > 30000;
commission = sales * 0.10;
5. Keep it simple: If you are trying to be clever or concise with your code
then change the code. Clever code should be left for the times when you
truly need to be clever in programming. The majority of programming is
repeating the same code over and over for different circumstances. For
example, for five years I always did the same generic code for database
access.
Example:
Tempquery.close;
Tempquery.clear;
Tempquery.sql.add(“SELECT * FROM SALES;”);
Tempquery.open;
6. IDE features: Breakpoints, variable watches, and many other features are
available in IDEs. Explore these options if you plan to get serious about a
particular development platform. These features could save hours and
hours of debugging.
Python I Notes Page 258
7. Another pair of eyes: I have spent hours looking for an error and then
breaking down, having another programmer look at the code and
immediately seeing the problem.
• Another pair of eyes also means that you set aside the program and
take a break from the examination.
• Work on some other project or get a good night’s sleep! Sometimes
then the problem is obvious.
The above are just a few ideas. This is not the complete list but just ones that I
used in my years of programming.
Python I Notes Page 260
Notes: Classes
Overview
Objects should not be used in every circumstance though (despite what Smalltalk
indicates)!
Python I Notes Page 261
Syntax:
classClassName:
initializer
methods
Example:
classRect:
def __init__(self,length=1,width=1):
self.length = length;
self.width = width;
self refers to the object invoking the particular method.
Python I Notes Page 262
Length and width are the parameters. Since these are set equal to one then, if the
programmer does not pass in any parameters, the value of length and width will
be equal to one.
Every function in a class definition must have the word “self” as the first
parameter. This is not actually passed in when you call it but must be there.
Python I Notes Page 263
Synax:
objectRefVar = ClassName (arguments)
Example:
import Rectangle as r
r1 = r.Rect(3,5);
r2 = r.Rect();
Python I Notes Page 264
The first instance r1 is created passed in 3 and 5 as parameters, which means its
instance variables are self.length = 3 and self.width = 5. The second instance r2 is
created passing with no parameters, which means that its instance variables
default to the value one 1: self.length = 1 and self.width = 1.
Example:
r1 = r.Rect(3,5);
print(r1.length);
r1.length = 8;
print(r1.length);
Example:
classRect:
def __init__(self,length=1,width=1):
self.length = length;
self.width = width;
defgetLength(self):
returnself.length;
defsetLength(self,param_l):
self.length = param_l;
Python I Notes Page 265
Many programmers adhere to the standards of using get and set methods to
change the value of the instance variables.
The standard allows anybody who has worked with objects to easily understand
the purpose of these two methods. The “get” method usually just returns the
value of the instance variable, while the “set” method usually just sets the value
of the instance variable. The code to then access the instance variables would
follow the example below.
Example:
classRect:
def __init__(self,length=1,width=1):
self.length = length;
self.width = width;
defgL(self):
returnself.length;
defsL(self,param_l):
self.length = param_l;
Python I Notes Page 266
Think of the interface of a television. The user could open up the back of the
computer and manipulate the circuit boards to change the channel, but the
designers did not wish the user to access it in this manner. A remote control
and/or buttons were implemented to provide a “safe” interface. The “get” and
“set” methods perform the same function.
Methods
“Get” and “set” methods are not the only pieces of code that can be implemented
for a class. Various methods can be defined to accomplish various tasks.
Example:
classRect:
def __init__(self,length=1,width=1):
self.length = length;
self.width = width;
defgetLength(self):
returnself.length;
defsetLength(self,param_l):
self.length = param_l;
defcalcPerimeter(self):
return 2 * self.length + 2 * self.width;
defcalcArea(self):
returnself.length * self.width;
Python I Notes Page 267
Notice that the last two methods have been implemented to calculate perimeter
and area. Notice that self.length and self.width are not passed in as parameters.
The instance of the object already knows its own instance variables. This is a
similar idea of an instance of a human being already knowing their name, age, etc.
Example:
print(r1.calcArea());
print(r1.calcPerimeter());
def __init__(self,length=1,width=1):
self.length = length;
self.width = width;
self.area = length * width;
self.perimeter = 2 * length + 2 * width;
Then each time the length or width have changed the area and perimeter would
have to be recalculated:
defsetLength(self,param_l):
self.length = param_l;
self.area = self.length * self.width;
self.perimeter = 2 * self.length + 2 * self.width;
Overview
The breakdown can be a logical breakdown that has been seen in past courses or
any definition.
Examples:
Company
- Department
- Employees
- Buildings
- Office Equipment
Example:
- Desks
House
- Chairs
- Rooms
- Dining Room
- Living Room
- Bedroom
- Bathroom
- Residents
This breakdown does not indicate that there will only be one bedroom or
bathroom, it is merely defining the “types” of rooms.
• The key is whether the object definition can be applied to every instance
of the object.
• This does not apply to every instance across the entire universe.
• Perhaps there are houses that do not have the same structure.
House
- Rooms “with instance variables of type (dining, living, sleeping,
bath, etc.)”
- Residents
Python I Notes Page 272
Types
By having the type defined as an instance variable, the object definition is more
flexible.
character
hero
villain
Python I Notes Page 273
Polynomial
Polynomial
- Monomial
- Binomial
- Trinomial
Example:
class Polynomial:
def __init__(self,degree=1):
self.__degree = degree;
defgetDegree(self):
returnself.__degree;
Trinomial
import math
from Polynomial import Polynomial
Python I Notes Page 275
class Trinomial(Polynomial):
def __init__(self,a=1,b=1,c=1):
super().__init__(2);
self.__a = a;
self.__b = b;
self.__c = c;
defgetZeros(self):
ans1 = (-1 * self.__b + math.sqrt(self.__b**2 - 4 * self.__a *
self.__c))/(2 * self.__a)
ans2 = (-1 * self.__b - math.sqrt(self.__b**2 - 4 * self.__a *
self.__c)) / (2 * self.__a)
return ans1, ans2
Stored in a file called Trinomial.py
Python I Notes Page 276
class Trinomial(Polynomial):
The super refers to the class it is inherited from and the __init__ calls the
initializer method (construct the instance).
Example:
import Trinomial as t
t1 = t.Trinomial(1,-5,6);
print("zeros are %.2f and %.2f" % t1.getZeros());
print("The degree was %d" % t1.getDegree());
Python I Notes Page 277
The trinomial class specifically refers to a quadratic. The definition could have
gone as follows:
Polynomial
- Monomial
- Binomial
- Trinomial
- Quadratics
But the question becomes whether every quadratic is a trinomial? Can a quadratic
be a binomial or even a monomial? Proper design of inheritance takes much time
and effort. An improper design could set up an infrastructure that creates many
headaches down the road!
Python I Notes Page 278
Overview
Consider a television set. The inner workings of the television set are hidden from
the user. Buttons allow the user to turn the television on/off or to change
channels. Imagine if the user had to understand all the inner workings of the
circuitry prior to use!
Abstraction
Class abstraction is the separation of the class implementation from the use of
the class. The creator defines the methods that can be used and how to use them.
Examples:
TV.PowerOn()
TV.PowerOff()
TV.ChangeChannel(channelNumber)
TV.ChangeVolume(volumeNumber)
• To call the method “ChangeChannel,” the user would need to pass in the
channelNumber.
Python I Notes Page 279
• This defines the class implementation (if all the code was placed with the
methods).
For example, Nielson states that there are 115.6 televisions in the
United States in 2014.
There are 115.6 instances of a television object in the United
States.
This is the usage, while the example above would just describe the interface.
Encapsulation
• This separation of the class implementation from the use of the class is
referred to as class abstraction. This class abstraction can be seen in the
following diagram from the book:
Python I Notes Page 281
Data Attributes
Overview
Example.
classRect:
def __init__(self,length=1,width=1): Note: The instance
defgetLength(self):
returnself.length;
defsetLength(self,param_l):
self.length = param_l;
self.area = self.length * self.width;
self.perimeter = 2 * self.length + 2 * self.width;
defgetArea(self):
returnself.area;
defgetPerimeter(self):
returnself.perimeter;
Python I Notes Page 284
import Rectangle as r
r1 = r.Rect(3,5);
print("area =",r1.getArea());
r1.length = 10;
print("area =",r1.getArea());
print("length =",r1.length);
In reality, we would only like instance variables to be accessible via “get” and
“set” methods. Otherwise, it is possible to directly change the length, but area
and perimeter do not get updated! The designer of the object would like to make
sure that the object does not have the integrity violated. This can be
accomplished with “information hiding.”
Obviously, it is not desired that the programmer access the instance variables
directly, but should use get and set methods to change the values. In this manner,
the designer of the object can make sure that the object functions as desired. Two
underscores before the name of the instance variables and/or methods will make
them private, so they can only be accessed within the object and not by programs
that have created an instance of the object!
Python I Notes Page 285
This is misleading. You can still see them from the Python program that
implements the object, but they cannot be changed and that is the important
component.
Overview
The main objective of this lesson is to analyze a formula and think in terms of
algorithms.
Sample Mean
Uppercase sigma means to sum up the values. Summing brings up more questions
before this can be programmed.
In our case, since the code is intended for a library, lists should hold the numbers!
The uppercase sigma implies looping. For and while loops will accomplish the
same task in many cases (but not all). While loops are best when the programmer
Python I Notes Page 287
does not know the exact count of numbers (n). For loops introduce fewer errors
in a program compared to while loops, which give the programmer complete
control. For the library that is going to be created, the “for” loop will be used due
to the simplicity and reduction of errors.
Consider a simple example. The programmer does not have to do the complete
example but just enough to understand the logic of the algorithm.
Example
x 3 8 16 27
(This will be accomplished by having a sum variable that is accumulated while the
list is traversed with a for loop.)
After that piece of code, the mean is a simple calculation that is returned to the
user.
Python I Notes Page 288
Programming Considerations
No matter what programming language is used, the programmer should ask basic
questions (that vary depending on the formula/algorithm/procedure):
Median
Again our library will deal with a series of numbers. so these will be kept in a list
so that our library can be used in many situations. For the library, the sort method
of lists will be used to perform the sorting. It would be overkill for our example to
use some sorting technique such as bubble sort, quick sort, etc.
Python I Notes Page 289
Example 1
data: 1, 5, 7, 9, 21
there is an odd number of data values which can be determined by
the following code:
n % 2 == 0 means even count of data values
n % 2 == 1 means odd count of data values
In order to determine the middle number in this setup, we would use the
following calculation:
Example 2
data: 1, 5, 7, 9
Much of the code above still works but with n / 2 would give 4 / 2 = 2, which
would be the 7 (remember indexing starts at 0). So we would need that position
and the one before it. Once we have the 5 and 7 (the middle two numbers), we
can add them and divide by two.
Programming Considerations
Our same considerations hold true to a dataset coming into the program and lists
will again be used. The sample mean will call the function that was already
created above. A for loop will again be used to accumulate. The square root
function will need to be used for this algorithm.
Python I Notes Page 291
Programming Considerations
While a simple example can be done, it is obvious that we will go through the x
values in the dataset, calculate the mean and then go back through the x values
calculating ( x x ) 2 and summing the result.
Python I Notes Page 292
Standard Variance
Programming Considerations
Standard Deviation
The Standard Deviation formula is the square root of the Variance. Variance is the
average of the squared difference from the Mean. The variance is computed as
the average squared deviation of each number from its mean. All of this means
that, if you don’t have the “Mean” right, nothing else is going to add up either.
Remember that mean, median, and mode are measures of central tendency.
These measures will give you information on the data values of the center of the
dataset.
Variance (S2) = average squared deviation of values from mean
Standard deviation (S) = square root of the variance
Think it through!
• If all of the data in a set is the same value, the standard deviation is zero
because each value equals the mean
When you need to add numbers, check to see if there is a sum function or
method already within the language to handle it. Review the list of Python’s built-
in functions.
Built-in Functions
Overview
The frequency distribution merely consists of adding how many numbers are in
each class so that there is no formula to program.
Factors to consider
1. One key question is whether the various classes are input by the user or
generated by the program. Considerations are code reuse and ease of use
by the user of the system.
2. For purposes of this example, the user will input the starting lower class
limit, class width, number of classes, and then the actual numbers.
After the first one, the next LCL and UCL limits will be calculated using:
After each class is built, then the list of data values will be looped through seeing
how many are >= LCL and <= UCL.
Programming considerations
Programming Considerations
Realize that the questions that arise based upon different algorithms are not all
valid! For example, a sum function would not benefit the program at all in the
binomial probability distribution. But these are questions that should mentally be
answered prior to programming.
Python I Notes Page 300
Probability Simulations
This would indicate that the first child was a girl (0), the second child was a boy
(1), and the third child was a boy (1). Now adding these numbers: 0 + 1 + 1 = 2,
which would mean 2 of the three children were boys!
Python I Notes Page 301
Programming Considerations
𝑥1− 𝑥̅ 𝑦1 − 𝑦̅
∑(
𝑠𝑥 ) ( 𝑠𝑦 )
𝑟=
𝑛−1
From previous functions, the sample mean and sample standard deviation have
already been placed into a function in our library. This formula will require two
sets of data that will be placed into lists. The sample mean and sample standard
deviation of both lists will be obtained and then a for loop will be used to
calculate the numerator of the above formula.
Programming Considerations
𝑦̂ = 𝑏1 𝑥 + 𝑏0
𝑤ℎ𝑒𝑟𝑒
𝑠𝑦
𝑏1 = 𝑟 ∙
𝑠𝑥
𝑏0 = 𝑦̅ − 𝑏1 𝑥̅
(slope)
(y – intercept)
This will be fairly simple to implement since the correlation coefficient (r), the
sample mean, and the sample standard deviation functions have already been
written in the library. This will just do basic formulas and return the results.
Python I Notes Page 303
Programming Considerations
𝑝̂ (1 − 𝑝̂ )
𝑝̂ − 𝑧𝛼⁄2 ∙ √
𝑛
Lower bound:
Upper bound: 𝑝̂ (1 − 𝑝̂ )
𝑝̂ − 𝑧𝛼⁄2 ∙ √
𝑛
Python I Notes Page 304
x
This formula is straightforward with pˆ . The only tricky part is the critical
n
Critical Values
Critical
Confidence
Value
Level
(Z-score)
0.90 1.645
0.91 1.70
0.93 1.81
0.94 1.88
0.95 1.96
0.96 2.05
0.97 2.17
0.98 2.33
0.99 2.575
There are many options for programming this lookup. This library will use the
dictionary component of Python to provide a simple and easy lookup scheme.
Python I Notes Page 305
Programming Considerations
Our library will calculate only one component of one type of claim. Consider the
following steps in testing claims about population proportions:
Python I Notes Page 306
𝑥
The test statistic will be programmed. This is a fairly simple program with 𝑝̂ = 𝑛.
Programming Considerations
1. Usually, an entire claim is not done in a library, but the library provides
various components such as the critical value, test statistic, or p-value,
depending upon the approach
2. Do I put this in a library or an object?
Python I Notes Page 308
Correlation Coefficient
Overview: The sign and the absolute value describe the direction and the
magnitude of the relationship between two variables.
The greater the absolute value the stronger the linear relationship
Uses population
means and
Linear
population
correlation ρ = [ 1 / N ] * Σ { [ (Xi - μX) / σx ] * [ (Yi -
standard
(population μY) / σy ] }
deviations to
data)
compute a
population
Python I Notes Page 311
correlation
coefficient (ρ) from
population data
• Linear regression is most often used for any level of data exploration but, if
you are looking at the relationship between two variables, chances are you
will want to conjure a regression.
Simple Linear
Formulas Meanings
Regression
b0 is the intercept
Simple linear
ŷ = b0 + b1x constant in a sample
regression line
regression line
b1 refers to the
Regression regression coefficient
= b1 = Σ [ (xi - x) (yi - y) ] / Σ [ (xi - x)2]
coefficient in a sample regression
line (i.e. the slope)
Regression slope
= b0 = y - b1 * x
intercept
Python I Notes Page 312
Simple Linear
Formulas Meanings
Regression
Regression
= b1 = r * (sy / sx)
coefficient
• α , β – constant values
Confidence Intervals
Statisticians interpreting various results from a set of data need to know how sure
they are while dealing with the data. Within statistics, confidence level is used to
describe the amount of uncertainty associated with a simple estimate of a
population parameter. Simply put, confidence interval is a range within which
most plausible values would occur.
and
Where
n = Number of terms
x = Sample Mean
σ = Standard Deviation
In order to express a confidence interval, you need three separate data pieces:
• Confidence level
• Statistic
• Margin of error
Given these three pieces of information, you can calculate the range of the
confidence interval by the sample statistic + margin of error. The confidence
interval is specified by the uncertainty associated with the confidence level. In
most cases, the margin of error is not given but must be calculated separately.
Estimation
When dealing with confidence intervals, the margin of error is the range of values
above and below the sample statistic. The critical value is a factor used to
calculate the margin error.
Python I Notes Page 316
SciPy is a set of open source (BSD licensed) scientific and numerical tools, used by
scientists, analysts, and engineers in Python. SciPy has several different modules
used for optimization, integration, linear algebra, interpolation, FFT, signal and
image processing, ODE solvers, special functions, and others.
NumPy and SciPy are open-source add-on modules to Python. Between them they
provide common numerical and mathematical routines in pre-compiled, fast
functions. Given the fact they are open-source, they are continually maturing.
The NumPy (Numeric Python) package offers basic routines for manipulating large
arrays and matrices of numeric data. The SciPy (Scientific Python) package
extends the functionality of NumPy by adding a collection of useful algorithms,
like minimization, Fourier transformation, regression, and other applied
mathematical techniques.
Hypothesis testing is one of the most often used and abused practices in
statistics. Formally, the hypotheses are specified, aα-level is chosen, a test
statistic is calculated, and it is reported as to whether H0 or H1 is accepted.
Hypothesis testing is also called significance testing. It tests the claim about a
parameter using evidence data in a sample.
2. Test statistic
• Quantity based on sample data and null hypothesis used to test
between null and alternative hypotheses
Rejection region – Values of the test statistic for which we reject the null in favor
of the alternative hypothesis.
= Χ2 = Σ[ (Observed - Expected)2 /
Chi-square test statistic
Expected ]
Python I Notes Page 320
Terms
Two forms of
Hypothesis testing
statistical
Estimation
inference
You can change the α-level for a hypothesis test. This is called the level of
significance and changing it can affect the results of the test, whether or not you
reject or fail to reject H0.