Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Notes/00_Setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ exercises. Feel free to look at this if you need a hint. To get the
most out of the course however, you should try to create your own
solutions first.

[Contents](Contents)
[Contents](Contents.md)



Expand Down
18 changes: 9 additions & 9 deletions Notes/01_Introduction/00_Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
The goal of this first section is to introduce some Python basics from
the ground up. Starting with nothing, you'll learn how to edit, run,
and debug small programs. Ultimately, you'll write a short script that
reads a CSV data file and performs a simple calculation.
reads a CSV data file and performs a simple calculation.

* [1.1 Introducing Python](01_Python)
* [1.2 A First Program](02_Hello_world)
* [1.3 Numbers](03_Numbers)
* [1.4 Strings](04_Strings)
* [1.5 Lists](05_Lists)
* [1.6 Files](06_Files)
* [1.7 Functions](07_Functions)
* [1.1 Introducing Python](01_Python.md)
* [1.2 A First Program](02_Hello_world.md)
* [1.3 Numbers](03_Numbers.md)
* [1.4 Strings](04_Strings.md)
* [1.5 Lists](05_Lists.md)
* [1.6 Files](06_Files.md)
* [1.7 Functions](07_Functions.md)

[Contents](../Contents)
[Contents](../Contents.md)

10 changes: 5 additions & 5 deletions Notes/01_Introduction/01_Python.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[Contents](../Contents) \| [Next (1.2 A First Program)](02_Hello_world)
[Contents](../Contents.md) \| [Next (1.2 A First Program)](02_Hello_world.md)

# 1.1 Python

### What is Python?

Python is an interpreted high level programming language. It is often classified as a
["scripting language"](https://en.wikipedia.org/wiki/Scripting_language) and
["scripting language"](https://en.wikipedia.org/wiki/Scripting_language) and
is considered similar to languages such as Perl, Tcl, or Ruby. The syntax
of Python is loosely inspired by elements of C programming.

Expand Down Expand Up @@ -40,12 +40,12 @@ able to type `python` like this:

```
bash $ python
Python 3.8.1 (default, Feb 20 2020, 09:29:22)
Python 3.8.1 (default, Feb 20 2020, 09:29:22)
[Clang 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>>
>>>
```

If you are new to using the shell or a terminal, you should probably
Expand Down Expand Up @@ -187,5 +187,5 @@ exercise work. For example:
If you can't make this work, don't worry about it. The rest of this course
has nothing to do with parsing XML.

[Contents](../Contents) \| [Next (1.2 A First Program)](02_Hello_world)
[Contents](../Contents.md) \| [Next (1.2 A First Program)](02_Hello_world.md)

16 changes: 8 additions & 8 deletions Notes/01_Introduction/02_Hello_world.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Contents](../Contents) \| [Previous (1.1 Python)](01_Python) \| [Next (1.3 Numbers)](03_Numbers)
[Contents](../Contents.md) \| [Previous (1.1 Python)](01_Python.md) \| [Next (1.3 Numbers)](03_Numbers.md)

# 1.2 A First Program

Expand Down Expand Up @@ -51,7 +51,7 @@ hello world
This so-called *read-eval-print-loop* (or REPL) is very useful for debugging and exploration.

**STOP**: If you can't figure out how to interact with Python, stop what you're doing
and figure out how to do it. If you're using an IDE, it might be hidden behind a
and figure out how to do it. If you're using an IDE, it might be hidden behind a
menu option or other window. Many parts of this course assume that you can
interact with the interpreter.

Expand All @@ -63,7 +63,7 @@ Let's take a closer look at the elements of the REPL:
The `...` prompt may or may not be shown depending on your environment. For this course,
it is shown as blanks to make it easier to cut/paste code samples.

The underscore `_` holds the last result.
The underscore `_` holds the last result.

```python
>>> 37 * 42
Expand Down Expand Up @@ -109,7 +109,7 @@ C:\SomeFolder>c:\python36\python hello.py
hello world
```

Note: On Windows, you may need to specify a full path to the Python interpreter such as `c:\python36\python`.
Note: On Windows, you may need to specify a full path to the Python interpreter such as `c:\python36\python`.
However, if Python is installed in its usual way, you might be able to just type the name of the program
such as `hello.py`.

Expand Down Expand Up @@ -265,7 +265,7 @@ Indentation groups the following statements together as the operations that repe
num_bills = num_bills * 2
```

Because the `print()` statement at the end is not indented, it
Because the `print()` statement at the end is not indented, it
does not belong to the loop. The empty line is just for
readability. It does not affect the execution.

Expand All @@ -275,7 +275,7 @@ readability. It does not affect the execution.
* Use 4 spaces per level.
* Use a Python-aware editor.

Python's only requirement is that indentation within the same block
Python's only requirement is that indentation within the same block
be consistent. For example, this is an error:

```python
Expand Down Expand Up @@ -464,12 +464,12 @@ NameError: name 'days' is not defined
Reading error messages is an important part of Python code. If your program
crashes, the very last line of the traceback message is the actual reason why the
the program crashed. Above that, you should see a fragment of source code and then
an identifying filename and line number.
an identifying filename and line number.

* Which line is the error?
* What is the error?
* Fix the error
* Run the program successfully


[Contents](../Contents) \| [Previous (1.1 Python)](01_Python) \| [Next (1.3 Numbers)](03_Numbers)
[Contents](../Contents.md) \| [Previous (1.1 Python)](01_Python.md) \| [Next (1.3 Numbers)](03_Numbers.md)
12 changes: 6 additions & 6 deletions Notes/01_Introduction/03_Numbers.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Contents](../Contents) \| [Previous (1.2 A First Program)](02_Hello_world) \| [Next (1.4 Strings)](04_Strings)
[Contents](../Contents.md) \| [Previous (1.2 A First Program)](02_Hello_world.md) \| [Next (1.4 Strings)](04_Strings.md)

