Skip to content

Latest commit

 

History

History
66 lines (49 loc) · 1.23 KB

File metadata and controls

66 lines (49 loc) · 1.23 KB

Python Tips

Use isinstance(), not type()

type() only tells you the name of the class lowest in the hierarchy. isinstance() tells you whether the object inherits from the given class, so it works for child classes.

List, set, and dictionary comprehensions

my_list = []
for i in range(4):
    my_list.append(i)

my_list = [i for i in range(4)]

my_dict = []
for i in range(4):
    my_dict.append(i)

names = ['Alice', 'Bob', 'Claire']
aliases = ['Angel', 'Bobo', 'Clack']
my_dict = {name: alias for name, alias in zip(names, aliases)}

my_set = set([i for i in range(4)])

Generators

for i in range(4):
    yield i

my_generator = (i for i in range(4))

Iterators

Iterators are objects with iter() and next() methods.

The itertools module helps you efficiently loop through iterables.

import itertools

counter = itertools.count()

Web Scraping

Beautiful Soup makes it easy to parse HTML.

pip install beautifulsoup4
pip install lxml
pip install html5lib
pip install requests
from bs4 import BeautifulSoup
import requests

with open('example.html') as html_file:
    soup = BeautifulSoup(html_file, 'lxml')

print(soup.prettify())

match = soup.title
print(match)