< prev | index

Bringing it all together

The Standard Library

Python includes extra functionality that can be accessed with the 'import' keyword. This functionality includes solutions to common problems and access to operating system functionality. For documentation on the many modules in the standard library, look here. Below I will show how to import and use standard library modules, as well as modules downloaded from other sources such as the Python Package Index (PyPI).

Randomness

Randomness is great for adding some variety and spice to a program. Pseudorandom functionality is provided by the random module in the standard library. Make sure to read the documentation for a full overview of the capabilities of the module.

import random

for _ in range(10):
    print(random.randint(1, 100))

Making a game

You should have the tools to make a guessing game now. You may want to give it a go yourself first, and then check out these examples.

Command Line Arguments

You can use command line arguments to pass values into a program when it starts. This is similar to passing an argument into a function. Assuming your program is called hello.py:

python3 hello.py foobarbaz

Your program will receive the string foobarbaz as an argument which it can then use.

Python too is a program, and it is being given the argument "hello.py". In this case Python uses the argument to know which file to run.

The below sample illustrates basic usage of arguments, using functionality from the sys module:

from sys import argv
print(argv) # prints all args
print(argv[0]) # is always the name of the program
# caution!
# the below line will fail if at least 2 arguments aren't passed in to the program!
print(argv[1], argv[2])

File I/O

Reading and writing files will let us expand beyond user input. It will allow us to read information from and write information to files. Now we have a way to make our data persist even after our program ends!

with open('my_file.txt', 'w', encoding='ascii') as f: # second argument is the mode to open the file in, w is for write
    # encoding above refers to how bytes translates to characters.
    # ascii is very common for simple english text and special characters.
    # utf-8 covers characters from many languages, and even emojis
    # character encodings are an interesting topic for further reading if you're curious
    f.write('This is both the beginning\nand the end\n')

with open('my_file.txt', 'a', encoding='ascii') as f: # a is for append
    f.write('This is the new end\n')

with open('my_file.txt', 'r', encoding='ascii') as f: # r for read
    for i, line in enumerate(f):
        print(f'line {i}: {line}', end='') # we can tell print not to add a new line at the end
        # we already have new lines on the lines we are reading

with open('my_file.txt', 'r+', encoding='ascii') as f: # r+ for read and write
    f.seek(17) # advance forward in the file 17 characters
    f.write('foobarbaz')
    f.seek(0) # back to the beginning!
    print(f.readline()) # let's see the result

The 'with' clause is introduced here. It provides a guarantee that at the end of the with block, all cleanup will be taken care of for you. With statements use context managers behind the scenes. Later, you'll be able to create your own context managers, but for a beginner, with statements offer a convenient and powerful way to safely handle working with files. Context managers and with statements allow you to conveniently reuse chunks of code that require setup and teardown with error handling.

By default, files get opened in text mode. You can add a 'b' to the end of the mode to open them in binary mode. You might open a file in binary mode if it is an image file or other special format to gain more direct access to the underlying bits and bytes without the assumptions that text mode makes. To learn more about reading and writing files, check out the official documentation.

More about imports; Working with dependencies; Exploring the web

Every file is its own module as well! If you have two Python files, named 'example_a.py' and 'example_b.py' that are in the same directory, example_a.py can use code written in the other file:

import example_b
# variables and functions defined inside of example_b can be accessed like so:
example_b.some_function()

Downloading packages from the Python Package Index (PyPI)

pip is the tool used to install packages from online. It should have come with your Python installation. It's a good idea to keep pip up to date, you can use the below command for this.

pip install --upgrade pip

On Linux, if you find that you do not have pip, you can use your system's package manager to install it. For instance, on a Debianlike operating system with the apt package manager, you could run:

sudo apt install python3-pip

Managing dependencies and versions

If you install a package with pip, that version is likely to end up available for ALL of your Python projects on your computer. This can cause conflicts and confusion, particularly if you ever need to have projects using different versions of things. For this reason, we create a virtual environment for each Python project. A virtual environment stores our downloaded packages and keeps them isolated.

When we want to run our project, we must make sure the virtual environment is activated. Below are Linux commands to create a virtual environment called 'venv', to activate the environment, and to download packages listed in a file.

python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

See this link for all the ways to activate a venv on different platforms.

If you are on Linux and find that you do not have access to the venv module, you can install it with your system's package manager. An example using apt is below:

sudo apt install python3-venv

Browsing the web with the requests package

Let's use a package from PyPI! Our first step is optional, but strongly recommended. First, create a virtual environment and enable it.

python3 -m venv venv
source venv/bin/activate

Then run:

pip install requests

Now, when your Python programs run inside of the virtual environment they can import requests and make use of the tools in this library. Check out the documentation for the library to get a feel for all of the things you can do with it! Let's look at the contents of a website:

import requests
response = requests.get('http://example.com')
print(response.text)

The body of the response for this webpage is in HTML. This website is fairly simple, with only a few elements in its body. Getting the raw HTML of a page isn't immediately useful to us; how do we make sense of it? Let's pull in another library!

pip install beautifulsoup4
from bs4 import BeautifulSoup
import requests
response = requests.get('http://example.com')
html_doc = response.text
soup = BeautifulSoup(html_doc, 'html.parser')
headers = soup.find_all('h1')
print(headers[0].text)

On HTML pages, important information is often stored in header elements. Header elements are numbered and their tag is "hn", with n being a number. Lower numbers are more important, with 1 being most imporant. With a little bit of study into how HTML is structured, you can use different HTML tags and information inside the tags such as classes and ids to extract any information you want out of a website! Using your browser's developer tools can be helpful here. On most desktop browsers, you can open the developer tools by hitting F12. Not only can you inspect the HTML content, but you can also inspect the network requests that a website makes in order to work. Be aware that some websites generate content with a combination of scripts and web requests. If this is the case, you can often mimic the web requests that the site is making and get the information directly.


< prev | index