1. Introduction#

Effective programming combines problem-solving skills, domain knowledge, and programming skills; these notebooks will target each of these different areas. The overall goal, though, is not necessarily to teach you Python but rather how you can solve problems and perform tasks with computer programs.

A program is a series of instructions to carry out a specific task. These tasks can run a large gamut of possibilities:

  • solving mathematical problems

  • processing images and text

  • playing video games

  • analyzing financial data

  • making decisions (which covers another range from answering simple yes/no questions to driving a car).

Learning to program is not a passive activity. You cannot simply just read documentation and these notebooks. Becoming an effective programmer takes targeted practice, and that practice takes time. These notebooks will present fundamental concepts, how Python implements those concepts, and then how you can solve real problems with those concepts. These notebooks contain a large amount of Python code. Not only should you run this code, but you should also make changes to the code and see what happens. Do not be afraid to make mistakes - try things! The computer does not care and will not make fun of you. You should complete the exercises at the end of each notebook. Yes, we provide many of the answers. However, you will learn more by attempting the exercises on your own. We have written these exercises to reinforce the concepts presented.

The following notebook presents an approach to solving problems with programming. The fundamental principle is understanding what occurs and how we can translate that into a series of steps. And most importantly, plan how to implement that approach before you write any code. Once we have those steps, we can then write code. As you perform this process, you should have a paper handy to take notes and record your thoughts.

To start our journey to learn computer programming through Python, this notebook contains three separate programs to demonstrate some of the capabilities of computer programs and some fundamental concepts within computer science. As these are some of the first Python programs you might have seen, we provide detailed explanations. We do not expect that you will now be able to write equivalent programs.

1.1. Sample Program: The Wayback Machine#

The following program allows users to enter a particular web address (URL) and date. The program will then query the “Wayback Machine” hosted at https://archive.org/ to find a copy of that web address closest to the entered date and then print that URL to the console.

 1import json
 2from urllib import request 
 3    
 4print("Let's find an old website.")
 5site = input("Type a website URL: ")
 6era = input("Type a year, month, and day, like 20140410: ")
 7url = "http://archive.org/wayback/available?url=%s&timestamp=%s" % (site, era)
 8response = request.urlopen(url)
 9contents = response.read()
10data = json.loads(contents)
11try:
12    old_site_url = data["archived_snapshots"]["closest"]["url"]
13    print("Found this copy: ", old_site_url)
14except:
15    print("Sorry, no luck accessing", site)
Let's find an old website.
Type a website URL: http://irishwildcat.com
Type a year, month, and day, like 20140410: 20140410
Found this copy:  http://web.archive.org/web/20140317220451/http://irishwildcat.com:80/

Source: Introducing Python: Modern Computing in Simple Packages, 2nd Ed, Bill Lubanovic O’Reilly Amazon

While you may be new to programming, hopefully you can see how the previous code block worked.

A few general notes:

  1. This code makes substantial use of existing modules and functions. json and urllib are modules - collection of code others have written that we can use in our code. One of the benefits of most programming languages is the libraries - both delivered as part of the programming platform (aka, “standard libraries”) as well as those that others have written. These libraries abstract many of the tasks to perform specific functionality and make doing those tasks much simpler than if we had to entirely write the system.

  2. Statements that look like name(value) are function calls. These function calls allow us to access code that has been previously written and performs a certain task.

  3. Statements that look like name.name(value) are also function calls, but these functions belong to modules.


Note: If you need to see line numbers, refer to the previous notebook for instructions to enable them.

  • Lines 1 let the program use the json library. JSON has become one of the standards for exchanging data through internet-based APIs. The json library provides code to parse the JSON data format.

  • Line 2 allows us to use the request module from the urllib package. A package is a just a group of related modules. As you can see from these first three lines, one of Python’s advantages is the large number of included standard libraries and available 3rd party open-source libraries.

  • Line 4 prints a message to the console telling the user what the program will do

  • Line 5 allows the user to type in the URL for a particular website. For this example, I used http://irishwildcat.com, an old blog no longer available on the Internet.

  • Line 6 gets a date in a particular format, starting with the year, month, and day. This representation is based upon an international standard - ISO-8601 - for dates and times.

  • Line 7 creates a variable called url to point to a location on Wayback Machine at https://archive.org/

  • Line 8 opens a connection to that url on archive.org and places the result into the variable response. It performs this by calling the urlopen() function within the request module.

  • Line 9 then reads the text output from that connection, setting the output into the variable contents

  • Line 10 converts the contents of the text variable (which contains a JSON object) to a Python dictionary. A dictionary stores data in key-value pairs - we discuss this in much greater detail in a later notebook.

  • In lines 11-15, we execute a code block in a special region. If a Python error occurs, the interpreter will detect the error and show the user the message in line 15.

  • Line 12 grabs a specific web address (URL) from the results of the Wayback Machine.

  • Line 13 prints that URL

Note: if you want to see the values of some of these intermediate values, you can edit the above source code and insert a line such as the following just after that variable is assigned (before line 10):

print(contents)

1.3. Sample Program: Traveling Salesperson and Solvability#

Fundamentally, computer science is concerned with asking if a particular problem can be solved and, if it is solvable, how expensive is a specific solution.