# 1.3 Numbers
# 1.3 Numbers

This section discusses mathematical calculations.

Expand Down Expand Up @@ -55,7 +55,7 @@ x / y Divide (produces a float)
x // y Floor Divide (produces an integer)
x % y Modulo (remainder)
x ** y Power
x << n Bit shift left
x << n Bit shift left
x >> n Bit shift right
x & y Bit-wise AND
x | y Bit-wise OR
Expand Down Expand Up @@ -170,7 +170,7 @@ Try it out.
## Exercises

Reminder: These exercises assume you are working in the `practical-python/Work` directory. Look
for the file `mortgage.py`.
for the file `mortgage.py`.

### Exercise 1.7: Dave's mortgage

Expand Down Expand Up @@ -224,7 +224,7 @@ How much will Dave pay if he pays an extra $1000/month for 4 years starting in y

### Exercise 1.10: Making a table

Modify the program to print out a table showing the month, total paid so far, and the remaining principal.
Modify the program to print out a table showing the month, total paid so far, and the remaining principal.
The output should look something like this:

```bash
Expand Down Expand Up @@ -264,4 +264,4 @@ True
>>>
```

[Contents](../Contents) \| [Previous (1.2 A First Program)](02_Hello_world) \| [Next (1.4 Strings)](04_Strings)
[Contents](../Contents.md) \| [Previous (1.2 A First Program)](02_Hello_world.md) \| [Next (1.4 Strings)](04_Strings.md)
12 changes: 6 additions & 6 deletions Notes/01_Introduction/04_Strings.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Contents](../Contents) \| [Previous (1.3 Numbers)](03_Numbers) \| [Next (1.5 Lists)](05_Lists)
[Contents](../Contents.md) \| [Previous (1.3 Numbers)](03_Numbers.md) \| [Next (1.5 Lists)](05_Lists.md)

# 1.4 Strings

Expand Down Expand Up @@ -299,7 +299,7 @@ Verify this by trying to change the first character of `symbols` to a lower-case
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>>
>>>
```

### Exercise 1.14: String concatenation
Expand Down Expand Up @@ -408,18 +408,18 @@ To do that, use an f-string. For example:
>>> price = 91.1
>>> f'{shares} shares of {name} at ${price:0.2f}'
'100 shares of IBM at $91.10'
>>>
>>>
```

Modify the `mortgage.py` program from [Exercise 1.10](03_Numbers) to create its output using f-strings.
Modify the `mortgage.py` program from [Exercise 1.10](03_Numbers.md) to create its output using f-strings.
Try to make it so that output is nicely aligned.


### Exercise 1.18: Regular Expressions

