This course will take place online on zoom. Please reference these notes to follow the lessons! (Cred to Sunny Lu, Jessica Ness, and Hyemin Lee)

Getting Started

Why Programming?

Programming is defined as the construction of a series of instructions that a computer can follow. In an ever-adapting world, programming has been shown to be more and more significant. Computers, which are usually made of millions of transistors, are able to execute the instructions that we give them, otherwise known as code, very efficiently.

Computers have the advantage of being fast and accurate. While a human might take roughly $0.1$ seconds of response time even trying to calculate $1+1=2$, a computer can readily compute very large numbers like $4763878 \times 3476893 = 16563494071054$ . In fact, it doesn’t even take the blink of an eye to calculate the same thing $1000$ times in a row. Every computer has a specification that states how fast it can execute each instruction. My computer, which is a typical computer, is a $2.3$ GHz ( pronounced giga-hertz ) computer. That means that it can execute $2.3 \times 10^9$ ( $2.3$ billion ) instructions every second. That is why I couldn’t even conceivably experimentally measure the time the computer took to evaluate the product of two numbers: the time is effectively $0$. Additionally, while one might tell you that everybody makes mistakes, that’s not really the case for computers: a well-engineered computer can run without malfunction for more than $50$ years! Not only does the computer seldom feel fatigued, but it also always guarantee accurate results.

In fact, using time, as you will later learn in this course, you can time the run, which takes 0.01 seconds. However, doing a simple second test will tell you that simply printing the output takes 0.01 seconds.

But a computer isn’t just a glorified calculator. Specifically, computers are also able to efficiently memorize data. For install, my computer memory is $16$ GB ( pronounced giga-bytes ). A byte is somewhat similar to a letter. This means that we can store the entire works of Shakespeare several thousand times before we run out of memory. Of course, unlike a human’s memory, a computer doesn’t misremember.

Programming can be a unique asset to our lives since it can aid us in doing repetitive tasks. As an example, when I was first studying electrical engineering, I was working on a project involving the making of logic gates using MOS FETs. Our ultimate goal was to create an arithmetic and logic unit, but we had to label many wires with tags so we and the program knew what the wires were. However, frequently we would encounter a set of multiple wires representing a single number, say $A$. We had to label the wires $A_1, A_2, A_3, \cdots,$ and the task was very excruciating. Eventually I settled on creating an automated set of code, or a script, to accomplish this for me and my teammates. A task that originally took half an hour was simplified into one that took $5$ minutes, hands free.

Programming can also help us analyze patterns. For instance, I have built a script that can track my mouse movements. A computer can handle large batches of data and analyze them very efficiently. This can let us visualize the data behind it. Here’s a sample:

[Image]

Learning how to program can enhance your critical thinking and problem solving skills. Finding a solution for a problem is hard, and turning the solution into code is even more so. Turning your ideas into code is the closest you can get to an instruction that cannot be misinterpreted. Some people dream of the time in which machines can understand a common human language without any form of mistake. Well, that human language is called code. Learning programming offers a wide range of job opportunities. Most major tech companies nowadays requires some skill with programming.

So, without further ado, let’s get started!

Quiz

  1. In what aspects are computers better than humans? Select all that apply.
    • [x] Efficiency while Completing Tasks
    • [x] Accuracy and Consistency
    • [x] Memorizing and Recalling Information
    • [x] Gathering and Analyzing Data
  2. Which of the following is the problem least suited for programming? Select one.
    • [ ] Logging into a website to check whether all students have been properly vaccinated
    • [ ] Analyzing whether a person has mobility-related diseases by recording keyboard presses
    • [x] Giving personalized advice for people under a wide range of different circumstances
    • [ ] Generating and sending via E-mail completion certificates for an online course
  3. Which of the following statements are not correct? Select all that apply.
    • [x] Programming is the process of typing out any document.
    • [ ] Code is a set of instructions we give to computers to execute.
    • [x] A byte is approximately the amount of space needed to store a word.
    • [ ] Code is a way for humans to interact with computers.

 

Python: A Brief History

Python is a programming language. A programming language is the method that the user chooses to communicate to your machine. For instance, C++ is another programming language. However, as you’ll eventually see, if you tell a C++ compiler, which only understands C++, to execute Python, it will try and most times fail. There are multiple versions, or releases, of Python. Each “major version” is substantially different. Van Rossum, the creator of Python, first uploaded Python $0.9.0$ to alt.sources in $1991$. Here, $0$ is the “major version number”, which symbolizes the first major change (i.e. the creation) of the language. $9.0$ is the release number. Typically, to refer to Python $0.9.0$, one can also say Python $0$ or Python $0.9$. In $1994$, Van Rossum updated Python to $1.0$, introducing lambda expressions, map, filter, reduce, and many other components that we will later learn. Then, in the year $2000$, Python $2$ was introduced, with list comprehensions and garbage collection. Garbage collection is very important, since it can largely reduce the working memory your computer takes up.

