Skip to main content

Introduction to Computer Programming with Python: Chapter 1. Introduction

Introduction to Computer Programming with Python
Chapter 1. Introduction
  • Show the following:

    Annotations
    Resources
  • Adjust appearance:

    Font
    Font style
    Color Scheme
    Light
    Dark
    Annotation contrast
    Low
    High
    Margins
  • Search within:
    • Notifications
    • Privacy
  • Project HomeIntroduction to Computer Programming with Python
  • Learn more about Manifold

Notes

table of contents
  1. Cover
  2. Title Page
  3. Copyright Page
  4. Contents
  5. Chapter 1 Introduction
    1. Learning Objectives
    2. 1.1 A Brief History of Computers
    3. 1.2 Fundamentals of Computing and Modern Computers
      1. Number Systems and the Foundation of Computing
      2. Computability and Computational Complexity
      3. The Construction of Modern Computers
        1. Analog Computers
        2. Digital Computers
        3. Mechanic-Based Components
        4. Vacuum Tube–Based Components
        5. Transistors
        6. Integrated Circuits and Very Large-Scale Integrated Circuits
    4. 1.3 Programming and Programming Languages
    5. 1.4 Python Programming Language
      1. The Development and Implementation of Python
      2. Advantages of Python
      3. Resources for Python and Python Education
    6. 1.5 Getting Ready to Learn Programming in Python
      1. Installing and Setting Up the Python Programming Environment
        1. Installing Python
        2. Setting Up a Virtual Environment for a Python Project
        3. Installing Jupyter Notebook
        4. Installing Visual Studio Code
      2. Additional Tools Supporting Software Development in Python
        1. Buildbot
        2. Trac
        3. Roundup
    7. 1.6 Getting a Taste of Programming with Python
      1. Program Interactively with Python Interactive Shell
      2. Program with VS Code IDE
      3. Use Jupyter Notebook Within VS Code to Program Interactively
      4. Write Documentation in Markdown
        1. Headings
        2. Paragraphs
        3. New Lines
        4. Italic, Bold, and Strikethrough Texts
        5. Horizontal Rules
        6. Keyboard Keys
        7. Unordered Lists
        8. Ordered Lists
        9. Definition Lists
        10. Links
        11. Links to Internal Sections
        12. Images
        13. Blockquotes
        14. Tables
        15. Inline Program / Script Code
        16. Code Block
        17. Mathematical Formulas and Expressions
        18. To-Do List
        19. Escape Sequence for Special Characters
      5. Programming Interactively with Jupyter Notebook Within VS Code
      6. Run Python Programs Outside IDE
      7. Make the Python Program File Executable
      8. Errors in Programs
    8. 1.7 Essentials of Problem Solving and Software Development
      1. Design Algorithms to Solve Problems
      2. Phases of Software System Development
        1. Phase 1. Understand the Project
        2. Phase 2. Analyze the Requirements to Identify Computer-Solvable Problems and Tasks
        3. Phase 3. Design the System
        4. Phase 4. Implement the System
        5. Phase 5. Test the System
        6. Phase 6. Maintain the System
    9. 1.8 Manage Your Working Files for Software Development Projects
      1. Set Up Git on Your Computer and Version-Control Locally
      2. Set Up an Account on GitHub and Version-Control with Remote Repositories
    10. Chapter Summary
    11. Exercises
    12. Projects
  6. Chapter 2 Essential Building Blocks of Computer Programs
    1. Learning Objectives
    2. 2.1 Primary Constructs of Computer Programs in Python
      1. Vocabulary of the Programming Language
        1. Rules of Naming Identifiers
        2. Python Naming Conventions
        3. Names with Leading and/or Trailing Underscores
        4. Rules of Scope Resolution for Identifiers
      2. Simple Data Types
        1. Signed Integers (int)
        2. Float (float)
        3. Boolean (bool)
        4. Complex (complex)
      3. Compound Data Types
        1. String (str)
        2. List
        3. Tuple
        4. Set
        5. Dictionary
        6. Object
      4. Variables and Constants
        1. Variables
        2. Built-In Constants
      5. Operators
        1. Arithmetic Operators
        2. Comparison Operators
        3. Logical Operators
        4. Bitwise Operators
        5. Assignment Operators
        6. Identity Operators
        7. Sequence Operators
        8. Membership Operator
      6. Built-In Functions
      7. Expressions
    3. 2.2 Higher-Level Constructs of Python Programs
      1. Structure of Python Programs
      2. Documentation and Comments
      3. Simple Statements
        1. Expression Statement
        2. Assignment Statement
        3. print Statement
        4. input Statement
        5. assert Statement
        6. pass Statement
        7. del Statement
        8. return Statement
        9. open Statement
        10. yield Statement
        11. raise Statement
        12. break Statement
        13. continue Statement
        14. import Statement
        15. global Statement
        16. nonlocal Statement
        17. help Statement
      4. Compound Statements
        1. Code Blocks
        2. Rules of Indentation
        3. Rules of Spacing
        4. if Statement
        5. if-else Statement
        6. if-elif Statement
        7. if-elif-else Statement
        8. while Statement
        9. for Statement
        10. def Statement
        11. class Statement
        12. try-except Statement
        13. with Statement
    4. Chapter Summary
    5. Exercises
    6. Projects
  7. Chapter 3 Flow Control of Statements
    1. Learning Objectives
    2. 3.1 Selective with the if Statement
    3. 3.2 Single-branch selective with if Statement
    4. 3.3 Multiple-Branch Selective with if-elif-… and if-elif-…-else Statements
    5. 3.4 Iterate with for Statement
      1. Using break and continue Statements and an else Clause Within Loops
      2. Common Coding Mistakes with the for Loop
    6. 3.5 Iterate with the while Statement
      1. Common Coding Mistakes with a while Loop
    7. 3.6 Iterate with for Versus while
    8. Chapter Summary
    9. Exercises
    10. Projects
  8. Chapter 4 Handle Errors and Exceptions in Programs
    1. Learning Objectives
    2. 4.1 Errors in Your Programs
      1. Exception
      2. ArithmeticError
      3. OverflowError
      4. ZeroDivisionError
      5. FloatingPointError
      6. AssertionError
      7. AttributeError
      8. BufferError
      9. EOFError
      10. GeneratorExit
      11. ImportError
      12. IndexError
      13. KeyError
      14. KeyboardInterrupt
      15. MemoryError
      16. ModuleNotFoundError
      17. NameError
      18. NotImplementedError
      19. OSError
      20. BlockingIOError
      21. ChildProcessError
      22. ConnectionError
      23. BrokenPipeError
      24. ConnectionAbortedError
      25. ConnectionRefusedError
      26. ConnectionResetError
      27. FileExistsError
      28. FileNotFoundError
      29. IsADirectoryError
      30. NotADirectoryError
      31. PermissionError
      32. ProcessLookupError
      33. TimeoutError
      34. RecursionError
      35. ReferenceError
      36. RuntimeError
      37. StopIteration
      38. StopAsyncIteration
      39. SyntaxError
      40. IndentationError
      41. TabError
      42. SystemError
      43. SystemExit
      44. TypeError
      45. UnboundLocalError
      46. UnicodeError
      47. UnicodeEncodeError
      48. UnicodeDecodeError
      49. UnicodeTranslateError
      50. ValueError
    3. 4.2 Handling Runtime Errors and Exceptions
    4. Chapter Summary
    5. Exercises
  9. Chapter 5 Use Sequences, Sets, Dictionaries, and Text Files
    1. Learning Objectives
    2. 5.1 Strings
      1. Methods of Built-In Class str
      2. Built-In Functions and Operators for Strings
      3. Constructing and Formatting Strings
      4. Regular Expressions
    3. 5.2 Lists
    4. 5.3 Tuples
    5. 5.4 Sets
    6. 5.5 Dictionaries
    7. 5.6 List, Set, and Dictionary Comprehension
      1. List Comprehension
      2. Set Comprehension
      3. Dictionary Comprehension
    8. 5.7 Text Files
      1. Opening and Closing a File
      2. Write or Append to a File
      3. Reading from a File
      4. Update Existing Content of a Text File
      5. Deleting Portion of a Text File
    9. Chapter Summary
    10. Exercises
    11. Projects
  10. Chapter 6 Define and Use Functions
    1. Learning Objectives
    2. 6.1 Defining and Using Functions in Python
    3. 6.2 Parameters and Arguments in Functions
    4. 6.3 Recursive Functions
    5. 6.4 Anonymous Functions: lambda Expressions
    6. 6.5 Special Functions: Mapping, Filtering, and Reducing
      1. Mapping
      2. Filtering
      3. Reducing
    7. 6.6 Generators: Turning a Function into a Generator of Iterables
    8. 6.7 Closures: Turning a Function into a Closure
    9. 6.8 Decorators: Using Function as a Decorator in Python
    10. 6.9 Properties of Functions
    11. Chapter Summary
    12. Exercises
    13. Projects
  11. Chapter 7 Object-Oriented Programming with Python
    1. Learning Objectives
    2. 7.1 Introduction to Object-Oriented Programming (OOP)
      1. Abstraction
      2. Information Hiding or Data Encapsulation
      3. Inheritance
    3. 7.2 Defining and Using Classes in Python
      1. Inheritance: Subclass and Superclass
      2. Public, Private, and Protected Members of a Class
      3. Class Methods
      4. Static Methods
      5. Class Attributes
    4. 7.3 Advanced Topics in OOP with Python
      1. Dunder Methods in Class Definition
      2. Using Class as Decorator
      3. Built-In Property() Function and Property Decorator
      4. Creating a New Class Dynamically and Modify a Defined Class or Instance
      5. Keeping Objects in Permanent Storage
    5. Chapter Summary
    6. Exercises
    7. Project
  12. Chapter 8 Modules and Packages
    1. Learning Objectives
    2. 8.1 Creating Modules and Packages
    3. 8.2 Using Modules and Packages
    4. 8.3 Install and Learn About Modules Developed by Others
    5. 8.4 Module for Generating Random Numbers
      1. Functions for Bookkeeping
      2. Functions for Generating Random Integers
      3. Functions for Randomly Generating Float Numbers
      4. Functions for Randomly Selected Item(s) from Sequences
    6. 8.5 Module for Mathematical Operations
    7. 8.6 Modules for Time, Date, and Calendar
      1. The Datetime Module
      2. The Time Module
      3. The Calendar Module
    8. 8.7 Modules for Data Representation and Exchange
    9. 8.8 Modules for Interfacing Operating Systems and Python Interpreter
      1. OS Module for Interacting with the Operating System
      2. The path Submodule from os for Manipulating File Paths
      3. The sys Module for Interaction Between the Python and Python Interpreter or Python Virtual Machine (PVM)
    10. 8.9 Module for Logging Events During Program Runtime
    11. 8.10 Modules for Playing and Manipulating Audio and Video Files
      1. winsound
      2. PyGame
    12. 8.11 Modules for Creating and Manipulating Graphics and Images
      1. Create Graphics with Tkinter
      2. Manipulate Images with Pillow
    13. 8.12 Modules for Data Analytics
    14. Chapter Summary
    15. Exercises
    16. Projects
  13. Chapter 9 Develop GUI-Based Applications
    1. Learning Objectives
    2. 9.1 Terminal-Based Applications Versus GUI-Based Applications
    3. 9.2 Designing and Developing GUI-Based Applications in Python
      1. Tkinter Module
      2. tkinter.ttk—Tk-Themed Widgets
    4. Chapter Summary
    5. Exercises
    6. Projects

Chapter 1 Introduction

This chapter prepares you to learn how to program with Python. Preparation includes a brief introduction to computers and computing, programming, and programming languages, as well as the installation of Python and Python interactive programming and integrated development environments (IDEs), including Jupyter Notebook for interactive programming and VS Code as a required IDE.

Learning Objectives

After completing this chapter, you should be able to

  • • talk about the history of computers.
  • • describe the basic components of modern computers and their roles.
  • • explain the basic principles of modern computers.
  • • discuss the basics of computability and computational complexity.
  • • explain how the construction of modern computers has evolved.
  • • explain what computer systems are made of.
  • • discuss computer programming languages.
  • • describe Python and discuss its development, features, and advantages.
  • • install Python and required Python IDEs on the computer.
  • • get Python and Python IDEs running for the learning activities included in this textbook.

1.1 A Brief History of Computers

The history of human tools, devices, or instruments to help us count, compute, and think can be traced back to the Stone Age, when our ancestors used knots on ropes, marks on bark, stones, and balls of clay. One of the most well-known and widely used devices for computing in human history is the abacus, shown in Figure 1-1.

A wooden abacus with an equal number of counters along several rods.

Figure 1-1: A traditional abacus

The exact origin of the abacus is unknown, but before the adoption of the written Hindu-Arabic numeral system, the abacus had already been widely used in many countries, including China, Russia, and some European countries. Abacuses continued to be widely used by almost all accountants in China before the adoption of modern computers and calculators.

Today, although abacuses are rarely used in real applications such as accounting, their working principles are still used to train people to do mental math, such as in programs like the Universal Concept of Mental Arithmetic System (UCMAS; see ucmas.ca). UCMAS holds annual competitions worldwide, including in Canada.

The invention and development of today’s modern computers can be attributed to the invention and development of all previous relevant concepts, principles, and technologies. The concept of a digital, programmable computer originated with Charles Babbage, an English mathematician, philosopher, inventor, and machine engineer. In 1822, he designed a steam-driven calculating machine for automatically computing tables of numbers. Although his government-funded project failed, the many ideas he developed and used in his analytical engine were adopted and used in the world’s first modern programmable computer, built a century later. That is why Charles Babbage is considered to be one of the “fathers of computers.”

In 1847, George Boole, an English mathematician introduced Boolean logic, propositional calculus, and Boolean algebra as the laws of thinking, which later became the foundation of modern electronic computers and digital devices in general. For that reason, George Boole is regarded as a founder of computer science.