One limitation of the basic string operations is that they don't
support any kind of advanced pattern matching. For that, you
need to turn to Python's `re` module and regular expressions.
need to turn to Python's `re` module and regular expressions.
Regular expression handling is a big topic, but here is a short
example:

Expand Down Expand Up @@ -485,4 +485,4 @@ upper(...)
>>>
```

[Contents](../Contents) \| [Previous (1.3 Numbers)](03_Numbers) \| [Next (1.5 Lists)](05_Lists)
[Contents](../Contents.md) \| [Previous (1.3 Numbers)](03_Numbers.md) \| [Next (1.5 Lists)](05_Lists.md)
8 changes: 4 additions & 4 deletions Notes/01_Introduction/05_Lists.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Contents](../Contents) \| [Previous (1.4 Strings)](04_Strings) \| [Next (1.6 Files)](06_Files)
[Contents](../Contents.md) \| [Previous (1.4 Strings)](04_Strings.md) \| [Next (1.6 Files)](06_Files.md)

# 1.5 Lists

Expand Down Expand Up @@ -98,7 +98,7 @@ for name in names:

This is similar to a `foreach` statement from other programming languages.

To find the position of something quickly, use `index()`.
To find the position of something quickly, use `index()`.

```python
names = ['Elwood','Jake','Curtis']
Expand Down Expand Up @@ -267,7 +267,7 @@ Use the `append()` method to add the symbol `'RHT'` to end of `symlist`.
>>> # append 'RHT'
>>> symlist
['HPQ', 'AAPL', 'AIG', 'MSFT', 'YHOO', 'GOOG', 'RHT']
>>>
>>>
```

Use the `insert()` method to insert the symbol `'AA'` as the second item in the list.
Expand Down Expand Up @@ -411,4 +411,4 @@ example, a list that consists entirely of numbers or a list of text
strings. Mixing different kinds of data together in the same list is
often a good way to make your head explode so it's best avoided.

[Contents](../Contents) \| [Previous (1.4 Strings)](04_Strings) \| [Next (1.6 Files)](06_Files)
[Contents](../Contents.md) \| [Previous (1.4 Strings)](04_Strings.md) \| [Next (1.6 Files)](06_Files.md)
12 changes: 6 additions & 6 deletions Notes/01_Introduction/06_Files.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[Contents](../Contents) \| [Previous (1.5 Lists)](05_Lists) \| [Next (1.7 Functions)](07_Functions)
[Contents](../Contents.md) \| [Previous (1.5 Lists)](05_Lists.md) \| [Next (1.7 Functions)](07_Functions.md)

# 1.6 File Management

Most programs need to read input from somewhere. This section discusses file access.
Most programs need to read input from somewhere. This section discusses file access.

### File Input and Output

Expand Down Expand Up @@ -63,7 +63,7 @@ Read a file line-by-line by iterating.
```python
with open(filename, 'rt') as file:
for line in file:
# Process the line
# Process the line
```

### Common Idioms for Writing to a File
Expand Down Expand Up @@ -168,7 +168,7 @@ of column headers).
>>>
```

`next()` returns the next line of text in the file. If you were to call it repeatedly, you would get successive lines.
`next()` returns the next line of text in the file. If you were to call it repeatedly, you would get successive lines.
However, just so you know, the `for` loop already uses `next()` to obtain its data.
Thus, you normally wouldn’t call it directly unless you’re trying to explicitly skip or read a single line as shown.

Expand Down Expand Up @@ -234,12 +234,12 @@ Data scientists are quick to point out that libraries like
[Pandas](https://pandas.pydata.org) already have a function for
reading CSV files. This is true--and it works pretty well.
However, this is not a course on learning Pandas. Reading files
is a more general problem than the specifics of CSV files.
is a more general problem than the specifics of CSV files.
The main reason we're working with a CSV file is that it's a
familiar format to most coders and it's relatively easy to work with
directly--illustrating many Python features in the process.
So, by all means use Pandas when you go back to work. For the
rest of this course however, we're going to stick with standard
Python functionality.

[Contents](../Contents) \| [Previous (1.5 Lists)](05_Lists) \| [Next (1.7 Functions)](07_Functions)
[Contents](../Contents.md) \| [Previous (1.5 Lists)](05_Lists.md) \| [Next (1.7 Functions)](07_Functions.md)
10 changes: 5 additions & 5 deletions Notes/01_Introduction/07_Functions.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Contents](../Contents) \| [Previous (1.6 Files)](06_Files) \| [Next (2.0 Working with Data)](../02_Working_with_data/00_Overview)
[Contents](../Contents.md) \| [Previous (1.6 Files)](06_Files.md) \| [Next (2.0 Working with Data)](../02_Working_with_data/00_Overview.md)

