Presentation is loading. Please wait.

Presentation is loading. Please wait.

Core libraries Scripts, by default only import sys (various system services/functions) and builtins (built-in functions, exceptions and special objects.

Similar presentations


Presentation on theme: "Core libraries Scripts, by default only import sys (various system services/functions) and builtins (built-in functions, exceptions and special objects."— Presentation transcript:

1 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__.

2 Built in functions https://docs.python.org/3/library/functions.html
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()

3 Python Standard Library
Most give useful recipes for how to do major jobs you're likely to want to do.

4 Useful libraries: text
difflib – for comparing text documents; can for example generate a webpages detailing the differences. Unicodedata – for dealing with complex character sets. See also "Fluent Python" regex

5 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', 'blue']: c[word] += 1 print(c) <Counter({'blue': 3, 'red': 2, 'green': 1})>

6 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)]

7 Useful libraries: binary data
See especially struct:

8 Useful libraries: maths
decimal — Does for floating points what ints do; makes them exact fractions — Rational numbers (For dealing with numbers as fractions

9 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 50th 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.

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

11 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. functions Normally uses os time, but can be forced to a seed.

12 Useful libraries: lists/arrays
bisect — Array bisection algorithm (efficient large sorted arrays for finding stuff)

13 Useful libraries: TkInter
Used for Graphical User Interfaces (windows etc.) Wrapper for a library called Tk (GUI components) and its manipulation languages Tcl. See also: wxPython: Native looking applications: (Not in Anaconda)

14 Turtle For drawing shapes. TKInter will allow you to load and display images, but there are additional external libraries better set up for this, including Pillow:

15 Useful libraries: talking to the outside world
Serial ports rs232-port argparse — Parser for command-line options, arguments and sub-commands datetime

16 Databases DB-API dbm — Interfaces to Unix “databases” Simple database sqlite3 — DB-API 2.0 interface for SQLite databases Used as small databases inside, for example, Firefox.


Download ppt "Core libraries Scripts, by default only import sys (various system services/functions) and builtins (built-in functions, exceptions and special objects."

Similar presentations


Ads by Google