In 1890, Herman Hollerith successfully designed and built a punch-card-based system for the US government to calculate its 1890 census. It saved the US government US$5 million by finishing the calculation in one year instead of 10 years it would have taken using traditional methods, as in the previous census. In 1896, Herman Hollerith established a company called the Tabulating Machine Company to make the machines. This company ultimately became IBM.

The central concept and theory of modern computers were conceived in 1936 by English mathematician Alan Turing in his “universal machine,” which became known as the Turing machine in his honour. He successfully proved that his universal machine could calculate anything that is computable. Alan Turing is also considered by some to be “a father of computers”—or more precisely, “a father of computing”—for his seminal paper on the theory of computation. In the eyes of the general public, Alan Turing is more famous for his role in cracking the so-called unbreakable codes used by the German army during World War II, as presented in The Imitation Game, a well-known Hollywood movie.

Until 1937, all computing machines or computers were mechanically based, using gears, cams, belts, or shafts. John Vincent Atanasoff, an American mathematician and physician at Iowa State University, attempted in 1937 to build the first electronic computer. He and Clifford Berry, one of his graduate students at the time, designed and built a special-purpose digital computer, ABC. For that reason, he is considered to be “the father of the modern computer.” It was in the ABC machine that binary math and Boolean logic were successfully used for the first time.

In 1943, across the Atlantic Ocean in England, Colossus Mark I, a prototype of a special-purpose computer, was built. A year later, in 1944, Colossus Mark II was built and used to break the encrypted radiotelegraphy messages transmitted by the German army at the end of World War II.

In 1946, the first general-purpose digital computer, the Electronic Numerical Integrator and Computer (ENIAC), was built by two professors at the University of Pennsylvania: John Mauchly and J. Presper Eckert. The computer had over 18,000 vacuum tubes and weighed 30 tons. A rumour at the time said that whenever the computer was turned on, the lights in some districts of Philadelphia would dim.

During the same time, the same group of people at the University of Pennsylvania also designed and built EDVAC (the Electronic Discrete Variable Automatic Computer, completed in 1949), BINAC (the Binary Automatic Computer, also completed in 1949), and UNIVAC I (the Universal Automatic Computer I, completed in 1950), which were the first commercial computers made in the US. Although the computing power of these computers was not even comparable to that of today’s smartphone, their contributions to the development of today’s modern computers are enormous. These contributions include the concepts, principles, and technology of stored programs, subroutines, and programming languages.

Around the same time that ENIAC was built, the Harvard Mark I automatic sequence-controlled calculator was also built at Harvard University. It is believed that John von Neumann, a mathematician and physicist working at the Los Alamos National Laboratory, was the first user of the Harvard Mark I, which he used to run programs on the machine to calculate for the Manhattan Project.

In 1944, Von Neumann had an opportunity to join a discussion with J. Presper Eckert and John Mauchly, who were developing ENIAC and EDVAC machines at the time. He wrote up a report on the EDVAC, First Draft of a Report, in which he described a computer architecture later known as Von Neumann architecture (see Figure 1-2). The key idea behind the architecture is a stored-program computer that includes the following components:

  • • a processing unit that contains an arithmetic logic unit and processor registers
  • • a control unit that contains an instruction register and program counter
  • • internal memory that stores data and instructions
  • • external mass storage
  • • input and output mechanisms

The unpublished report was widely circulated. It had great impact on the later development of modern computers, although to many people, Turing was the originator of this key idea and architecture, and many others, including J. Presper Eckert and John Mauchly, made significant contributions to its evolution as well. Regardless, it is Von Neumann architecture that has guided the design of modern computers. All computers we use today are essentially still Von Neumann computers.

Modern computers can be categorized into three generations based on the core hardware technology used. The first generation of computers (1937–46) used vacuum tubes, the second generation (1947–62) used transistors, and the third generation (1963–present) used integrated circuits (IC).

A diagram of the central processing unit and all its components.

Figure 1-2: Von Neumann architecture

Both hardware and software play important roles in computers. On the hardware front, in the first generation of modern computers, magnetic tapes and disks were developed and used for storage, and printers appeared and were used for output. On the software side, simple operating systems (OSs) were developed and used, and over 100 high-level programming languages were developed.

During the second generation of computers, the Universal Automatic Computer (UNIVAC I), the first computer for commercial use, was introduced to the public (1951). The International Business Machines Corporation (IBM) also brought their IBM650 and IBM700 series computers to the computer world (1953).

Since 1963, the beginning of the third generation of modern computers, advances in hardware have made computers smaller and smaller but much more powerful, with a higher speed of CPU and a bigger memory with faster throughput.

In 1980, Microsoft developed MS-DOS, short for Microsoft Disk Operating System, and in 1981, IBM introduced the first personal computer (PC) for home and office use. In 1984, Apple brought its Macintosh computer with its icon-driven interface to the world, and in the 1990s, Microsoft brought the world the Windows operating system. Billions of computer users around the world have enjoyed both Microsoft Windows and Apple’s macOS.

More information about the history of computers can be found on the internet by searching for “history of computers,” including the following articles:

  • https://en.wikipedia.org/wiki/Computer
  • https://www.computerhistory.org/timeline/computers/
  • https://www.explainthatstuff.com/historyofcomputers.html
  • https://www.britannica.com/technology/computer/History-of-computing

1.2 Fundamentals of Computing and Modern Computers

The development of modern computers is the result of the collective efforts of many great mathematicians, scientists, and engineers and advances in both theories and technologies. This section looks at the theoretical and technical fundamentals of computing and modern computers in order to show how modern computers work internally.

Number Systems and the Foundation of Computing

Initially, computers and computing devices were developed to deal with numbers. They later made their way into the realm of text handling and information processing. This is done through encoding. When properly coded, all text can be represented as numbers—codes—and all ways of manipulating text can be accomplished through operations on those codes. Number systems and operations on numbers are really a basic foundation of computing and modern computers.

We all know numbers, and the number system we have known since childhood is base-10, which is represented using 10 digits from 0 to 9. A base-10 number such as 2539867 can be written as

2 * 106 + 5 * 105 + 3 * 104 + 9 * 103 + 8 * 102 + 6 * 101 + 7 * 100

In general, an n-digit number dn−1dn−2…d1d0 (where n is any positive integer and each d ∈ [0, 1, 2, …, 9], where 0 < i < n ) in a base-10 number system can be rewritten as

dn−1 * 10n−1 + dn−2 * 10n−2 + … + d1 * 101 + d0 * 100

In fact, a number system can be based on any whole number other than 0 and 1. There is a base-7 system for weeks, a base-12 system for the Chinese zodiac and imperial foot and inch, and a base-24 number system for the 24-hour clock. A long list of different numbering systems that have appeared since prehistory can be found at https://en.wikipedia.org/wiki/List_of_numeral_systems.

In general, a base-Q number system will require Q unique symbols representing 0, 1, … Q−1, respectively. The base-10 equivalence of an n-digit base-Q number dn−1dn−2…d1d0 can be represented as

dn−1 * Qn−1 + dn−2 * Q−2 + … + d1 * Q1 + d0 * Q0

The evaluation of the expression above will result in its base-10 equivalence. This is how we convert a number from base-Q to base-10.

For Q = 2, the numbers 0 and 1 are used to represent digits; for Q = 3, the numbers 0, 1, and 2 are used; for Q = 8, the numbers 0, 1, 2, 3, 4, 5, 6, and 7 are used; for Q = 16, the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a/A, b/B, c/C, d/D, e/E, and f/F are used.

The expression above also shows how to convert a number from base-10 to base-Q: divide the number by Q repeatedly, and the remainders will be the digits. The details of these conversions can be found on the internet.

For a number dn−1dn−2…d1 d0 in a base-Q number system, it is often necessary to add Q as a subscription of the sequence, as shown below:

dn−1dn−2…d1d0Q or (dn−1dn−2…d1d0)Q

This explicitly indicates that the number is in a base-Q number system. For example, number 657 in a base-8 number system will be written as 6578 or (657)8, especially if the context does not indicate which number system the number is in.

All number systems have the same operations that we are already familiar with in a base-10 system, such as addition, subtraction, multiplication, and division. The only difference is that in a base-Q number system, a 1 carried to or borrowed from a higher digit is equal to Q instead of 10.

For example, in a base-2 number system, 101 can be written as 1 * 22 + 0 * 21 + 1 * 20, and its 10-base equivalence is 1 * 4 + 0 + 1 = 5, and for 101 + 110, the operation is

101

+ 110

______

1011

110

− 101

______

001

Given the four basic mathematical operations—addition, subtraction, multiplication, and division—it is very simple to prove that multiplication can be done by repeated addition, whereas division can be done by repeated subtraction. Therefore, if you know how to do addition and subtraction and are able to count, you will be able to do multiplication and division as well.

More importantly, because a − b can be rewritten as a + (−b), you may be able to eliminate subtraction if you can represent −b without the minus sign. In a base-Q number system, (a − b) can be conveniently handled through a + b′, in which b′ is the Q-complement of b representing (−b).

Given a negative number, −N, in which N is called the magnitude of −N, how do you get N′, the Q’s complement to N? It turns out to be very easy.

For a number N written as dndn−1…d0 in a base-Q number system, its Q-complement N′ is a number, also in a base-Q number system, such that N + N′ = Qn, and N′ can be easily calculated in the following two steps:

  1. 1. For each di in dn−1dn−2…d0, find ti such that di + ti = Q−1, to get tn−1tn−2…t0
  2. 2. Add 1 to tn−1tn−2…t0 to get N’s complement cn−1cn−2…c0

For b in a − b, if b has fewer digits than a, add 0s to the left of b to make up the missing digits before working on the above steps. For example, for 768 − 31, do 768 + (−31) or 768 + the 10’s complement of 31.

To get 10’s complement of 31, first add one 0 to the front of 3 to get 031, then find out the 10’s complement of 031, which is 968 + 1 = 969. Then do 768 + 969 to get 1737, and ignore the last carried digit, 1, to get 737, which is the result of 768 − 31.

It is easy to see that if C is N’s complement, then N is C’s complement as well. Using the notation given above, this can be written as (N′)′ = N.

What about floating-point numbers—that is, numbers with fractions, such as 3.1415926?

In a base-Q number system, a floating-point number can be represented much like integers, as shown below:

dn−1 * Qn−1 + dn−2 * Qn−2 + … + d1Q1 + d−1 * Q−1 + d−2 * Q−2 + … + d−mQ−m

This may give us a hint that a floating-point number can be represented by two sequences of digits: one for the integer portion and the other for the fraction portion. If floating-point numbers are represented this way in computers, addition and subtraction could be performed on the two portions separately, but with a mechanism to deal with carrying from the fraction to the integer in the case of addition, and a mechanism to deal with borrowing from the integer to the fraction in the case of subtraction. This would be rather clumsy, and it would be even more clumsy when doing multiplication and division.

In reality, floating-point numbers are represented in scientific notation, such as 1.3 * 102 in a base-10 or decimal system, where 1.3 is called the fraction, 2 is called the exponent, and 10 is the radix. In a base-2 system, the radix would be 2.

In scientific notation, arithmetic operations on floating-point numbers can be easily done, especially if the fractions of all floating-point numbers are normalized—that is, if the number of digits before the radix point is fixed for all floating-point numbers. For example, to perform the addition or subtraction of two floating-point numbers, all you need to do is shift the digits of one floating-point number based on the difference of the two exponents, then perform addition or subtraction in the same way as on integers. More surprisingly, multiplying and dividing floating-point numbers in scientific notation is much easier than adding and subtracting. There is no need to shift the digits because

a * rm * b * rn = a * b * rm+n

and

a * rm / b * rn = a / b * rm−n

All you need to build a computer capable of adding, subtracting, multiplying, and dividing in a base-Q number system is the ability to add and count, which is needed for multiplication and division. Moreover, counting itself can be done by addition when counting up or subtraction when counting down. This is very encouraging.

The following discoveries are even more encouraging:

  • • Many mathematical and scientific problems can be represented and reformulated based on the basic mathematical operations discussed above.
  • • If we can build a computing device capable of doing basic mathematical operations, the machine can solve many computing and information processing problems.

The remaining question is how to make such a machine work faster. On a computing machine with only an addition and counting unit, if each step of an operation has to be enabled by a human, the computation would be very slow because it would have to wait for the human to input instructions and data. To speed things up, the entire problem solving process needs to be automated. This requirement has led to the introduction of memory to computing machines to store both the operating instructions (the program) and data needed during the computation. For the central processing unit (CPU) and memory (RAM) to communicate and work together, a communication channel and control unit need to be added. For human users to communicate with the computing machine, input and output units are also needed. Together these include everything described in Von Neumann’s architecture for modern computers.

Computability and Computational Complexity

Now comes the question of how powerful the computing machine described above can be. In particular, we should ask,

  • • What problems can or cannot be computed with a computing machine?
  • • How difficult is it to solve a given problem with a computing machine, assuming that the machine can take as many steps and use as much memory as needed to solve the problem?

Answering the first problem is the study of computability. Answering the second question is the study of computational complexity.

A well-known computability problem was raised by David Hilbert, a very famous German mathematician in the 19th century. The 10th problem in his list of 23 important mathematical problems he published in 1900 is the decidability problem, which is about finding an algorithm that can determine whether a Diophantine equation such as xn + yn = zn, where n is an integer and greater than 2, has integer solutions. It was proved in 1970 that such an algorithm doesn’t exist, meaning that the problem of deciding whether a Diophantine equation has integer solutions is unsolvable, or incomputable.

Between the 1930s and 1940s, before the existence of modern computers, Alan Turing invented a simple yet powerful abstract machine called the Turing machine. He proved that the abstract machine could simulate the logic of any given algorithm and further determine whether an algorithm is computable. The operation of the machine will halt if the algorithm is incomputable. During the same time, computability was also well studied by Alonzo Church based on lambda calculus. The two works later intertwined in a formal theory of computation known as the Church-Turing thesis.