# 1.7 Functions

Expand Down Expand Up @@ -41,15 +41,15 @@ import math
x = math.sqrt(10)

import urllib.request
u = urllib.request.urlopen('http://www.python.org/')
u = urllib.request.urlopen('http://www.python.org/')
data = u.read()
```

We will cover libraries and modules in more detail later.

### Errors and exceptions

Functions report errors as exceptions. An exception causes a function to abort and may
Functions report errors as exceptions. An exception causes a function to abort and may
cause your entire program to stop if unhandled.

Try this in your python REPL.
Expand Down Expand Up @@ -130,7 +130,7 @@ Try typing a command such as `help(greeting)` to see it displayed.

### Exercise 1.30: Turning a script into a function

Take the code you wrote for the `pcost.py` program in [Exercise 1.27](06_Files)
Take the code you wrote for the `pcost.py` program in [Exercise 1.27](06_Files.md)
and turn it into a function `portfolio_cost(filename)`. This
function takes a filename as input, reads the portfolio data in that
file, and returns the total cost of the portfolio as a float.
Expand Down Expand Up @@ -278,4 +278,4 @@ Total cost: 44671.15
bash %
```

[Contents](../Contents) \| [Previous (1.6 Files)](06_Files) \| [Next (2.0 Working with Data)](../02_Working_with_data/00_Overview)
[Contents](../Contents.md) \| [Previous (1.6 Files)](06_Files.md) \| [Next (2.0 Working with Data)](../02_Working_with_data/00_Overview.md)
16 changes: 8 additions & 8 deletions Notes/02_Working_with_data/00_Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ lists, sets, and dictionaries and discusses common data handling
idioms. The last part of this section dives a little deeper
into Python's underlying object model.

* [2.1 Datatypes and Data Structures](01_Datatypes)
* [2.2 Containers](02_Containers)
* [2.3 Formatted Output](03_Formatting)
* [2.4 Sequences](04_Sequences)
* [2.5 Collections module](05_Collections)
* [2.6 List comprehensions](06_List_comprehension)
* [2.7 Object model](07_Objects)
* [2.1 Datatypes and Data Structures](01_Datatypes.md)
* [2.2 Containers](02_Containers.md)
* [2.3 Formatted Output](03_Formatting.md)
* [2.4 Sequences](04_Sequences.md)
* [2.5 Collections module](05_Collections.md)
* [2.6 List comprehensions](06_List_comprehension.md)
* [2.7 Object model](07_Objects.md)

[Contents](../Contents)
[Contents](../Contents.md)
6 changes: 3 additions & 3 deletions Notes/02_Working_with_data/01_Datatypes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Contents](../Contents) \| [Previous (1.6 Files)](../01_Introduction/06_Files) \| [Next (2.2 Containers)](02_Containers)
[Contents](../Contents.md) \| [Previous (1.6 Files)](../01_Introduction/06_Files.md) \| [Next (2.2 Containers)](02_Containers.md)

# 2.1 Datatypes and Data structures

Expand Down Expand Up @@ -242,7 +242,7 @@ shares and the price:
```

Is math broken in Python? What’s the deal with the answer of
3220.0000000000005?
3220.0000000000005?

This is an artifact of the floating point hardware on your computer
only being able to accurately represent decimals in Base-2, not
Expand Down Expand Up @@ -446,4 +446,4 @@ dict_items([('name', 'AA'), ('shares', 75), ('price', 32.2), ('date', (6, 11, 20
>>>
```

[Contents](../Contents) \| [Previous (1.6 Files)](../01_Introduction/06_Files) \| [Next (2.2 Containers)](02_Containers)
[Contents](../Contents.md) \| [Previous (1.6 Files)](../01_Introduction/06_Files.md) \| [Next (2.2 Containers)](02_Containers.md)
Loading