One of the most well-studied problems in computer science is the traveling salesperson problem. Suppose a salesperson has to visit \(n\) cities and return to that starting point. What is the shortest route that visits each city exactly once?

The brute-force method would be to try all possible permutations of the city routes. With three cities, the following possibilities exist:

  • City 1, City 2, City 3

  • City 1, City 3, City 2

  • City 2, City 1, City 3

  • City 2, City 3, City 1

  • City 3, City 1, City 2

  • City 3, City 2, City 1

How long would it take to search all the possibilities to find the optimal answer?

\(n!\) possible permutations exist.

Assuming we can search one billion possibilities in a second, how much time is required to solve the problem for 20 cities? 100 cities?

1import math
2
3num_sec_in_day = 24 * 60 * 60
4num_tries_per_second = 1_000_000_000
5num_cities = 20
6num_route_permutations = math.factorial(num_cities)
7num_days = num_route_permutations // num_tries_per_second // num_sec_in_day
8print(f"{num_days:,}")
28,158

Try running the above code block for different values for the number of cities. If you use a small value, the program prints zero as the code uses integral division // which discards the remainder (e.g., 5 // 2 has the result of 2, not 2.5). For small values, take out // num_sec_in_day to see the number of seconds or alter the assumption of one billion tries per second. How could you convert the result to the number of years? Try making these code changes in the above cell and re-running the cell. Part of being a computer scientist is to explore different possibilities.

You can also step through the code to see what occurs on each program step.

The traveling salesperson problem has direct applicability to real-life:

  • Paths robots need to take for manufacturing (e.g, welding a car, soldering a circuit board)

  • Delivery schedules for companies such as Amazon, FedEx, and UPS.

1.4. What is a Program?#

As mentioned at the start of this notebook, a program is a series of instructions to carry out a specific task. As you ran through these different examples, you may have noticed a few commonalities.

  1. Each of these programs had some sort of input. The first example asked for the input from the user. The other two had “hard-coded” inputs through the search terms and the number of cities.

  2. Each of the programs had some sort of output (result).

  3. All three had some form of sequence of commands with assignments, mathematical operations, or other statements.

  4. Each program used variables to hold information containing the current state of the program.

  5. Each program used existing libraries and functions to produce the desired functionality. One aspect of modern programming is not only learning the syntax (the rules) of a programming language, but learning how to find and use existing libraries.

Additionally, as these notebooks will demonstrate, programs typically contain some form of conditional expression that determines if different blocks of code should execute. Finally, the last fundamental commonality is that programs will regularly have some form of repetition where an action (or sequence of actions) will repeated.

1.5. Abstraction and Encapsulation#

One of the keys to successful programming is to apply fundamental programming principles. In this notebook, we have relied heavily upon two, abstraction and encapsulation, frequently appearing together. Abstraction seeks to distill a concept into its fundamental parts, while encapsulation bundles those parts and hides the implementation’s necessary and sometimes complex details. With abstraction, we focus on the outside view of an object or function, while encapsulation hides the exact details and steps to perform a specific task. For example, to query the Wayback Machine, many details were encapsulated by the urllib module. We did not have to concern ourselves with opening a network connection, following the HTTP protocol, and parsing the results. Instead, the urllib module handled those tasks with an abstracted (simplified) view to open a URL and read the response - the fundamental operations.

We take advantage of abstraction, encapsulation, and other programming principles when we use existing classes, modules, and functions. As a result, we can solve real-life problems by focusing on the essential parts without understanding the precise implementation details. Throughout these notebooks, we will apply programming principles to model real-life systems, problems, and tasks.

1.6. Python Documentation#

Python offers substantial documentation - both from within the interpreter with the help() function and online.

The homepage for the Python documentation is at https://docs.python.org/3/
Visit that site and see what is available.

1.7. Suggested LLM Prompts#

  • Explain computer programming to me as if I were a college student using finance topics.

  • Explain the difference between abstraction and encapsulation in Python programming. Provide code examples using finance topics.

  • Provide 5 examples of intractable problems in computer science. Explain how each relates to financial technology.

1.8. Review Questions#

At the end of many of these notebooks, we will have series of questions to reflect and review upon the material. Some questions are meant you to reflect and do not necessarily have correct answers, while others are meant to directly test your recollection and understanding of the material.

  1. How do problem-solving skills, domain knowledge, and programming skills combine together for effective programming? What does each bring?

  2. Besides your laptop and smartphone, where is software present in your daily life? Where is software not present?

  3. Identify the commonalities observed among the three programs in this notebook.

  4. Explain the importance of abstraction and encapsulation in programming, as discussed in the text. Provide examples to illustrate how these principles are applied in real-life programming scenarios.

  5. Which is a key aspect of programming?

    1. Accurate syntax
    2. Memorization of language features and library functions
    3. Understanding what needs to occur
    4. Well-trained minions
  6. What is the primary purpose of abstraction in programming?

    1. To bundle complex details within an object.
    2. To reveal the precise implementation details of a concept.
    3. To simplify a concept by distilling it to its fundamental parts.
    4. To focus solely on the internal workings of a function.
  7. What is the primary definition of a program?

    1. A collection of random instructions.
    2. A set of commands that can only be executed once.
    3. A series of instructions designed to automate tasks.
    4. A list of variables and their values.

Answers