In computer science, the computational complexity of an algorithm devised to solve a problem is about the number of resources—CPU time and memory, in particular—needed to run the algorithm on a computer. A problem may be solved in different ways using different algorithms with different computational complexities. Sometimes it is necessary to find a better algorithm that runs faster and/or uses less memory.

The time required to run an algorithm is also related to the size of the problem to be solved. For example, finding someone in a classroom would be much easier than finding someone in a city. If you denote the size of a problem with n, then in computer science, O(f(n))—called the big O of f(n)—is used to describe asymptotically the upper bound of running time: the time complexity of an algorithm for solving the problem.

For example, assume you want to sort the numbers in a list. The size of the problem will be the number of numbers in the list. One algorithm is called selection sort, which begins with selecting the smallest number from the list, then selecting the smallest number from the remaining of the list, and so on. Assume the size of the list is n. The first time it will need to do n − 1 comparisons. The second time it will need n − 2 comparisons. When these are only two numbers left in the list, only one comparison is needed to finish. So the complexity of the algorithm in terms of the total number of comparisons needed will be

(n − 1) + (n − 2) + (n − 3) … + 2 + 1 = (n − 1 + 1) / 2 * (n − 1) = n * (n − 1) / 2 = (n2 − n) / 2 = O(n2)

You may have noted in the big-O notation that we have kept only the highest-order term and have removed the constant ½ as well. This is because, with the big-O asymptotic notation, only the scale of complexity increased when the size of the problem increases is of interest.

In computer science, problems can be classified as P (for polynomial), NP (for nondeterministic polynomial), NP-complete, or NP-hard problems. A problem is said to be in P if it can be solved using a deterministic algorithm in polynomial time—that is, if the complexity is a big O of a polynomial. A problem is said to be in NP if it can be solved with a nondeterministic algorithm in polynomial time. A problem is NP-complete if a possible solution can be quickly verified but there is no known algorithm to find a solution in polynomial time. A problem is NP-hard if every NP problem can be transformed or reduced to it within polynomial time.

In the above, a deterministic algorithm refers to the one in which, given the same input, the output would always be the same, whereas a nondeterministic algorithm may give different outputs even for the very same input.

The complexity of algorithms, which is closely related to the response and processing speed of programs and computer systems, is the concern of good programmers, especially when dealing with resource-intensive applications. It is also an important topic to be studied in senior or graduate courses in computing and computer science. When writing a program or just a function for assignments or an application, always think about the time and space complexity and ask yourself, Is there a better way to get the job done?

The Construction of Modern Computers

In general, computers can be categorized into two types: analog computers and digital computers. The computers in use today are almost all digital computers. But analog computers do have their advantages.

Analog Computers

As a computing machine, an analog computer uses the properties of certain objects or phenomena to directly analogize the values of variables (such as the speed of a vehicle) of a problem to be solved. Examples of the properties include the voltage and amperage of electricity, the number of teeth of a gear, or even the length of a wood or metal stick.

Analog computing machines were often built for some special purpose, with a very wide range of difficulties and complexities. A simple analog computer could be built with three gears for addition and subtraction. For addition, using both operands to drive the third gear in the same direction will yield the sum of the two operands on the third gear, whereas for subtraction, the difference can be calculated by driving the two gears in two different directions.

Historically, even as early as 100 BC, complicated analog computing machines were built for various applications, from astronomy in ancient Greece to the differential machine for solving differential equations in the late 1800s and early 1900s. Some of the most complicated analog computing machines in modern history include those for flight simulation and gunfire control. Analog computing machines continued well into the early age of modern digital computers because the special applications analog computers were developed for were still too hard for digital computers in the 1960s or even 1970s.

Digital Computers

Different from analog computers, digital computers use sequences of digits to represent the values of variables involved in the problems to be solved. In digital computers, the machine representation of a problem is often abstract, with no analogy to the original problem.

Theoretically, in accordance with the earlier discussion about general number systems, digital computers’ digits can be in any base, from 2 up. Hence a digital computer could be base-2, base-3, … base-10, base-16, and so on. The digital computers we are using today are all base-2, or binary, computers, although it has been proved that base-3 computers would be even more efficient than base-2 computers.

This is because it is more convenient and cheaper to build components with the two easily distinguishable states needed to represent base-2 numbers.

Also, in a binary system, signed numbers can be naturally represented using the highest sign bit: 0 for positive numbers and 1 for negative numbers, for example. Moreover, the addition and subtraction of signed binary numbers can be easily done by using 2’s complements to represent negative numbers.

For example, to do −2−3, we would do

(−2) + (−3)

or

(−00000010)b + (−00000011)b

Next, we need to convert (−00000010)b and (−00000011)b into their 2’s complement representations. According to the two steps we described in the section above, “Number Systems and the Foundation of Computing,” with Q = 2, you first get 1’s complement of the magnitude of each negative number above by flipping each bit, then adding 1 to 1’s complement, as shown below:

(00000010)b (1’s complement by flipping each bit) → (11111101)b (then + 1) → (11111110)b

(00000011)b (1’s complement by flipping each bit) → (11111100)b (then + 1) → (11111101)b

The addition above will become

(11111110)b + (11111101)b

which is equal to

(11111011)b

The 1 on the highest bit of the result means that the result is 2’s complement representation of a negative number. Interestingly, the magnitude of the negative number is the complement of (11111011)b, which is

(11111011)b (1’s complement by flipping each bit) → (00000100)b (then + 1) → (000000101)b,

which is 5, meaning that the result of −2−3 is −5.

It is very easy to calculate the 2’s complement of a binary number. Adding a negative number can be done by adding its 2’s complement. That is the advantage that the binary system has offered for the construction and development of modern digital computers.

As such, in principle, the key component needed in our modern digital computers for computing and information processing is a unit to add two binary numbers, since the three other basic arithmetic operations can be done based on addition, using respective algorithms or routines such as the ones mentioned above. In the real implementation and construction of digital computers, all basic arithmetic operations, including bitwise operations on integer binary numbers such as the operations needed to find 2’s complements, are hardwired into a unit called the arithmetic logic unit (ALU), which is the core of the central processing unit (CPU) that you have heard about often.

A digital computer also needs to compute real or floating-point numbers as well as process graphics. To get these two jobs done more efficiently, modern computers also have a floating-point unit (FPU) for floating-point numbers, and a graphics processing unit (GPU) for graphics.

Mechanic-Based Components

Mechanical components such as levels, gears, and wheels can be used to build analog computers. In fact, digital computers can also be built with mechanical components. The earliest special-purpose mechanic-based digital computer was the difference engine designed by Charles Babbage. A general-purpose mechanic-based digital computer, called the analytical engine, was also first proposed by Charles Babbage. In the design, the machine has memory and CPU. It would have been programmable and, therefore, would have become a general-purpose digital computer, if ever actually built.

Vacuum Tube–Based Components

Invented in 1904 by John Ambrose Fleming of England, a vacuum tube is a tube made of glass with gas/air removed, with at least two electrodes inserted for allowing or controlling the flow of electrons. Vacuum tubes can be made to be switches or amplifiers. As switches, the two states of on and off can be used to represent the 0 and 1 of binary arithmetic or Boolean logic. When the state of output can be controlled by input or inputs, the switch is made to be a gate to perform different logical operations. That’s how vacuum tubes could be used to implement modern computers; as amplifiers, they found their uses in many electronic devices and equipment such as radio, radar, television, and telephone systems. It was the vacuum tube that made electronic computers and other electronic devices possible for the first time. Vacuum tubes played very important roles in the development and construction of all electronic devices and systems in the first half of the 20th century.

However, vacuum tubes disappeared from computers and most other electronic devices soon after transistors were developed at the Bell Laboratories by John Bardeen, Walter Brattain, and William Shockley in 1947 because, compared to transistors, vacuum tubes were too big, too heavy, too unreliable and consumed too much power to operate. That limited the construction and uses of vacuum tube–based computers and devices.

Transistors

Recall that in the construction of computers, the ALU or CPU requires two-state switches to represent 0 and 1 and logical gates to perform additions and subtractions of binary number systems. Vacuum tubes could be made to construct electronic computers, but the computers became too bulky and too heavy to be more useful.

Integrated Circuits and Very Large-Scale Integrated Circuits

Integrated circuits (ICs) are chips with many electronic circuits built in, while very large-scale integrated (VLSI) circuits are those with millions and even billions of electronic circuits built into very small semiconductor chips. According to Moore’s law (attributed to Gordon Moore, the cofounder and former CEO of Intel), the number of transistors in a dense integrated circuit doubles about every two years. This has been the case over the last few decades since 1975, though advancement in VLSI technology has slowed since 2010.

Today, computers are all using VLSI chips. That is why computers have become smaller and smaller, while computing power has increased exponentially in accordance with Moore’s law.

1.3 Programming and Programming Languages

One of the key principles of modern computers is using stored programs, which are sequences of instructions telling computers what to do. The task of writing programs for computers is called programming.

During the first generation of computers, machine languages and assembly languages were used to write programs. Machine languages use sequences of 0 and 1 to code instructions for computers. Programs in a machine language can be directly understood by the CPU of the target computer. The following is an example of machine language for the Intel 8088 CPU:

  • 00000011 00000100
  • 10001000 11000011

As you can see, although machine language is friendly to computers, it is not friendly to people, because the instruction itself does not tell us what it does in a human-readable language. To solve this problem, assembly language was invented. The two instructions above are written in assembly language as follows:

  • ADD AX, [SI]
  • MOV BL, AL

The instruction in assembly language is much more friendly to people because one can more easily tell what each instruction does. However, assembly language is not as friendly to computers. For computers to understand programs in assembly language, an assembler is needed to translate the programs into code in machine language.

With assembly language, programming is still a difficult task because instructions in an assembly language are mostly direct mapping of their machine-language equivalent and only describe fine and tiny steps of CPU operations. It is difficult to program in assembly language to solve real-world problems.

The idea of using a high-level, more human-friendly programming language was attributed to Konrad Zuse for his work between 1942 and 1945. He developed a high-level programming language called Plankalkül for a non–Von Neumann computer called Z4 that he made during the same period of time. The language was not implemented and used on Z4 or any other real computer during that time, but Konrad Zuse did write a computer chess program in the language.

The first high-level programming language for our modern computers (Von Neumann machines) is ALGOL 58, but it was soon surpassed by ALGOL 60, a structured programming language.

During the second generation of modern computers, more than 100 high-level programming languages were developed. The most popular programming languages include COBOL, for programming business systems; Fortran (an imperative procedural programming language), for scientific calculation; ADA, used by the military and National Defense in the US; Lisp (a functional programming language), used for symbolic or information processing; and Prolog (a logic programming language), for logic programming, especially during the first revival of artificial intelligence in the 1980s. There have been thousands of programming languages since the invention of the modern computer, according to the list at https://en.wikipedia.org/wiki/List_of_programming_languages.

However, most of these programming languages have never been used in real application development. The most popular programming languages used today include Python, Kotlin (for Android applications), Java, R (for data analysis and machine learning), JavaScript, C++, C#, PHP, Swift (for iOS applications), Go, and C, which has been a popular language for more than half of the century and is considered a foundational language of many other popular programming languages.

High-level programming languages are friendly to people but not friendly to computers. For computers to understand and execute programs written in a high-level programming language, they must be translated into machine language. Based on how programs in high-level languages are translated into target machine language, high-level programming languages are either compiled or interpreted. A program written in a compiled programming language such as C or C++ needs to be compiled into its target machine’s codes and linked with copies of code in static libraries, or linked with references to code in dynamic or shared libraries, so that the resulting object can then be executed by the target computer.

Programs written in an interpreted programming language can be executed directly by the interpreter of the programming language, in view of programmers and users. Behind the scenes, however, to speed up the execution of programs written in an interpreted programming language, the programs are often translated first into some intermediate codes, often called bytecodes. The bytecodes are then executed within a so-called virtual machine built into the interpreter of the programming language.

More readings about the history of computer programming languages can be found on the internet by searching for “history of computer programming.” The following are some of the articles:

  • https://en.wikipedia.org/wiki/History_of_programming_languages
  • http://www.softschools.com/inventions/history/computer_programming_history/369/
  • https://www.datarecoverylabs.com/company/resources/history-computer-programming-languages
  • https://www.computerhistory.org/timeline/software-languages/
  • https://www.thecoderschool.com/blog/the-history-of-coding-and-computer-programming/

A good overview of programming languages can be found at https://en.wikipedia.org/wiki/Programming_language.

It is worth noting that although well-formed computer programming languages only came into existence in the late 1940s, Ada Lovelace, an English mathematician who lived between 1815 and 1852, is considered the first computer programmer. When she was commissioned to translate an article based on Charles Babbage’s speech about his analytical engine, she wrote extensive notes alongside the translation to explain the principles and mechanism of the analytical engine. Those notes were later considered to be the first computer programs in history. For example, one set of those notes would calculate a sequence of Bernoulli numbers if executed on the analytical engine.

1.4 Python Programming Language

Python is an interpreted, high-level, general-purpose programming language. It is one of the most popular programming languages in the world, second only to Java. The 2023 rankings for programming language popularity can be found at https://www.hackerrank.com/blog/most-popular-languages-2023/.

The Development and Implementation of Python

Python was originally conceived and developed in the Netherlands by Guido van Rossum in the late 1980s. Its first release was Python 0.9.0 in 1991. Python 2.0 was released in 2000, nine years after the release of Python 0.9.0.

Most of the development of Python was accomplished between 2005 and 2013, when Guido van Rossum was working at Google, where he spent half of his time on the development of the programming language. The latest Python 2 release is Python 2.7.16, whereas the latest release of Python 3 is Python 3.9.0, at the time of writing.

Python is an interpreted programming language rather than a compiled programming language. Programs written in Python don’t need to be compiled into target machine code. Instead, they only need to be translated into bytecodes, then executed by a Python Virtual Machine (PVM), which is built into the Python interpreter.