In $2008$, Python $3$, the newest major version, was developed, fixing fundamental design flaws. However, as a side effect, it meant that it was no longer backwards-compatible: although Python $0$ programs can still work in Python $2$ with few exceptions, almost none of the programs in Python $2$ still work in Python $3$. The change was very fundamental, including the most basic elements such as input and output and the evaluation of strings. Although many people were satisfied with Python $2$, eventually the decision was reached in $2020$ that Python $2$ would no longer be actively supported. That is, any bugs, even security vulnerabilities, will not be fixed.

As of April 5th, 2024, the newest version of Python is Python $3.12$. Updates include but are not limited to:

  • More user-friendly error messages, specifically ones that try to point out a possible typo the user had made.
  • Significant performance improvements in multi-thread computing, which mean branching a calculation into multiple parallel branches that executes simultaneously.
  • Arbitrarily nested f-strings, which you will later learn.

Here are some characteristics of Python programming:

  • Python is high-level. This means that, comparatively, Python is less specific about the exact implementation details. This means that you can give Python less detailed code and Python will still be able to take care of executing it. Although high-level programming languages are usually more easy to learn and beginner-friendly, they can often prove to be sub-optimal in implementation details, leading to slower runtime.
  • Python is general-purpose. In general, Python is intended to be the Jack of all trades. Python is not intended to be focused on one particular field, unlike JavaScript, which is mainly focused on web development, and Processing, which is usually used for animations and interactives. However, Python is known for its widespread use in the machine learning community, and is often used to analyze data.
  • Python is whitespace-significant. The following two Python programs do two different things:
  • [Image]
  • Python is dynamically-typed. Just like in human conversations, the same word, such as “this” or “it” can stand for many different things. They are called variables. In languages such as C that are statically-typed, an integer variable will always be an integer. In some more extreme cases, such as brainfuck, there are no “variable types”. However, in Python, the variable can be overwritten at any time and assigned to anything. You will learn more about typing, that is, the type of variables, once you finish your lesson on Variables and their Types.

Aren’t you interested to learn how this works?

Quiz

  1. What is the newest version of Python? Select one.
    • [ ] Python $2$
    • [x] Python $3$
    • [ ] Python $4$
  2. Which of the following is not a characteristic of Python? Select all that apply.
    • [ ] High-level.
    • [ ] General-purpose.
    • [ ] Whitespace-significant.
    • [x] Statically-typed.
 

Setting up and testing your Python Environment

No text content. Supply video instead.

Quiz

  1. What is the difference between an IDE and the IDLE?

    An IDE is usually an external software that can help color your Python code such that it is more readable. It can help you write programs by offering suggestions and shortcuts. Although programming without an IDE is certainly possible, programming with an IDE is much more pleasant. The IDLE specifically refers to the built in Python IDE. ( The extra L stands for learning. )

  2. What are the positives and negatives of using PyCharm?

    Pros:

    • PyCharm offers a comprehensive set of tools for Python development, including code completion, debugging, refactoring, and version control integration.
    • PyCharm provides code completion and error highlighting, significantly boosting productivity by catching errors early and suggesting code improvements.
    • PyCharm supports a wide range of plugins that extend its functionality, which allows users to customize the IDE to suit their specific needs.

    Cons:

    • PyCharm can be resource-intensive, since it requires significant memory and processing power, which could lead to slower performance on older hardware.
    • PyCharm (Community Edition) does not contain some advanced features are only available in the paid version (Professional Edition).
    • PyCharm can have occasional performance issues, such as slow startup times or laggy behavior, especially when working with very large projects.
  3. What does print("Hello, World! ") output?

    It outputs the string “Hello, World! ”, with spaces and without quotes, exactly as given.

  4. Which of the following can not be a reason why print("Hello, World! ") produces no output? Select one.

    • [ ] The parenthesis () was entered as a bracket [] .
    • [ ] A different quotation symbol “” was used instead of "" .
    • [x] "Goodbye, World! " was entered instead of "Hello, World! " .
    • [ ] The Python installer was not installed properly.
    • [ ] A full-width parenthesis () was entered instead of () .
    • [ ] output was entered instead of print.

The Python REPL

The Python REPL performs the actions Read, Evaluate, Print, and Loop. That is, it reads every line individually, evaluates the statement, prints the output if there is one, and repeat. For instance, the image below shows the Python REPL calculating 1//2 , -1//2 , and 2-1//2 , and outputting the results 0 , -1 , and 2 .

Variables

Introduction to Variables

