Core libraries Scripts by default only import sys

Core libraries Scripts, by default only import sys (various system services/functions) and builtins (built-in functions, exceptions and special objects like None and False). The Python shell doesn’t import sys, and builtins is hidden away as __builtins__.

Built in functions abs() dict() help() min() setattr() all() dir() hex() next() slice() any() divmod() id() object() sorted() ascii() enumerate() input() oct() staticmethod() bin() eval() int() open() str() bool() exec() isinstance() ord() sum() bytearray() filter() issubclass() pow() super() bytes() float() iter() print() tuple() callable() format() len() property() type() chr() frozenset() list() range() vars() classmethod() getattr() locals() repr() zip() compile() globals() map() reversed() __import__() complex() hasattr() max() round() delattr() hash() memoryview() set() https: //docs. python. org/3/library/functions. html

Python Standard Library https: //docs. python. org/3/py-modindex. html https: //docs. python. org/3/library/index. html https: //docs. python. org/3/tutorial/stdlib. html Most give useful recipes for how to do major jobs you're likely to want to do.

Useful libraries: text difflib – for comparing text documents; can for example generate a webpages detailing the differences. https: //docs. python. org/3/library/difflib. html Unicodedata – for dealing with complex character sets. See also "Fluent Python" https: //docs. python. org/3/library/unicodedata. html regex https: //docs. python. org/3/library/re. html

Collections https: //docs. python. org/3/library/collections. html # Tally occurrences of words in a list c = Counter() for word in ['red', 'blue', 'red', 'green', 'blue']: c[word] += 1 print(c) <Counter({'blue': 3, 'red': 2, 'green': 1})>

Collections https: //docs. python. org/3/library/collections. html # Find the ten most common words in Hamlet import re words = re. findall(r'w+', open('hamlet. txt'). read(). lower()) Counter(words). most_common(5) [('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631)] https: //docs. python. org/3/library/collections. html#collections. Counter

Useful libraries: binary data Binary https: //docs. python. org/3/library/binary. html See especially struct: https: //docs. python. org/3/library/struct. html

Useful libraries: maths math https: //docs. python. org/3/library/math. html decimal — Does for floating points what ints do; makes them exact https: //docs. python. org/3/library/decimal. html fractions — Rational numbers (For dealing with numbers as fractions https: //docs. python. org/3/library/fractions. html

Statistics https: //docs. python. org/3/library/statistics. html mean() Arithmetic mean (“average”) of data. harmonic_mean() Harmonic mean of data. median() Median (middle value) of data. median_low() Low median of data. median_high() High median of data. median_grouped() Median, or 50 th percentile, of grouped data. mode() Mode (most common value) of discrete data. pstdev() Population standard deviation of data. pvariance() Population variance of data. stdev() Sample standard deviation of data. variance() Sample variance of data.

Random selection Random library includes functions for: Selecting a random choice Shuffling lists Sampling a list randomly Generating different probability distributions for sampling.

Auditing random numbers Often we want to generate a repeatable sequence of random numbers so we can rerun models or analyses with random numbers, but repeatably. https: //docs. python. org/3/library/random. html#bookkeepingfunctions Normally uses os time, but can be forced to a seed.

Useful libraries: lists/arrays bisect — Array bisection algorithm (efficient large sorted arrays for finding stuff) https: //docs. python. org/3/library/bisect. html

Useful libraries: Tk. Inter https: //docs. python. org/3/library/tk. html Used for Graphical User Interfaces (windows etc. ) Wrapper for a library called Tk (GUI components) and its manipulation languages Tcl. See also: wx. Python: Native looking applications: https: //www. wxpython. org/ (Not in Anaconda)

Turtle https: //docs. python. org/3/library/turtle. html For drawing shapes. TKInter will allow you to load and display images, but there additional external libraries better set up for this, including Pillow: http: //python-pillow. org/

Useful libraries: talking to the outside world Serial ports https: //docs. python. org/3/faq/library. html#how-do-i-access-the-serial-rs 232 -port argparse — Parser for command-line options, arguments and sub-commands https: //docs. python. org/3/library/argparse. html datetime https: //docs. python. org/3/library/datetime. html

Databases DB-API https: //wiki. python. org/moin/Database. Programming dbm — Interfaces to Unix “databases” https: //docs. python. org/3/library/dbm. html Simple database sqlite 3 — DB-API 2. 0 interface for SQLite databases https: //docs. python. org/3/library/sqlite 3. html Used as small databases inside, for example, Firefox.
- Slides: 16