Python language has different implementations with different names. CPython, written in C and known simply as Python, is what we will be using, and it is the implementation you will get from Python standard distribution at python.org by default. In addition to CPython, the following are some alternative implementations of Python:

  1. 1. JPython or Jython, and implementation for running on Java Virtual Machine (JVM)
  2. 2. IronPython, an implementation for running on .NET
  3. 3. PyPy, an implementation for working with a just-in-time (JIT) compiler
  4. 4. Stackless Python, an implementation of a branch of CPython supporting microthreads
  5. 5. MicroPython, an implementation for running on microcontrollers

These implementations may be of interest only for some special applications.

Advantages of Python

Python was designed with the principle of “programming for everyone.” Programs written in Python are clean and easy to read. Yet Python is a general-purpose programming language and very powerful to code in. Programming in Python has the least overhead, especially when compared to Java. Let’s take the famous “Hello World!” application as an example. In Java, to create this application, you need to write a class with a main method like the following:

class first {

    public static void main(String args[]){

    System.out.println("Hello World!");

    }

}

Then save it to a file and compile the file into Java bytecode by running the following command, provided you have a JDK correctly installed:

javac myhelloworld.java

This command will generate a file named first.class. You can then run command java first to have “Hello World!” spoken out.

In Python, however, you only need to write the following code and save to a file, say, myhelloworld.py:

print("Hello World!")

Then run the following command:

python myhelloworld.py

More importantly, Python supports different programming paradigms, including structured programming, imperative programming, object-oriented programming, functional programming, and procedural programming. Logic programming is probably the only exception; it is not supported by Python.

Python is very powerful for two reasons. The first reason, as you will see later, is that Python has many powerful language constructs and statements to represent data and operations on various data. The second reason is that there are thousands of third-party packages/libraries/modules available for all kinds of programming needs in addition to the large standard libraries included with every release of Python. For example, Python is widely used for data analytics and machine learning with the support of the NumPy, SciPy, Pandas, Matplotlib, and TensorFlow libraries.

Resources for Python and Python Education

The ultimate resource for Python and API libraries is the official website of the Python software foundation at https://www.python.org/. From that website, you will be able to access all the releases of Python and other packages and libraries that you may be interested in, in addition to the standard libraries that come with the official release of Python.

At the time of writing, the latest release of Python is 3.11.1, but you can always check and locate the latest release for your platform at https://www.python.org/downloads/. However, for your convenience and for your future study and use of Python, we will use a package management system called Anaconda to install and manage Python and all tools and libraries that you will need for this textbook, as detailed later.

There are plenty of resources on the internet for eager learners to learn almost anything, including programming in Python. To learn effectively on the internet, however, a learner must be able to choose the right materials from thousands—even millions—of choices, and that has proved to be a very difficult task. That’s probably one of the reasons why universities and professors are still needed: to set the right learning paths and to select the right resources for learners.