In Python, variables are containers that can store data. Therefore, before we can understand variables, we’ll first have to talk about data. Data is the lifeblood of our modern world, relating to nearly every aspect of society, from business and healthcare to entertainment and education. Its importance cannot be overstated, as it fuels decision-making, innovation, and progress across various domains. Our world is filled with data. Everything is digitalized.

The data can be as simple as a number ( like $42$ or $\pi$ ) or a piece of text ( like the Hello, World! that you saw earlier ), but it can also be a set of numbers ( like $\{-5, 0, 3, 5, 7\}$ ), an array ( like $[-2,3,2,0,-2,-2,0,5]$ ), dogs, lists of dogs, the list of all corresponding dog owners and their household names, or generally any object you can think of. Each piece of data is associated with a type. For instance, $42$ is an integer, and $\pi$ is a decimal… normally. Although it is unconventional to do so, it is also possible to represent $42$ as a decimal ( $42.0$ ), a piece of text ( 42 ), or even more bizarre types. Although we intuitively think of $42$, $42.0$, and 42 as the same thing, representing the data “$42$”, computers, and hence programming language, often separate them. Therefore, although the three pieces of data have the same value, they have different types. It is important that their are viewed as very different data, similar to how $42$ people and $42$ meters do not mean the same thing.

Imagine that you say x = 0 in Python. That means that you “assign” the variable x to 0. Notice the difference between x = 0 in Python and the statement $x=0$ in mathematics. The common misconception is that the two are the same. However, the = sign in Python means assignment. It does not state that x is equal to 0; it simply tells x to become equal to 0, that’s it. It’s analogous to assigning a worker to work. If you assign Alice to do the cooking, then that just means that Alice will cook unless you tell him otherwise. But once you assign Alice to do the accounting instead, Alice is no longer cooking. Similarly, x = 0 does not mean that x must always be equal to 0. If x = 1 is executed next, then x will cease being 0 and instead become 1. However, $x=0$ means that $x$, no matter what, must always be $0$. If you say $x=0$, and then you say $x=1$, then $x$ doesn’t “become” $1$; instead, you have shown a mistake or you have proven a contradiction.

We typically represent variables as arrows that tell you where to look for something. x = 0 tells Python to link x to 0 like so:

The variable  is shown with an arrow pointing to .

Then, if we execute y = 0 , then Python will link y to 0 like so:

The variables  and  are shown each with an arrow pointing to the same .

Then, if we execute x = 1 , then Python will link x to 1 like so:

The variables  and  are shown each with an arrow pointing to  and , respectively.

Consider what happens when we execute y = x . There are two likely guesses that you may have:

The variables  and  are shown both with an arrow pointing to

y will be assigned to 1, the value of x.

The variables  and  are shown each with an arrow pointing to  and , respectively.

y will be assigned to x directly.

Both seams reasonable. However, the way Python executes a statement like y = x is that it first evaluates the right hand side, yielding 1, and then assigns the variable to the data. In this case, x is evaluated to its assigned data, 1 , which is why y is assigned to 1 and not x . Remember this visual. This will be the key to understanding the chapters that follow it.

 

The Integer Type

The integer type is the easiest to understand in Python. An integer is just like what you would expect in math. $3$ is an integer. $-429$ is an integer. Even $64878468761784677683$ is an integer. As with other data, we can do operations on them. Some of these should be familiar to many of you. For instance, there are addition ( + ), subtraction ( - ), multiplication ( * ), and exponentiation ( ** ). Note that exponentiation is not ^, as commonly used in worlds of mathematics ( for instance, in $\LaTeX$, exponentiation is “^”.) The operations on integers follow standard rules: exponentiation first, then multiplication, then addition and subtraction. For instance, 13+2^5*6-1 evaluates to $13+2^5\times6-1=204$.

Most of you might be satisfied. However, a small percentage of you will have realized by now that division is not on the list. The problem is that there are actually two types of division: floating point division and integer division. We’ll worry about the former later ( once we introduce real numbers ). Integer division // is seemingly simple: divide, then round down. Specially, negation comes before integer division, but subtraction comes after integer division. In general, any unary operation or monad – operations performed on one number – comes before any binary operation or dyad – operations performed on two numbers.

 

The Float Type

I take it back. The float type is the easiest to understand in Python. A float is just like what you would expect of a decimal in math. $3.14159$ is a float. $-429.0$ is a float. Even $64878468761784677683.8732872387234782348723$ is a float. As with other data, we can do operations on them. Some of these should be familiar to many of you. For instance, there are addition ( + ), subtraction ( - ), multiplication ( * ), division ( / ), and exponentiation ( ** ).

For instance, 1.3+1.5/6.0 evaluates to $1.3+1.5\div6.0=1.55$.