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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
163 changes: 163 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# VSCode
*.code-workspace
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Learn Python Programming 4th Edition

Welcome to Learn Python Programming, 4th Edition.

Here you can find the complete source code for this book. Every
line of code you will find in the book is in this repository, plus
all the code that we couldn't fit in the book.

## Target Python Version

Python 3.12.*

## Working on a chapter

We suggest you create a dedicated virtual environment for each chapter
you wish to work on. Chapters that require third-party libraries will
have a folder inside, called `requirements`.

First you will need to create a virtual environment. If you don't know
what a virtual environment is, please read Chapter 1.

Let's now pretend you want to work on Chapter 7. First, change into the folder
for Chapter 7 (it's called `ch7`):

$ cd ch7

Then, create a virtual environment. In this example the virtual environment
will live inside the chapter's folder, but you can choose any other folder
that might suit you better.

$ python3.12 -m venv .venv

We have given the virtual environment folder the name `.venv`. Feel free
to choose any other name that you might want.

---

Note: the above procedure might fail in some of the chapter folders, due
to the presence of files that aren't meant to be run. If that is the case, you
should choose another folder where to place your virtual environment. You can
create one within the chapter folder itself, or place the virtual environment
outside of the chapter folder altogether.

---

Next step is to activate the virtual environment:

$ source .venv/bin/activate

If you're on Windows, to activate your environment, you will need to run:

$ .venv\Scripts\activate

Next, if the `requirements` folder is present, change into it, and run
the following command for each of the `.txt` files you will find in it.
Normally there is only a `requirements.txt` file.

$ cd requirements
$ pip install -U -r requirements.txt
$ cd .. # this brings you back to the root of the chapter

Once you have installed all requirements, you are ready to run the
chapter's code. If a chapter needs extra work to be set up, you will
find all the instructions you need in the chapter's text.

**Note**:
Always remember to activate the virtual environment before you install
third-party libraries.
6 changes: 6 additions & 0 deletions ch01/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Chapter 1 data files
====================

The files in this folder are not supposed to work if run.
They serve as source for the book chapters, and to provide a
quick copy/paste tool for whoever would need their content.
Empty file added ch01/example/core.py
Empty file.
Empty file added ch01/example/run.py
Empty file.
Empty file added ch01/example/util/__init__.py
Empty file.
Empty file added ch01/example/util/db.py
Empty file.
Empty file added ch01/example/util/maths.py
Empty file.
Empty file added ch01/example/util/network.py
Empty file.
3 changes: 3 additions & 0 deletions ch01/factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
>>> from math import factorial
>>> factorial(5)
120
Empty file added ch01/files_only/core.py
Empty file.
Empty file added ch01/files_only/db.py
Empty file.
Empty file added ch01/files_only/maths.py
Empty file.
Empty file added ch01/files_only/network.py
Empty file.
Empty file added ch01/files_only/run.py
Empty file.
19 changes: 19 additions & 0 deletions ch01/names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
>>> n = 3 # integer number
>>> address = "221b Baker Street, NW1 6XE, London" # Sherlock Holmes' address
>>> employee = {
... 'age': 45,
... 'role': 'CTO',
... 'SSN': 'AB1234567',
... }
>>> # let us print them
>>> n
3
>>> address
'221b Baker Street, NW1 6XE, London'
>>> employee
{'age': 45, 'role': 'CTO', 'SSN': 'AB1234567'}
>>> other_name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'other_name' is not defined
>>>
15 changes: 15 additions & 0 deletions ch01/scopes1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# scopes1.py
# Local versus Global

# we define a function, called local
def local():
age = 7
print(age)

# we define age within the global scope
age = 5

# we call, or `execute` the function local
local()

print(age)
13 changes: 13 additions & 0 deletions ch01/scopes2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# scopes2.py
# Local versus Global

def local():
# age does not belong to the scope defined by the local
# function so Python will keep looking into the next enclosing
# scope. age is finally found in the global scope.
print(age, 'printing from the local scope')

age = 5
print(age, 'printing from the global scope')

local()
21 changes: 21 additions & 0 deletions ch01/scopes3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# scopes3.py
# Local, Enclosing and Global


def enclosing_func():
age = 13

def local():
# age does not belong to the scope defined by the local
# function so Python will keep looking into the next
# enclosing scope. This time age is found in the enclosing
# scope
print(age, 'printing from the local scope')

# calling the function local
local()

age = 5
print(age, 'printing from the global scope')

enclosing_func()
70 changes: 70 additions & 0 deletions ch01/virtualenv.creation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
macOS:


fab@m1:~/code$ mkdir my-project # step 1
fab@m1:~/code$ cd my-project

fab@m1:~/code/my-project$ which python3.12 # check system python
/usr/bin/python3.12 # <-- system python3.12

fab@m1:~/code/my-project$ python3.12 -m venv lpp4ed # step 2
fab@m1:~/code/my-project$ source ./lpp4ed/bin/activate # step 3

# check python again: now using the virtual environment's one
(lpp4ed) fab@m1:~/code/my-project$ which python
/Users/fab/code/my-project/lpp4ed/bin/python

(lpp4ed) fab@m1:~/code/my-project$ python # step 4
Python 3.12.2 (main, Feb 14 2024, 14:16:36)
→ [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more
→ information.
>>> exit()

(lpp4ed) fab@m1:~/code/my-project$ deactivate # step 5
fab@m1:~/code/my-project$


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

Windows 11
Simply install from python website, then from terminal:

PS C:\Users\H\Code> mkdir my-project # step 1


Directory: C:\Users\H\Code


Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 3/15/2024 3:50 PM my-project


PS C:\Users\H\Code> cd .\my-project\

# check installed python versions
PS C:\Users\H\Code\my-project> py --list-paths
-V:3.12 *
→ C:\Users\H\AppData\Local\Programs\Python\Python312\python.exe


PS C:\Users\H\Code\my-project> py -3.12 -m venv lpp4ed # step 2
PS C:\Users\H\Code\my-project> .\lpp4ed\Scripts\activate # step 3

# check python versions again: now using the virtual environment's
(lpp4ed) PS C:\Users\H\Code\my-project> py --list-paths
*
→ C:\Users\H\Code\my-project\lpp4ed\Scripts\python.exe
-V:3.12
→ C:\Users\H\AppData\Local\Programs\Python\Python312\python.exe

(lpp4ed) PS C:\Users\H\Code\my-project> python # step 4
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36)
→ [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more
→ information.
>>> exit()

(lpp4ed) PS C:\Users\H\Code\my-project> deactivate # step 5
PS C:\Users\H\Code\my-project>
Loading