On the internet, the most authoritative learning source about the Python language is the documentation (https://docs.python.org/3/), including language references (https://docs.python.org/3/reference/index.html) and library references (https://docs.python.org/3/library/index.html), although they may not be articulated for beginners.

For learners who prefer to learn by reading, the following are some of the best resources on the web:

  • • https://docs.python.org/3/: This resource is from the official Python website, so the terms and jargons used should be deemed official at least within the Python community. It is a comprehensive section as well.
  • • https://www.w3schools.com/python/: This is a very popular site that offers resources covering many different languages.
  • • https://www.learnpython.org/: This is another good site for learning Python. it has many resources on specific topics.

For learners who love to watch videos and lectures, the following are recommended:

  • • Python Crash Course for Beginners, at https://www.youtube.com/watch?v=JJmcL1N2KQs, by Traversy Media. This video uses VS Code IDE to illustrate the program examples so that you can follow along once you have the required programming environment set up.
  • • Learn Python—Full Course for Beginners, at https://www.youtube.com/watch?v=rfscVS0vtbw, by freeCodeCamp.org and narrated by Mike Dane. This video is one of the best. Please note that the video lecture recommends the installation and use of PyCharm IDE. However, the installation and use of VS Code IDE is required in this book.

1.5 Getting Ready to Learn Programming in Python

To learn the content covered in the rest of this book, you first need to have the following installed on your computer:

  1. 1. A newer version of Python from https://www.python.org/. The latest version, at the time of writing, is Python 3.11.1. The standard distribution includes everything you need to interpret and execute Python programs, which are also called Python scripts.
  2. 2. A newer version of Jupyter Notebook, from https://jupyter.org/. Jupyter Notebook is a powerful interactive programming environment supporting up to 40 programming languages, including Python.
  3. 3. A newer version of Visual Studio Code (VS Code) from https://code.visualstudio.com/. VS Code is a free and open-source code editor developed by Microsoft, and it will be used as an integrated development environment (IDE) and to host Jupyter Notebook for interactive programming.

In this section, we will learn how to install Python, Jupyter Notebook, and Visual Studio Code IDE on our computer. A standard distribution of Python includes a large volume of libraries for general programming needs. Special applications such as those in data science and machine learning will require special libraries to be installed. We will learn about that in later units when we need them.

Installing and Setting Up the Python Programming Environment

There are different ways of setting up your Python programming environment. The method we present below is believed to be more reliable and less subject to future changes.

Installing Python

The first package we need to install is Python. The steps are as follows:

  1. 1. To ensure a clean installation of Python, uninstall all old versions of Python that may have previously been installed on your computer and check the value of the PATH environment variable to delete all those, and only those, related to Python. To ensure that all have been deleted properly, open a shell terminal such as CMD or PowerShell on Windows and check if a Python installation still exists by trying to run it. You should be told that Python is not recognized.
  2. 2. Go to https://www.python.org/downloads/ and look for the newest release of Python for your platform. For example, if you want to install Python on your Windows machine, download the latest release for Windows. Normally, the website should be able to detect the type of system you are using to access the website and show you the right Python package for you to download. You should, however, be aware of what you will be getting from the website.
  3. 3. Run the installer to install it. If installing Python on a Windows platform, all you need to do to start the installation is to double-click the downloaded file or press the run button if it does not start the installation automatically.

    After the installation has started, a window will pop up, as shown here:

Python 3.11.1 (64-bit) Setup

----------------------------------

Install Python 3.11.1 (64-bit)

Select Install Now to install Python with default settings, or choose Customize to enable or disable features.

→ Install Now

C:\Users\james\AppData\Local\Programs\Python\Python311

Includes IDLE, pip and documentation

Creates shortcuts and file associations

→ Customize installation

Choose location and features

☑ Use admin privileges when installing py.exe

☑ Add gython.exe to PATH!

Cancel

  1. You can either let it install Python automatically for you by clicking “Install Now” or choose “Customize installation.” When you choose “Customize installation,” you have the opportunity to choose where Python will be installed on your computer and what optional modules and features will be installed.
  2. In either case, you must check the box to add Python to the PATH environment variable. Choose yes to add Python to the system PATH environment variable so that Python and other related commands can be found.
  3. Do a customized installation so that you will see what is to be installed and where. When doing customized installation, remember to check the box to install pip, Python package manager, and other components within the distribution, as shown here:

Python 3.11.1 (64-bit) Setup

----------------------------------

Optional Features

☑ Documentation

Installs the Python documentation files.

☑ pip

Installs pip, which can download and install other Python packages.

☑ td/tk and IDLE

Installs tkinter and the IDLE development environment.

☑ Python test suite

Installs the standard library test suite.

☑ py launcher    ☑ for all users (requires admin privileges)

Use Programs and Features to remove the 'py' launcher.

Back    Next    Cancel

  1. Check the following advanced options, as shown here:

Python 3.11.1 (64-bit) Setup

----------------------------------

Advanced Options

❑ Install Python 3.11 for all users

☑ Associate files with Python (requires the 'py' launcher)

☑ Create shortcuts for installed applications

☑ Add Python to environment variables

❑ Precompile standard library

❑ Download debugging symbols

❑ Download debug binaries (requires VS 2017 or later)

Customize install location

d:\Programs\Python\Python311

You will require write permissions for the selected location.

Back    Install    Cancel

  1. To check whether Python has been installed successfully on your computer, open a shell terminal and run the following command:

$ python --version

  1. If the installation was successful, the version of Python will be displayed and should be the same as the one you have downloaded from python.org.

Setting Up a Virtual Environment for a Python Project

When programming in Python, you will need a Python interpreter and any additional libraries required for the project. Libraries required for one project are different from those for other projects. To ensure that each project has the right programming environment without interfering with others, Python uses a technology called a virtual (programming) environment, which can be created and activated for each project. When you work in a virtual environment for a specific project, all installations of additional libraries will be used only for that specific project.

To create and use a virtual environment, take the following steps:

  1. 1. Check whether you have a tool called pipenv installed with your Python installation by running the following command on Windows:

    where.exe pipenv

  • or the following command on Linux or Mac:

    which pipenv

    If it is installed, the command will tell you where it is located on your computer, and you can go to step 3.

  • 2. If the command cannot be located on your system, you will need to install it by running the following command on a shell terminal:

    pip install pipenv

    If you are running the shell terminal as a regular user, pipenv will be installed under your home directory, so that you will have to add the location to the environment variable PATH. It is better to start a shell terminal as an administrator, then run the above command as root/administrator to ensure that pipenv will be installed globally and be accessible for all users with the already set value for PATH.

  • 3. Once you are sure that pipenv is installed, create a directory for your new project and change the work directory to that directory, say c:\dev\testproject as an example, by running the following commands in sequence:

    c:\dev> mkdir testproject

    c:\dev> cd testproject

  • 4. Within the testproject directory, run the following command:

    c:\dev\testproject> pipenv install

    This will create a virtual environment for your project rooted at c:\de\firstproject.

  • 5. To work on the project with the virtual programming environment, you need to activate the virtual environment for the project by running the following command within the project directory:

    c:\dev\testproject> pipenv shell

    Once the virtual environment has been activated, the prompt of the shell terminal will become something similar to the following:

    (testproject-UCP5-sdH)c:\dev\testproject>

    Please note the text within the parentheses. It contains the name of the project directory. It means that you are now working in a subshell terminal invoked by pipenv.

  • 6. From now on, any package installed by running the pip command on this subshell terminal will be only part of this virtual environment, without interfering with installations elsewhere for other projects.
  • 7. To get out of the virtual environment, simply type “exit” to close the subshell, as shown below:

    (testproject-UCP5-sdH)c:\dev\testproject>exit

    The prompt will become

    c:\dev\testproject>

  • 8. If you want to remove the virtual environment created for a project, run the following command within the subshell on the root of the project directory:

    (testproject-UCP5-sdH)c:\dev\testproject> pipenv -- rm

Installing Jupyter Notebook

The second package that needs to be installed is Jupyter Notebook. The steps are as follows:

  1. 1. After you have successfully installed Python, install Jupyter Notebook with pip, the Python package manager that comes with the Python installation, by running the following command at a shell terminal:

    pip install Jupyter

    or use the following command if pip itself is not recognized as a command, but only installed as a module of Python:

    python -m pip install jupyter

    To see if it has been successfully installed, run the following command from a shell terminal:

    jupyter-notebook

    The command will start a web service on your computer, then launch Jupyter Notebook Service within your default web browser.

    Start a new notebook by clicking “New” and “Choose Python 3.” You can then start the program interactively within the notebook, as shown in Figure 1-3.

    An untitled Jupyter webpage with the text “Hello World!” in the notebook.

    Figure 1-3: Jupyter Notebook in browser

Installing Visual Studio Code

The last package to be installed is Visual Studio Code, a free and open-source IDE developed by Microsoft that can be used for Python programming. The steps are as follows:

  1. 1. Go to https://code.visualstudio.com/, click “Download,” and download the installer for your platform.

    If you want to see other download options, such as a version for a platform other than the one identified by the VS Code website, you can scroll down further to check.

  2. 2. Double-click “Downloaded Installer” for your platform to run and install Visual Studio Code.

To run Visual Studio Code on Windows, click the Start menu, scroll down to find Visual Studio Code, and click.

Additional Tools Supporting Software Development in Python

To make software development more efficient, the Python community has made the following frameworks or systems available for Python developers.

Buildbot

Buildbot is a framework intended to automate all aspects of software development. It supports parallel and distributed job execution across multiple platforms, with version control and job status reporting and automated job scheduling when required resources become available. It is hosted at https://buildbot.net/. An introductory section can be found at http://docs.buildbot.net/current/tutorial/firstrun.html.

Trac

Compared to Buildbot, Trac is a simpler web-based software project management system that can track issues and bugs. It requires access to a database such as SQLite or MySQL. Trac is hosted at https://trac.edgewall.org/, but the latest release can be found at https://pypi.org/project/Trac/.

Roundup

Compared to Trac, Roundup is an even simpler tool to track issues in a software development project. You can do so via the command line, the web, or email. The system can be found at https://pypi.org/project/roundup/.

1.6 Getting a Taste of Programming with Python

In this section, you will see how to solve simple problems or do simple tasks with Python. At this time, you do not need to understand every piece of code because you will learn all those details later.

Program Interactively with Python Interactive Shell

Programming for computers is writing instructions for computers to execute. Interactive programming is programming in an environment in which you can interact with a computer, the interpreter of the programming language, more precisely, instruction by instruction. The opposite of interactive programming is batch programming, in which you will need to write a complete program and then feed the entire program to a language engine such as an interpreter, language runtime, or virtual machine in order to execute it. In interactive programming, a piece can be a single statement or a group of statements, but not a complete program. One of the advantages of interactive programming is immediate feedback on the code pieces from the interpreter.

The simplest interactive programming environment for Python is the Python interactive shell. It can be quickly started by running command Python at a command prompt such as within a Windows PowerShell, as shown here:

PS S:\Dev> python

The started interactive Python programming environment should look like this:

PS S:\Dev> python

Python 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:50:39) [M.06 v.1934 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>>

Here, >>> is the prompt of the Python interactive shell, waiting for your input, which will then be evaluated by the interpreter. This is how you program interactively within this environment.

As the de facto standard, our first program in the interactive Python programming environment is to say “Hello World!” as shown here:

PS S:\Dev> python

Python 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> print("Hello World!")

Hello World!

>>>

As can be seen above, this program in Python needs only one statement made of one line of code, whereas in C/C++ or Java, it would need a dozen lines of code.

Please note that in the previous example, the characters behind the Python prompt >>> are Python statements you need to type, and the rest are output from Python interpreter or Python Virtual Machine (PVM).

Our next sample program within the Python interactive shell is to assign 8 to variable x and assign 9 to variable y, then print out the sum of x and y, as shown here:

PS S:\Dev> python

Python 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32

Type .help., "copyright", "credits" or "license" for more information.

>>> print("Hello World!")

Hello World!

>>> x = 8 # assign 8 to variable x

>>> y = 9 # assign 9 to variable y

>>> print(f'{x} + {y} = {x+y}')

8 + 9 = 17

>>>

Within this interactive programming environment, you can type any Python expression directly behind >>>, and it will then be evaluated. An example is shown here:

PS S:\Dev> python

Python 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> 1350-789*3/26

1258.9615384615386

>>>

You may have realized that the interactive Python programming environment can be used as a powerful scientific or financial calculator, with all the functions you need, as long as you know where to find the functions and how to import and use them to write formulas.

Throughout the textbook, we will occasionally give examples in Python interactive shell when it is more convenient and proper in context. However, our preferred interactive programming environment is Jupyter Notebook within VS Code IDE, as you will see later in this section.

Program with VS Code IDE

Interactive programming environments like Python Shell and Jupyter Notebook are good for testing Python statements, code blocks, and some real programming tasks such as data analytics, where interactive programming is more suitable. However, writing programs that contain thousands of lines of code in a Python interactive shell or Jupyter Notebook is inconvenient.

As previously mentioned, we will be using VS Code as our IDE. Our first small project is to write a program that takes an integer from the user input and tells whether it is a prime number or not. The first step to do this is to start VS Code from the Windows Start menu or desktop. In Windows or iOS, you may type “VS Code” in the search bar to launch the application.

To create a Python program file within VS Code, choose “New File” from File, and then save the file as xyz.py, where xyz is your preferred name for the small project. Here, we use “primetest.py.” VS Code will ask you to choose a folder for the program, and you may create one and then select it to open after the file is created. The program in VS Code will look like the one in Figure 1-4.

A terminal page in code describing the program, input, and other specifications.

Figure 1-4: Python program in VS Code

At this time, you are not required to fully understand the program, but if you are interested, you may type the code line by line into your VS Code and run it by clicking the play button at the top-right corner of the VS Code window, just to get a taste of programming in VS Code with Python.

Please note that if you have multiple Python program files open in VS Code and want to run a particular one within the IDE, you will need to click the file to make it active; then, within the editing area, click the right button of the mouse to pop up a menu and select run for your particular program. This is even more important if you have multiple editing tabs open for different programs.

Use Jupyter Notebook Within VS Code to Program Interactively

You may recall that Jupyter Notebook can be launched and run in a web browser, but for the purposes of this text, Jupyter Notebook will be run and used within VS Code to program interactively to allow you to benefit from many of the features that VS Code has.

Because Jupyter Notebook is natively supported within Visual Studio Code (as long as Jupyter Notebook is installed in your Python environment), all you need to do to get Jupyter Notebook running within VS Code is to open or create a notebook file, with extension ipynb in VS Code.

There are two ways to create a Jupyter Notebook file. One is to press Ctrl+Shift+P to search for the command “create new blank Jupyter Notebook,” then click the command. A new blank Jupyter Notebook will be created within VS Code.

Alternatively, we can also simply create a new text file, and then save the file as xyz.ipynb, where xyz is a name you prefer and ipynb is the file’s extension. Please note that to ensure the file type is ipynb, you have to select Jupyter (.ipynb) or all files (*.*) from the “Save as Type” list.

In either case, you may be asked to install an extension for Jupyter Notebook support. Click yes when you are asked to. You may also be asked to install Python if you have not done so yet or if the installation path has not been added to the environment variable. Click yes too to make sure you have Python properly installed on your computer.

Once a new Jupyter Notebook is created, you can then use it to program interactively within VS Code and enjoy many of the smart features of VS Code, such as those offered by IntelliCode.

Another advantage of using Jupyter Notebook within VS Code is that you can export a notebook as an HTML, PDF, or Python Script file. If you use Markdown cells to document your coding in code cells, you can produce a very nice document as a PDF or in HTML.

Later on, you will be required to create a Jupyter Notebook for each chapter or section, if there are many coding examples to work on in a section. You will use the Jupyter Notebook to interactively program all sample codes in that chapter or section or to test your own code to reinforce your learning. For chapter x, the Jupyter Notebook should be named chapter-x.ipynb, and for section x.y, the notebook should be named section-x.y.ipynb.

In the next section, we will provide a brief introduction to Markdown language that you can use to document your work either within Markdown cells of Jupyter Notebook or within a Markdown file, a file with md as the file extension.

Write Documentation in Markdown

Markdown is a simple and straightforward markup language with a plaintext-formatting syntax. It was created by John Gruber and Aaron Swartz in 2004. With Markdown, you can write a document in an easy-to-write and easy-to-read format, then convert it to HTML. Within VS Code IDE, a Markdown file, with md as the extension, can be converted to PDF as well as HTML. Moreover, documentation written in Markdown cells of a Jupyter Notebook can be automatically converted into docstrings when the Jupyter Notebook is exported into a Python script file, and when the notebook is exported into PDF, scripts in code cells will be nicely embedded into documentation written in Markdown cells. Together with markup syntax for program code, Markdown cells within Jupyter Notebook running in VS Code provide a much more user-friendly way to include program code in rich and formatted documents.

The Markdown language has been extended since its initial release by John Gruber. You will be introduced to both the basic syntax and some extended syntax (but only those supported by Jupyter Notebook within VS Code) to serve our purposes in this book.

Headings

In Markdown documents, headings are simply led by one or more hash symbols. As we have seen in previous sections, a Level 1 heading is led by a single hash symbol, a Level 2 heading is led by two hash symbols, and a Level 3 heading is led by three hash symbols. You can have up to six levels of headings in a document, as shown below:

# This is a Level 1 heading

This is a Level 1 heading

## This is a Level 2 heading

This is a Level 2 heading

### This is a Level 3 heading

This is a Level 3 heading

#### This is a Level 4 heading

This is a Level 4 heading

##### This is a Level 5 heading

This is a Level 5 heading

###### This is a Level 6 heading

This is a Level 6 heading

Please note the space between the formatting symbol or symbols and the text to be formatted. A single space should be placed between the formatting symbol and the text being formatted in Markdown.

Paragraphs

In Markdown, paragraphs are separated with one or more blank lines.

This is a paragraph.

This is another paragraph.

  • This is a paragraph.
  • This is another paragraph.

New Lines

In Markdown, to break a line like you would with <br/> in HTML, use more than one single space to break the line.

This line will break.  This line starts on a new line.

  • This line will break.
  • This line starts on a new line.

Italic, Bold, and Strikethrough Texts

To format text to be italic in Markdown, simply lead it with a * or underscore _ and use another * or _ to indicate the end of the text; to make text bold, use ** or __; to make text both bold and italic, use ***; to have a line strikethrough the text, use ~~. The following are examples:

_ Italic_

  • Italic

** Bold **

  • Bold

*** both italic and bold ***

  • Italic and bold

~~ strikethrough ~~

  • Strikethrough

Horizontal Rules

To add a horizontal line within a document, like <hr /> in HTML, use three hyphens ---. Most of the time, this has the same effect in Word.

---

Keyboard Keys

In computing documentation, we often need to explain what key is used on the keyboard. To represent a key on the keyboard, we use HTML kbd tags directly, as shown below:

<kbd> Ctrl </kbd> <kbd> A </kbd>

  • Ctrl+A

<kbd> Ctrl+Shift+F3 </kbd>

  • Ctrl+Shift+F3

Unordered Lists

With Markdown, writing an unordered list is rather straightforward, as shown below:

* first list item

* second list item

 * first item of sublist

 * second item of sublist

* third list item

The rendered result will be the following:

  • • first list item
  • • second list item
    • - first item of sublist
    • - second item of sublist
  • • third list item

We can also use - in place of *.

Ordered Lists

To write an ordered list in Markdown is straightforward too, as shown below:

1. first list item

2. second list item

 * first item of sublist

 * second item of sublist

3. third list item

The rendered result will be

  1. 1. first list item
  2. 2. second list item
    • - first item of sublist
    • - second item of sublist
  3. 3. third list item

Definition Lists

The simple Markdown syntax for a definition list does not work in Jupyter Notebook within VS Code. However, we can use HTML <dl> tags directly to make such a list, as shown below:

<dl>

<dt> Python </dt>

<dd> It is a popular programming language, widely used in AI and Data Science. </dd>

<dt> AI </dt>

<dd> Short for Artificial Intelligence.

It is the study of how to design and develop smart artifacts.</dd>

</dl>

The rendered result will be:

  • Python
  • It is a popular programming language, widely used in AI and Data Science.
  • AI
  • Short for Artificial Intelligence. It is the study of how to design and develop smart artifacts.

Links

To add a link with Markdown, put the anchor name in a square bracket, and put the URL in a pair of parentheses, as shown below:

[Markdown Home](https://www.markdownguide.org/)

The rendered result will be the following text, which will take the user to https://www.markdownguide.org/ when clicked:

  • Markdown Home

As in Word and some other editors, legitimate URLs are usually automatically linked without any markup tags. In Markdown, if you don’t want a URL to be automatically linked, you can enclose it with a pair of backticks, just treating it as program code, as shown below:

`https://www.markdownguide.org/`

Links to Internal Sections

A specific ID can be added to each header. Such IDs can be used as internal anchors in a link, as shown below:

[Assignment 1] (#assignment_1)

  • Assignment 1

Images

To add an image to your documentation, use a syntax similar to that used for adding links, but with an exclamation mark at the front of the open square bracket, as shown below:

![Markdown logo](AU_and_50_logo.png)

The image will be rendered as

Athabasca University logo

Blockquotes

To include a blockquote in your documentation, use an angle bracket at the start of each line, as shown in the following example:

> COVID-19 UPDATES

>

> EXAMS

>

> HELP & SUPPORT

>

> FACULTY OF BUSINESS STUDENTS

>

> FACULTY OF SCIENCE STUDENTS

  • COVID-19 UPDATES
  • EXAMS
  • HELP & SUPPORT
  • FACULTY OF BUSINESS STUDENTS
  • FACULTY OF SCIENCE STUDENTS

Tables

To create a table in Markdown, use the pipe character | to divide columns and a sequence of dashes/hyphens to separate the header of a table, as shown below:

    | Markdown symbol | Description | HTML equivalent  |

    | :----------------| :-----------: | -----------: |

    | # | Level 1 heading | h1 |

    | ## | Level 2 heading | h2 |

The table will be rendered as

Markdown symbol

Description

HTML equivalent

#

Level 1 heading

h1

##

Level 2 heading

h2

Please note the colons used in the formatting syntax. A single colon to the left of dashes means to align all the text in the column to the left, a single colon to the right of dashes means to align all the text in the column to the right, and adding a colon to both sides means to align the text at centre. You can also use other Markdown syntax on the text in the table, such as italic, bold, and so on.

Inline Program / Script Code

When writing a report on a software project, you may need to include code samples in the report. To include a code sample within a single sentence, enclose the code within a pair of backticks `, as shown below:

The ` range(start, end, step) ` function is used to produce a sequence of integer numbers.

The rendered result will be:

  • The range(start, end, step) function is used to produce a sequence of integer numbers.

Code Block

In Markdown, a block of program code can be marked up using a pair of triple backticks ```, as shown below:

```Python

for i in range(1, 10):

for j in range(1, i + 1):

print(f'{i} * {j} = {i * k}')

```

The rendered result will be:

  • for i in range(1, 10):
  •   for j in range(1, i + 1):
  •     print(f'{i} * {j} = {i * k}')

Please note that the name of the programming language right behind the opening triple backticks is optional, though with the name, the code will be automatically highlighted in a way specific for the language.

Mathematical Formulas and Expressions

With Markdown in Jupyter Notebook, you can embed LaTeX representation mathematical formulas directly within your text. LaTeX is a typesetting system used for scientific publications. It would take you some time to learn the complete system. The following examples show you how to represent mathematical formulas in your documentation.

1.  $\hat{Y} = \hat{\beta}_{0} + \sum \limits _{j=1} ^{p} X_{j}\hat{\beta}_{j} $

2.  $\frac{n!}{k!(n-k)!}$

3.  $\binom{n}{k}$

4.  $\frac{\frac{x}{1}}{x - y}$

5.  $\sqrt{k}$

6.  $\sqrt[n]{k}$

7.  $\sum_{i = 1}^{10} t_i$

8.  $\int_0^\infty \mathrm{e}^{-x},\mathrm{d}x$

9.  $f(x) = x^2 + 2, if\ x = 2$

10. $\oint_C x^3\, dx + 4y^2\, dy$

11. $2 = \left(

\frac{\left(3 - x\right) \times 2}{3 - x}

\right)$

12. $\sum_{m = 1}^\infty\sum_{n = 1}^\infty\frac{m^2\, n}

{3^m\left(m\, 3^n + n\, 3^m\right)}$

13. $\phi_n(\kappa) =

\frac{1}{4\pi^2\kappa^2} \int_0^\infty

\frac{\sin(\kappa R)}{\kappa R}

\frac{\partial}{\partial R}

\left[R^2\frac{\partial D_n(R)}{\partial R}\right]\,dR$

The rendered result of the above Markdown code is shown in Figure 1-5. You can get the same result if you export the notebook file to PDF or HTML format.

A complex mathematical formula containing the elements that were coded.

Figure 1-5: Rendered result of the Markdown code

To-Do List

You may wish to have a to-do list in your learning notebook. With Markdown, you can get that accomplished as follows:

- [ ] a bigger project

 - [x] first subtask

 - [x] follow-up subtask

 - [ ] final subtask

- [ ] a separate task

The rendered result will look like this:

  • • ☐ a bigger project
    • – ☑ first subtask
    • – ☑ follow-up subtask
    • – ☐ final subtask
  • • ☐ a separate task

Escape Sequence for Special Characters

Because, as we have seen, some characters have special meaning in Markdown syntax, we need to use the backslash to allow these characters to keep their normal meaning. These characters include the backslash \, backtick `, asterisk *, underscore _, the pound hash symbol #, plus sign +, hyphen/dash -, period ., and exclamation mark !, as well as curly braces {}, square brackets [], and parentheses (). For example, if we want to have * in our documentation, we need to use \* instead of a simple *. This is especially necessary since confusion may arise from using these symbols without the backslash. Otherwise, you can use a special character directly, such as in the following example:

# A heading with a plus sign \+

This will be rendered as:

  • A heading with a plus sign +

Programming Interactively with Jupyter Notebook Within VS Code

Earlier in this section, we learned how to program interactively within a Python Shell launched within a CMD or PowerShell window, and how to start Jupyter Notebook within VS Code. In this section, we will find out how to use Jupyter Notebook within VS Code to program interactively as well as how to document your work and learning journey in both code cells and Markdown cells of Jupyter Notebook within VS Code.

Compared to the Python interactive shell, Jupyter Notebook is a much better and more powerful environment for interactive programming and has the following advantages:

  1. 1. Everything you typed and the output from the Python interpreter are kept in a notebook file so that you can go back and review your work whenever needed. In the Python interactive shell, however, everything within the shell will be lost as soon as you exit from it.
  2. 2. Within a programming cell of Jupyter Notebook, you can write and edit as many Python statements as you want, and the interpreter will wait until you hit Shift+Enter to run all the statements within the active cell, whereas the simplest interactive programming environments run only one statement at a time.
  3. 3. You can go back to a previous programming cell and edit the code in it, and then rerun the code as you wish, which you cannot do in a Python Shell.

Again, our first example to program interactively in Jupyter Notebook is to say Hello World! But before we begin, we need to create a new Jupyter Notebook named section-1.6.ipynb for this section. If you are using this book for a course, we recommend that on your desktop, you create a folder using the course name or number, and then create this and all the Jupyter Notebook files within this folder or subfolders to better organize all the files for the course. For the purposes of this textbook, we will give this folder the name “comp218.” In the example shown here, the notebook file section-1.6.ipynb is under a subfolder named “VS Code.”

With Jupyter Notebook in VS Code, you can use Markdown cells to present your ideas and thoughts about the program you are to write, and use code cells to write program code and Python documentation on your code. In this first example, we first write the following in a Markdown cell:

# First program in Jupyter Notebook within VS Code

As is tradition in teaching computer programming, our first program in Jupyter Notebook is to say Hello World!.

## How to start a program

As always, a program should begin with a brief description of the program, including what it does, who wrote it and when, and how it works and/or should be used. This is especially necessary for independent program files such as the Python script files you will be developing using VS Code IDE. Within a code cell of Jupyter Notebook, brief documentation is still needed if the code within the cell is complicated either grammatically or logically. You do not need to document if the code is only a scribble for testing.

In Python, brief documentation at the beginning of a program file is enclosed with a pair of triple single/double quotation marks, such as

"""

brief documentation

"""

or

'''

brief documentation

'''

This is called a docstring. Different from comments made on program code using a single hash symbol #, docstrings are meant to be formal documentation of the code that can be retrieved from an object.

We then write the following in a code cell:

    """

    This simple program is just to say Hello world!

    Everything between the two triple quotation marks is

    treated as documentation about the program.

    """

    print('Hello world!')   # a single statement of the program - inline comment

This results in the notebook printing, outside of the code cell, the following:

Hello world!

In Jupyter Notebook, there are two types of cells for input. One is the code cell for you to write actual program code in. The other is the Markdown cell for you to write more detailed notes or reports about your work within the notebook using the Markdown language we have introduced in the previous section.

When a new cell is created in a Jupyter Notebook within VS Code, by clicking the plus sign + on the left side of the notebook window, you will get a code cell by default. To change a code cell to a Markdown cell, click the M↓ button at the top of the cell; to change a Markdown cell back to code cell, click the {} button at the top of the cell.

Please note that when using Jupyter Notebook within a web browser, to switch a code cell to Markdown cell you will need to click “Code” at the top of the cell, and then choose “Markdown” from the pop-up menu.

In the Jupyter Notebook example above, two cells are used. The first is a Markdown cell, in which we explain in more detail what we are going to do for our first Python program and how we will do it, whereas the second cell is a code cell, in which actual Python code is written, together with docstring and inline comments, for the program.

In the simple program above, a pair of triple quotation marks is used to enclose some string literals, called docstrings, as the formal documentation for the program or module. In addition to each program file or each module file, a docstring is also recommended, and even required, for each function, class, and method. Docstrings should be placed at the very beginning of a program file, module file, or right after the header of the definition of a class, function, or method. The docstrings will be retrieved and stored as the __doc__ attribute of that respective object and can be displayed when help is called on that object. Python also has a utility program called pydoc that can be used to generate documentation from Python modules by retrieving all the docstrings.

Right after the docstring is a print statement that will print out Hello World! when the program is executed by pressing Shift+Enter while the cell is still active. A cell is active if there is a vertical blue bar on the left side of the cell. To execute statements inside an active cell, we can also click the play button (the thick right-facing arrow) at the top of the cell.

Our next sample program is to assign integers to two variables and then print the sum, difference, product, quotient, integer quotient, remainder, power, and root. The Python statements to accomplish these operations are as follows:

#Operators and expressions in Python

In the next cell we will show the use of operators and expressions in Python.

#Operators

  • + operator for addition
  • - operator for subtraction
  • * operator for multiplication
  • / operator for division
  • % operator for modulus
  • ** operator for exponentiation
  • // operator for floor division

We then write the following in a code cell:

i = 5 # assign 5 to variable i. Everything behind the hash mark is comment

j = 3 # assign 3 to variable j

print(f"{i} + {j} = {i + j}") # print i + j

print(f"{i} - {j} = {i - j}") # print i - j

print(f"{i} x {j} = {i * j}") # print i * j

print(f"{i} / {j} = {i / j}") # print i / j (/ is division)

print(f"{i} // {j} = {i // j}") # print i // j (// is quotient)

print(f"{i} % {j} = {i % j}") # print i % j (% is modulus)

print(f"{i} ** {j} = {i ** j}") # print the result of i power of j

print(f"root {j} of {i} = {i ** (1/j)}") # print the result of root j of i

This prints the following:

  • 5 + 3 = 8
  • 5 − 3 = 2
  • 5 x 3 = 15
  • 5 / 3 = 1.6666666666666667
  • 5 // 3 = 1
  • 5 % 3 = 2
  • 5 ** 3 = 125
  • root 3 of 5 = 1.7099759466766968

As you can see, in this interactive programming environment, you can write and edit many Python statements within a single cell. You may simply write some statements to accomplish certain calculation or data analysis tasks that cannot be done even on an advanced scientific finance calculator.

This next example in Jupyter Notebook within VS Code calculates the sum and the product of 1, 2, 3, … 100:

"""

This is to calculate the sum of 1,2,…,100.

"""

s=0

for i in range(100):

    s+=i+1

print(f"Sum of 1,2…,100 is {s})

"""

This is to calculate the product of 1,2,…,100

"""

p=1

for i in range(100):

    p*=i+1

print(f"The product of 1,2,…,100 is {p}")

This prints the following:

  • Sum of 1,2,…,100 is 5050
  • The product of 1,2,…,100 is 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Our next sample program in Jupyter Notebook within VS Code creates a simple data visualization to showcase how Python can be used for that purpose.

The data for x-axis are a list of letter grade in a grading system; the data for y-axis contain a list of numbers representing how many students received each corresponding grade in a class. The purpose of visualization is to see how the grades are distributed within the class.

import matplotlib.pyplot as plt

x = ['D','C-','C','C+','B-','B','B+','A','A','A+']

y = [6,9,12,19,23,28,15,13,7,5]

plt.bar(x,y)

plt.title('A showcase')

plt.xlabel('Letter Grade')

plt.ylabel('# of Students')

plt.show()

Here we use a module called Matplotlib to visualize the data by plotting graphs. The result of the program as shown in Figure 1-6.

A bar graph labeled “A showcase” with the number of students on the x-axis and the letter grade on the y-axis.

Figure 1-6: Graph produced by a Python script in Jupyter Notebook

As you can see, with only 11 lines of code, you can produce a nice graph to visualize the data in Python.

Run Python Programs Outside IDE

The VS Code IDE we used in previous sections is good for developing programs or applications, but it is impractical to start an IDE each time you need to run a Python program.

So how can we run a Python program or scripts stored in a file? In a previous section, we wrote a program called primetest.py. To run the program without invoking VS Code, we need to take the following steps:

  1. 1. Start Windows PowerShell or Windows Command Prompt by typing “terminal” in the search field on the Windows taskbar if you are on other platforms such as Linux. A shell terminal will come up; type the following:

    PS S:\Dev\Learn_Python>

  2. 2. Within the terminal, go to the directory where the Python program file is located.
  3. 3. Change the working directory to that folder by typing the following PowerShell command:

    cd  S:\Dev\Learn_Python\samples\

  4. 4. Run the Python program file by typing the following command at the PowerShell prompt:

    python .\primetest.py

    the result is shown below:

    PS S:\Dev\Learn_Python> cd .\samples\

    PS S:\Dev\Learn_Python\samples> python .\primetest.py

    give me an integer that is greater then 1, and I will tell you if it is a prime: 23

    23 is a prime

    PS S:\Dev\Learn_Python\samples>

Note that to run a Python program from a terminal, two conditions must be met: (1) The location of the Python interpreter (python.exe) must be in the PATH system variable, so that Windows is able to find it. (2) The Python program file should be in the current working directory of the terminal. If that is not the case, you must either change your current working directory to where the Python program file is located or specify the path to the program file. Assume we change the current working directory from the one shown at the end of the list above to the one shown here using the command cd:

PS S:\Dev\Learn_Python>

Because now the primetest.py program is one-level down from the current working directory at .\samples, where the leading dot (.) refers to the current directory, to run the program, you will have to specify the path to the program, as shown here:

PS S:\Dev\Learn_Python> python .\samples\primetest.py

give me an integer that is greater than 1, and I will tell you if it is a prime: 91

91 is divisible by 7, so that 91 is not a prime

PS S:\Dev\Learn_Python> python .\semples\primetest.py

give me an integer that is greater than 1, and I will tell you if it is a prime: 23

23 is a prime

PS S:\Dev\Learn_Python>

Make the Python Program File Executable

Sometimes, we may still consider it inconvenient to run a Python program file from a terminal by feeding the file to the Python interpreter and rather prefer to make the program executable on its own so that it can be run by clicking the file within Windows File Explorer. Luckily, a tool called pyinstaller can be installed and used to do that. To install it, you need to run PowerShell or another shell terminal as an administrator. Within the terminal, run the command shown here to install pyinstaller using the pip tool:

PS S:\Dev\Learn_Python> pip install pyinstaller

Requirement already satisfied: pyinstaller in s:\python\python311\lib\site-packages (5.7.0)

Requirement already satisfied: setuptools>=42.0.0 in s:\python\python311\lib\site-packages (from pyinstaller) (67.4.0)

Requirement already satisfied: altgraph in s:\python\python311\lib\site-packages (from pyinstaller) (0.17.3)

Requirement already satisfied: pyinstaller-hooks-contrib>=2021.4 in s:\python\python311\lib\site-packages (from pyinstaller) (2022.15)

Requirement already satisfied: pefile>=2022.5.30 in s:\python\python311\lib\site-packages (from pyinstaller) (2023.2.7)

Requirement already satisfied: pywin32-ctypes>=0.2.0 in s:\python\python311\lib\site-packages (from pyinstaller) (0.2.0)

PS S:\Dev\Learn_Python>

Before you can use the pyinstaller tool to make the conversion, you will need to copy the Python library into Windows’ system32 directory, as shown below:

PS C:\WINDOWS\system32> cp S:\Python\Python311\python311.dll .

PS C:\WINDOWS\system32>

Now you can simply run the command pyinstaller <Python program file name> to make the conversion, as shown here.

PS S:\Dev\Learn_Python\samples> pyinstaller .\primetest.py

1435 INFO: PyInstaller: 5.7.0

1435 INFO: Python: 3.11.1

1445 INFO: Platform: Windows-10-10.0.22621-SPO

1445 INFO: wrote S:\Dev\Learn_Python\samples\primetest.spec

1460 INFO: UPX is not available.

1460 INFO: Extending PYTHONPATH with paths ['S:\\Dev\\\Learn_Python\\samples']

pygame 2.3.0 (SDL 2.24.2, Python 3.11.1)

Hello from the pygame community. https://www.pygame.org/contribute.html

5243 INFO: checking Analysis

5254 INFO: Building because inputs changed

5254 INFO: Initializing module dependency graph…

5254 INFO: Caching module graph hooks…

5285 WARNING: Several hooks defined for module 'numpy'.

The created executable is under .\build\primetest, named primetest.exe.

Please note, however, that the executable file requires the Python DLL library in order to run, so that you can either (1) make the DLL library searchable and accessible by Windows OS or whatever OS you are using, if you have many executables generated from Python program files, or (2) copy the Python DLL to where the executable file is located. In this case, since we are using Python3.11, the library is named python311.dll—which is located within the installation directory of Python3.11, which is s:\python\python311—we can simply copy the DLL file to .\build\primetest for a test, as shown below:

-a---    2022-12-06  8:12 PM        1463681    NEWS.txt

-a---    2022-12-06  8:10 PM         101752    python.exe

-a---    2022-12-06  8:10 PM          65912    python3.dll

-a---    2022-12-06  8:10 PM        5761912    python311.dll

-a---    2022-12-06  8:10 PM         100216    pythonw.exe

-a---    2022-12-06  8:10 PM         49488    vcruntime140_1.dll

-a---    2022-12-06  8:10 PM         109392    vcruntime140_1.dll

PS S:\Dev\Learn_Python\samples> cp S:\Python\Python311\python3.dll .\build\primetest\

PS S:\Dev\Learn_Python\samples>

Once you have done all the steps above, you can run the program like all other apps on your computer, as shown here:

PS S:\Dev\Learn_Python\samples> .\build\primetest\primetest.exe

give me an integer that is greater than 1, and I will tell you if it is a prime: 31

31 is a prime

PS S:\Dev\Learn_Python\samples>

Errors in Programs

Errors in programs are hardly avoidable, and you should be prepared to see a lot of errors in your programs, especially at the beginning. The nice thing about programming within an IDE such as Visual Studio Code is that the IDE can point out many syntax errors and coach you to code along the way by showing you a list of allowable words or statements that you may want to use in that context, although you will have to decide which one to use for your program by highlighting the words that are problematic.

Syntax errors often include the following:

  1. 1. Missing, misspelled, or misplaced keywords such as for, while, if, elif, else, with, class, def, and so on. Remember that Python language is case-sensitive so for and For are totally different words to Python Virtual Machine (PVM).
  2. 2. Missing or misspelled operators such as >=, <=, +=, -=, *=, /=, and so on.
  3. 3. Missing symbols, such as a colon, comma, square or curly bracket, or parenthesis.
  4. 4. Mismatched parentheses, double quotation marks, single quotation marks, curly brackets, and square brackets.
  5. 5. Incorrect indentation because Python uses indentation to form code blocks.
  6. 6. Empty code blocks. If you do not know what to write in a code block, you can simply use a pass statement as a placeholder, as shown in the following examples.

    """

    This function will find the best student in a class based on their over performance, but at this time we don't know how.

    """

    def findBest():

     pass   # the pass statement is used to hold the place for a code block

The second type of errors you may encounter are runtime errors. While syntax errors may be easily avoided when programming within VS Code IDE, runtime errors can only be found during runtime. Examples of runtime errors include dividing a number with 0 and using a variable that has not been defined or that has no value before it is used. The discussion of error and exception handling, covered in Chapter 4, is mostly about runtime errors.

The following are some common runtime errors in Python programs:

  1. 1. ZeroDivisionError, when dividing something by 0.
  2. 2. TypeError, when an operation is performed on incompatible data types.
  3. 3. ValueError, when an incorrect value is used in a function call or expression.
  4. 4. NameError, when an identifier is used that has not been defined. In Python, define means that it has been assigned a value, even if the value is None.
  5. 5. IndexError, when the index used to access a sequence is out of boundary.
  6. 6. KeyError, when an incorrect key is used to access a dictionary value.
  7. 7. AttributeError, when an object attribute is sued that does not exist.
  8. 8. FileNotFoundError, when a file that one is trying to open to read does not exist. It is OK to open a file to write that does not exist. The operation will create a new file in that case.

In addition to syntax and runtime errors, you can also have logical errors in your programs. These errors can be found only by you or users of the program. An example of logical errors is when you are supposed to add two numbers, but you subtract one from the other instead.

Throughout the text, you will learn and gradually gain the ability to make your programs logically correct and to identify logical errors in your programs.

1.7 Essentials of Problem Solving and Software Development

Before you learn how to program in Python, you need to gain a basic understanding of the steps taken by computers to solve a problem and the steps taken by programmers to develop a software system for real-world application. The former, steps taken by computers to solve a problem, is called an algorithm. The latter, steps taken by programmers to develop an information system for real-world application, is in the area of system analysis and design.

Design Algorithms to Solve Problems

An algorithm is a sequence of instructions showing steps to solve a problem or get something done. The recipe for cooking a dish is an example of an algorithm from our everyday lives. Unlike algorithms for people to follow, algorithms for computers must show definitive steps of explicit operation.

Consider a very simple task for a computer to complete: give a computer two numbers and ask the computer to find the sum and print out the result. The algorithm can be described as follows:

  • [algorithm 1] Get two numbers from user, calculate, and print out the sum.
  • Step 1. Get the first number to n1.
  • Step 2. Get the second number to n2.
  • Step 3. Calculate the sum of n1 and n2, and store the result to s.
  • Step 4. Print out s.
  • [end of algorithm 1]

Describing the steps of operations as shown above is just one way to present algorithms. In computing and software development, algorithms can also be, and more often are, presented using pseudocode or a flowchart.

Pseudocode is a language that is easier for humans to understand but that is written like a computer program. There is no widely standard vocabulary and grammar for pseudocode. However, within an organization or even a class, the same pseudocode language should be used for collaboration and communication.

The above algorithm can be described in pseudocode as follows:

  • Start
  • Input from user → n1 # get an input from user and assign it to n1
  • Input from user → n2 # get another input from user and assign it to n2
  • n1 + n2 → s # n1 + n2 and assign the sum to s
  • print(s)
  • End

This simple algorithm can also be described using a flowchart, as in Figure 1-7.

In problem solving and computing, conditional operations and repetitive operations are often needed. An example is to calculate the sum of number 1, 2,…10000. One might think that the sum could be calculated by writing 1 + 2 + 3 + … + 10000, but that is wrong because that mathematical expression cannot be precisely understood by computers. The correct algorithm should be:

  • [algorithm 2] Calculate the sum of all positive integers that are no greater than 10000.
  • Step 1. 1 → i, 0 → s
  • Step 2. s + i → s, i +1 → i
  • Step 3. If i <= 10000 go to step 2 # loop back and make repetition under condition

A vertical chart with “start” and “stop” in ovals and with the input information in rectangular shapes with arrows between the words.

Figure 1-7: Flowchart of a simple algorithm

  • Step 4. Print s
  • [end of algorithm 2]

In pseudocode, the algorithm can be described as follows:

  • Start
  • Initialization: i = 1, s = 0
  • Repeat
  •    s = s + i
  •    i = i + 1
  • Until i > 10000
  • End

The algorithm can be depicted using a flowchart, as in Figure 1-8.

When programming to solve a problem, first develop an algorithm describing the steps of how the problem can be solved using computers, even if it is not explicitly and formally formulated as above.

Phases of Software System Development

Algorithm design and representation are essential when programming for computers to solve a problem or complete a task, but it is only part of software system development. Developing a software system to satisfy the requirements of a client requires several phases.

A vertical chart with “start” and “stop” in ovals, the input information in shapes between the words, and arrows between the shapes.

Figure 1-8: Flowchart of an algorithm

Phase 1. Understand the Project

When developing a software system at the request of a client or customer, the first thing to do is to understand what the client really needs so that you can get a clear definition of the problems to be solved and the tasks to be completed by the software system.

Phase 2. Analyze the Requirements to Identify Computer-Solvable Problems and Tasks

This phase is very important in turning people problems into computer problems, problems that can be solved by computers. Your clients and customers often have limited knowledge of what computers can do, and their requirements may be clear for humans, but not directly doable for computers. Requirement analysis will turn client’s requirements into problems and tasks suitable for computers to solve or complete. This often involves a strategy called divide and conquer, which means dividing big problems or tasks into smaller ones to solve. This strategy is widely used not only in computing but also in other disciplines and practices. Your understanding of the application domain and your knowledge of all aspects of computing are very important during this phase of software system development.

Phase 3. Design the System

System design involves the identification of modules and their connections. Module identification can be based on an understanding of the project and analysis of the requirements. The problems and tasks identified in phase 2 should be clustered into different modules of the system, though the same problem or task may exist in different modules. The next important thing at the system design phase is to design algorithms for each of the identified problems and tasks. Your knowledge and skill in system design, problem solving, and mathematical and logical thinking play important roles at this phase.

Phase 4. Implement the System

System implementation is programming, which turns the algorithms into computer programs. Your knowledge of the language chosen to implement the system is vital. Implementation involves programming to solve the problems and complete the tasks identified at phase 2, based on the structure of the system and algorithms designed at phase 3, as well as integration of all the subsystems and modules to make them work together.

Phase 5. Test the System

The system implemented at phase 4 may not correctly reflect what the client wants and may have errors and bugs. A thorough test is needed to find the bugs and close up the gaps between what has been implemented and what the client really wants.

Phase 6. Maintain the System

After the system has been fully tested and accepted by the client, the system will be delivered to the client. But that is not the end of the project. Maintaining the system is often an even bigger task after the release and delivery of the software because there is no guarantee that all bugs were found during the test, no guarantee that the implementation genuinely reflects the needs of the client, and no guarantee that the client will not come up with a “better” idea or requirements after the fact.

1.8 Manage Your Working Files for Software Development Projects

When working on a software development project, you need to deal with many files and make many changes to the files. Later, you might realize that some changes to a file are incorrect or do not improve the file, so you might want to go back and use a previous version of the file. It may not be a big issue if you are only dealing with one or just several files, but it will be very difficult when many files are involved in a project. That is why a version-control system is needed. Use a version-control system called Git to manage all your working files on the exercises and projects included in this text.

Originally developed by Linus Torvalds, Git is an open-source-distributed version-control system for tracking changes in any set of files, including Jupyter Notebook files with the extension ipynb.

In this section, you will learn

  1. 1. how to install and set up Git on your computer and use it to version-control your project files locally.
  2. 2. how to set up an account at GitHub and version-control your project files using Git and remote repositories on GitHub.
  3. 3. how to use Git and GitHub within VS Code to version-control your project files.

Set Up Git on Your Computer and Version-Control Locally

Git can be freely downloaded from https://git-scm.com/downloads and installed on your computer. After installing Git, you can create a local Git repository on your computer by running the git init command within a shell window such as CMD or PowerShell on Windows. A preferred one is Git-Bash shell, which comes with Git. It can be started within Windows File Explorer.

For example, if you want to start a project called Learn_Python, you can create a folder called Learn_Python on your desktop within Windows File Explorer, then right-click the folder icon, and choose from the pop-up menu. A Git-Bash window will open with Learn_Python as the current working directory, as shown here:

james@trustshop MINGW64 /s/Dev/Learn_Python

$

Within this Git-Bash shell window, you can run the following command to initialize:

$ git init

You have to run this Git command once to create a local repository for a project. It will create an empty Git repository on your local computer, which is a directory named .git with specific subdirectories and files that Git will use to version-control your project files. It will also create a master/main branch for the project. Other branches can be created if the project is divided into smaller ones to be worked on in parallel and then later merged.

Before running the command, you can set up an environment variable called $GIT_DIR to determine where the local repository will be placed on your computer. If $GIT_DIR is not set, the local Git repository—that is, the .git subdirectory—will be created right under the current working directory; if $GIT_DIR is set in a different location of your choosing, .git will be created right under $GIT_DIR. The common location of .git for a project is right under the project directory.

For the system to know who has made changes to the files and how to communicate with them, the following two commands need to run to config the Git that you installed on your computer:

$ git config -global user.name "Jon Doe"

$ git config -global user.email "Jon.Doe@gmail.com"

These commands will configure the user’s name and email address. Please keep in mind that this username is different from the username that you will use to sign up for your account with GitHub later in this section.

When changes have been made to a file and you want Git to manage the changes to the file, the first step is to stage the file by running the following command:

$ git add <file/files>

This is called “staging changes to the file,” which is the first step Git takes to version-control files.

For example, if you want to stage changes to all Python program files under the current project directory, run the following command:

$ git add *.py

To stage changes to all files and even subdirectories under the current working directory, run the following command:

$ git add .

The next step Git will take to version-control your project files is called commit. It is done by running the following command:

$ git commit -m "A brief message about this commitment"

Now changes to the files have been committed to the local repository. Please note that the stuff inside the quotation marks behind option -m are the comments or notes about the changes. If you run Git commit without option -m, a text editor will pop up to let you enter comments.

You can check the status of your Git system by running the following command:

$ git status

  • On branch master
  • No commits yet
  • Changes to be committed:
  • (use "git rm --cached <file>…" to unstage)
  •    new file: start.py

This shows that the file start.py has been staged but not committed yet. You can still use the git rm command to unstage the file (remove it from the staged list).

In the above, we basically described a process of making changes to a file, staging the files for the changes, and committing the changes to the repository. You may, and most likely want to, reverse the process to undo some changes.

To a file that has been staged but not committed, use the following command to unstage it:

$ git rm -cached <file>

or

$ git reset HEAD <file>

To a file that has not been staged, use the following command to discard the changes to the file:

$ git checkout -- file

To revert a commit that has been made, run the following command:

$ git revert HEAD

To restore a file to one that is n versions back in the master branch of the repository, run the following command:

$ git restore -source master~n <file>

There are some other Git commands available for various needs of version control, and each has many options providing powerful functionalities. A list of these commands can be found at https://git-scm.com/docs/git#_git_commands. You can also get the GitHub-Git cheat sheet at https://training.github.com/downloads/github-git-cheat-sheet/.

For Git, files that have been staged or committed for changes are called tracked, and those that have not been staged or committed are called untracked. In software development, some files, such as those objects and executable files derived from code files, do not need to be tracked for changes, so they should be ignored by Git. To tell Git what files under a project directory can be ignored, especially when running the command to stage all, you can add the file names or ignore patterns to a special file called .gitignore right under the project directory. You need to manage the list in the .gitignore file by editing the file using a text editor. Details of ignored file patterns can be found from Git documentation at https://git-scm.com/docs/gitignore. For our purposes, it may be sufficient to just describe the files based on what you already know, such as a file name or file names with a wildcard such as *.log.

Note that unlike other version-control systems, Git does not work with files for different versions, but works only with the changes that led to the current version of the file. For each file, Git will only keep one copy of the file for its current version in its repository and keep only the changes that led to the current version for all older versions.

Set Up an Account on GitHub and Version-Control with Remote Repositories

With Git, you can also use remote repositories on GitHub, which is a web-based system that you can use to create, access, and manage your remote repositories on a GitHub server. The benefits of working on a remote repository like GitHub for a software development project are twofold: the first is that you do not need to worry about the possible loss of your computer or the corruption of the file system; the second is that a team of programmers can work on a project at the same time efficiently and globally.

To use the remote repository on GitHub, first create an account with GitHub.com. GitHub offers students use of their repositories free of charge but also provides a pack of paid professional tools for software development.

Once you have signed into GitHub.com, you can create a repository for each of your projects under your account. Once created, each repository will have a unique URL where project files can be synchronized between the local repository and the remote repository, which can be cloned/downloaded. A repository on GitHub can be either public or private. A private repository can be accessed only by the owner, whereas a public repository can be accessed by everyone on the internet. Because you will be using the remote repository for your assignment projects, you should choose private so that your assignment work will not be shared with any others. You can create public repositories for projects that are substantially different from projects in the assignments and interesting enough for others to collaborate with you on the projects.

Now you know how to create both a local repository and a remote repository, and you’ve learned how to use Git commands to move project files to and from the local repository.

To continue work on a remote repository for the version-control of project files, you need to tell the computer where the remote repository is by using the following Git command:

$ git remote add <remote repository name> URL

Git supports several network protocols to communicate between your local computer and remote repository servers such as GitHub, including SSH and HTTPS. Because using SSH requires additional setup on your computer, we recommend HTTPS. The following is an example of adding the remote repository previously created for Jupyter Notebooks to our local notebook repository for Jupyter Notebooks:

$ git remote add notebooks https://github.com/jamesatau/comp218-notebooks.git

You can also use the following to delete a remote repository:

$ git remote remove <remote repository name>

And you can use

$ git remote rename <remote repository old name> <remote repository new name>

to rename a remote repository.

You can also make changes to the URL of an existing repository, as shown in the following example:

$ git remote set-url origin https://github.com/jamesatau/allgitcases.git

To view which remote repository is configured for the current project, use the following Git command:

$ git remote -v

The command below initializes a git repository and adds remote notebooks:

$ git init

Initialized empty Git repository in S:/Dev/Learn_Python/.git/

james@trustshop MINGW64 /s/Dev/Learn_Python (master)

$ git remote add notebooks https://github.com/jamesatau/comp218-notebooks.git

james@trustshop MINGW64 /s/Dev/Learn_Python (master)

The text below shows a git command to display the remote connections configured after running the commands above:

$ git remote -v

notebooks   https://github.com/jamesatau/comp218-notebooks.git (fetch)

notebooks   https://github.com/jamesatau/comp218-notebooks.git (posh)

james@trustshop MINGW64 /s/Dev/Learn_Python (master)

$

Between the local repository and the remote repository, project files can go in two directions. Moving files from the local to the remote repository is referred to as a push; moving files from the remote to the local repository is a pull or fetch. Between pull and fetch, pull is the default mode of moving files from remote to local, and fetch provides additional power, such as is needed when moving files from several repositories at the same time.

Please note that pull or push operations are not just transferring files. Key points of the operation are merging the changes to files in the target repository and versioning. Git has special commands for those key operations, but those operations often run behind the scenes without anyone noticing.

After you have added a remote repository to your project, you can push the already committed changes to the local repository by running the git push command in a shell terminal within the project directory, as shown here:

$ git push --set-upstream notebooks master

Enumerating objects: 3, done.

Counting objects: 100% (3/3), done.

Delta compression using up to 6 threads

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 545 bytes | 272.00 KiB/s, done.

Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

remote:

remote: Create a pull request for 'master' on GitHub by visiting:

remote:   https://github.com/jamesatau/comp21B-notebooks/pull/new/master

remote:

To https://github.com/jamesatau/comp218-notebooks.git

* [new branch] master -> master

branch 'master' set up to track 'notebooks/master'.

james@trustshop MINGW64 /s/Dev/Learn_Python (master)

$ git push notebooks

Everything up-to-date

james@trustshop MINGW64 /s/Dev/Learn_Python (master)

$

Sometimes you might be interested in a project in a repository and would like to make a local copy of the entire project. The operation of copying an entire project in a remote repository onto a local computer is called cloning. For example, say there is a repository called learn-python3 in GitHub containing sample code, and you want to download all the code to play with it. You can do that by doing the following:

  1. 1. Open a terminal on a file folder in which the project is to be placed—say, the desktop.
  2. 2. From GitHub.com, copy the HTTPS URL from the “Code” dropdown menu of the learn-python3 repository and run the following Git command:

    $ git clone https://github.com/michaelliao/learn-python3.git

    You will then have all the code samples on your computer.

To work with Git and GitHub, you can use Git GUI, a GUI-based system, by invoking Git GUI on the project folder within Windows File Explorer.

Another way, our preferred way to use Git and GitHub for versioning on both local and remote repositories, is to operate within VS Code, which has native support for Git and GitHub, as long as Git is properly installed on the local computer, as shown earlier in this section.

  1. 1. Create a new folder on your local drive for a project.
  2. 2. Open the folder from VS Code. The new folder can also be created when you open the file folder in VS Code.
  3. 3. Initialize the local repository for the project by running git initialize in VS Code command palette by pressing Ctrl+Shift+P. Type “git” and click “git initialize.” You will be asked to do the following:
    1. a. Choose a local workspace folder to initialize.
    2. b. Set the email address of the programmer/developer/author.
    3. c. Set the name of the programmer/developer/author.

    Now the local repository is ready for versioning your project files locally.

  4. 4. Create or copy project files under the project folder.
  5. 5. Click the source control icon on the left navigation bar, type a brief message about the changes to the files or project, and press Ctrl+Enter or click the commit icon above.
  6. 6. To push the changes to the project files and the project to the remote repository in GitHub, click the Git Sync icon at the bottom-left of the VS Code window.
    1. a. If this is the first time you are pushing the project to your GitHub account, the command palette dialogue will pop up and ask you to choose whether to publish the project into a public repository or private repository under your account. The project folder name will be used as the repository name by default, but you can use a different name.
    2. b. You may be asked to sign into your GitHub account if you have not accessed GitHub from VS Code before.
    3. c. You may also be asked to either use the existing username and email associated with your GitHub account or provide a different email and/or username to identify yourself as the author of the project being pushed.

If you have already created a repository for the project on GitHub, you can copy the URL of the repository and add the remote repository to the project in VS Code by running git add remote in the command palette. You will also be asked to provide a name for the remote repository after entering the URL.

Chapter Summary

  • • The first chapter has introduced you to computers and programming languages and guided you in setting up the programming environment.
  • • The introduction to modern computers included a brief history as well as a description of the fundamental architecture of modern computers. Knowing who made significant contributions to the development of computers, what contributions they made, and how they made those contributions can be very inspiring.
  • • The computers we use today are called Von Neumann machines because they are based on Von Neumann architecture, which consists of a control unit, an arithmetic logic unit (ALU), registers, and a memory unit (collectively called CPU) plus inputs and outputs. That’s why Von Neumann is often credited as one of the fathers of modern computers.
  • • The key features of modern computers are that they are digital, programmable, and automatic, with stored programs, although these features were already in the design of the analytical engine by British mathematician Charles Baggage in the 1800s. Hence Charles Babbage is also credited as a father of modern computers, although his machines were not electronic.
  • • Binary or Boolean algebra is one of the important theories behind today’s modern computers. It can be proved that computing with a ternary number system would be more efficient, but it would be more costly to make computing components to build computing machines based on a ternary number system than on a binary number system.
  • • Signed numbers, including both integers and real numbers, need to and can be represented by a sequence of digits, using the highest bit to represent the sign (0 for positive, 1 for negative). A certain number of digits are assigned for decimals.
  • • To apply arithmetic operations more easily and efficiently on computers, 2’s complements are used to represent signed numbers. With 2’s complements, the addition of signed numbers can be easily done.
  • • How do you know what modern computers are and are not capable of? Alan Turing, independently from Alonzo Church, laid the foundational work on computability with his Turing machine. Because of that, Alan Turing is credited as a father of modern computing.
  • • There is also the question of how difficult it is to solve a problem with computers, which is the study of computational complexity. The computational complexity of a problem is often measured in terms of the total number of basic computations, such as addition and multiplication, which can be converted into the time needed to solve the problem on specific computers. The space required to solve a problem can be a concern as well, but most of the time, when people are talking about computational complexity, they are talking about the steps or time required to solve the problem.
  • • Problems to be solved on computers are often divided into three classes of problems: P, NP-complete, and NP, in which P is short for polynomial and NP is short for nondeterministic polynomial.
  • • If, on a deterministic and sequential machine like a computer, for a problem of size n, if the time or number of steps needed to find the solution is a polynomial function of n, the problem is said to be in the P class. If a problem can be solved in polynomial time on a nondeterministic machine, the problem is in the NP class. A problem is said to be NP-complete if proposed answers can be verified in polynomial time, and if an algorithm can solve it in polynomial time, the algorithm can also be used to solve all other NP problems.
  • • Programs are the computers’ soul. The task of writing programs for computers is called programming. Languages in which programs can be written for computers are programming languages.
  • • Programming languages play important roles in building soul into computers. Programming languages can be machine languages, assembly languages, and high-level languages.
  • • Ada Lovelace, who wrote code for Charles Babbage’s analytical engine, was credited as the first programmer of modern computers. The Ada programming language was named in her honour.
  • • An algorithm describes the steps a computer needs to take to solve a problem or complete a task.
  • • Pseudocode and flowcharts are two ways of describing algorithms.
  • • System analysis and design are the steps taken to design and develop an information system for real-world application.
  • • The official website for the Python programming language is at www.python.org. Use the Anaconda package to install Python, Jupyter Notebook, Visual Studio Code IDE, and other tools for your study of programming with Python.
  • • Python interactive shell and Jupyter Notebook are recommended for learning Python programming interactively.
  • • Create a Jupyter Notebook for each chapter and/or section, and work through all the examples within that part of the course in that notebook to keep a record of your learning activities for review and evaluation.
  • • Visual Studio Code (VS Code) is the IDE recommended for you to complete the projects and programming projects in the assignments.

Exercises

  1. 1. Convert the following numbers in their respective bases into their binary equivalence:

(78)10

(1F)16

(27)8

(121)10

(3E)16

(33)8

(29)10

(CD)16

(52)8

  1. 2. Complete the following binary operations:

10111 + 1101

1101010 − 101101

10101 + 1110

10101 − 1111

1101 * 101

111011 / 11

  1. 3. Complete a thorough review of computer history by reading library books and online articles. Compile a table showing the significant developments of theories and technologies of modern computers in Europe and a similar table for America. For each development, the table should show the time, a description of the development, the key players/contributors, and its impact on the later development of modern computers.
  2. 4. Investigate the classification and development of computer programming languages and explain the features of the following programming languages:
    1. a. Machine code
    2. b. Assembly languages
    3. c. High-level programming languages
    4. d. Procedural programming languages
    5. e. Structured programming languages
    6. f. Imperative programming languages
    7. g. Functional programming languages
    8. h. Logic programming languages
    9. i. Object-oriented programming languages
  3. 5. Create a new folder called comp218, open it with VS Code, then create a new file and rename it comp218start.ipynb, which is recognized by VS Code as Jupyter Notebook. Start working in the cells and use it as a calculator to see what expressions and statements you can perform correctly with Python, based on what you have learned so far. Please note that you may wish to choose a Python interpreter in order to run the code or make sure the notebook is working properly.
  4. 6. In a cell of a newly created notebook comp218start.ipynb, type the following code and then press Shift+Enter to run it to see what you will get:

In [ ]:

first_name = 'John'

last_name = 'Doe'

full_name = first_name + ' ' +last_name

print(full_name)

  1. 7. In a new cell of notebook comp218start.ipynb, type the following code and then press Shift+Enter to run the code to see what you will get.

In [ ]:

for i in range(11):

 p = (2**i)

  print(f'{bin(p)}')

  1. 8. Write an algorithm in pseudocode describing the steps to make a pizza.
  2. 9. Write an algorithm in pseudocode describing the steps to cook rice.
  3. 10. Write an algorithm in a flowchart describing the steps you usually take in the morning, from getting up until leaving home to go to work or school.

Projects

  1. 1. Research the history of computers online, then write a summary about each of the following:
    1. a. The computing machines designed and developed in history that are significant to the development of today’s computers
    2. b. The people in history who have made significant contributions to the development of computers
    3. c. The concepts, theories, designs, and technologies in history that are important to the development of modern computers
  2. 2. Research the history of programming languages online, then write a summary for each of the following:
    1. a. Machine languages that have been important to the development of computer systems
    2. b. The differences between machine languages, assembly languages, and high-level programming languages
    3. c. The essence (what defines it and differentiates it from others) of structural programming, the pros and cons of structural programming, and some well-known programming languages that support structural programming
    4. d. The essence of imperative programming, the pros and cons of imperative programming, and some well-known programming languages that support imperative programming
    5. e. The essence of declarative programming, the pros and cons of declarative programming, and some well-known programming languages that support declarative programming
    6. f. The essence of functional programming, the pros and cons of functional programming, and some well-known programming languages that support functional programming
    7. g. The essence of logical programming, the pros and cons of logical programming, and some well-known programming languages that support logical programming
    8. h. The essence of object-oriented programming, the pros and cons of object-oriented programming, and some well-known programming languages that support object-oriented programming

Annotate

Next Chapter
Chapter 2. Essential Building Blocks of Computer Programs
PreviousNext
Powered by Manifold Scholarship. Learn more at
Opens in new tab or windowmanifoldapp.org