Important !!! - Do not name your python script the same as the module being imported!!! e.g. if you want to import "calendar" module, do not name you script as calendar.py ... it will import your script instead.
This module implements specialized container datatypes providing alternatives to Python's general purpose built-in containers, dict,
list, set, and tuple.
namedtuple
factory function for creating tuple subclasses with named fields
deque
list-like container with fast appends and pops on either end
.append(..)
.appendleft(..)
.clear(..)
.copy(..)
.count(..)
.extend(..)
.extendleft(..)
.index(..)
.insert(..)
.pop(..)
.popleft(..)
.remove(..)
reverse(..)
.rotate(..)
ChainMap
dict-like class for creating a single view of multiple mappings
Counter
dict subclass for counting hashable objects
OrderedDict
dict subclass that remembers the order entries were added
defaultdict
dict subclass that calls a factory function to supply missing values
UserDict
wrapper around dictionary objects for easier dict subclassing
UserList
wrapper around list objects for easier list subclassing
UserString
wrapper around string objects for easier string subclassing
namedtuple example 2
>>> from collections import namedtuple
>>> Car = namedtuple('Car','Price Mileage Colour Class')
>>> xyz = Car(Price = 100000, Mileage = 30, Colour = 'Cyan', Class = 'Y')
>>> print xyz
Car(Price=100000, Mileage=30, Colour='Cyan', Class='Y')
>>> print xyz.Class
Y
namedtuple example 3
#input line 1 N = no. of students
#input line 2 = column headings that include MARKS
#next N lines, columns of data for each headingfrom collections import namedtuple
N = int(input())
Student = namedtuple('Student',input().split())
print(sum([ int(Student._make(input().split()).MARKS) for i in range(N) ]) / N)
>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> d['python'].append("awesome")
>>> d['something-else'].append("not relevant")
>>> d['python'].append("language")
>>> for i in d.items():
print i
('python', ['awesome', 'language'])
('something-else', ['not relevant'])
explanation: when d['python'] was first used, there was no item in the dictionary with key='python', but because default factory was set as list, it will call list() to assign empty list to d['python'] and then allow it to call the append method (which is available to list data type] to
add items to that list value.
OrderedDict example
from collections import OrderedDict
N = int(input())
inventory = OrderedDict()
for i in range(N) :
itemlist = input().split()
item_name = ' '.join(itemlist[:-1])
price = int(itemlist[-1])
try:
inventory[item_name] += price
except:
inventory[item_name] = price
for item,price in inventory.items():
print(item,price)
Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools/*/*.gif), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).
If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is followed by an os.sep, only directories and subdirectories match.
Note
Using the “**” pattern in large directory trees may consume an inordinate amount of time.
Changed in version 3.5: Support for recursive globs using “**”.
glob.iglob(pathname, *, recursive=False)
Return an iterator which yields the same values as glob() without actually storing them all simultaneously.
glob.escape (pathname)
Escape all special characters ('?', '*' and '['). This is useful if you want to match an arbitrary literal string that may have special characters in it. Special characters in drive/UNC sharepoints are not escaped, e.g. on Windows escape('//?/c:/Quovadis?.txt') returns '//?/c:/Quovadis[?].txt'.
acos(x, /)
Return the arc cosine (measured in radians) of x.
acosh(x, /)
Return the inverse hyperbolic cosine of x.
asin(x, /)
Return the arc sine (measured in radians) of x.
asinh(x, /)
Return the inverse hyperbolic sine of x.
atan(x, /)
Return the arc tangent (measured in radians) of x.
atan2(y, x, /)
Return the arc tangent (measured in radians) of y/x.
Unlike atan(y/x), the signs of both x and y are considered.
atanh(x, /)
Return the inverse hyperbolic tangent of x.
ceil(x, /)
Return the ceiling of x as an Integral.
This is the smallest integer >= x.
copysign(x, y, /)
Return a float with the magnitude (absolute value) of x but the sign of y.
On platforms that support signed zeros, copysign(1.0, -0.0)
returns -1.0.
cos(x, /)
Return the cosine of x (measured in radians).
cosh(x, /)
Return the hyperbolic cosine of x.
degrees(x, /)
Convert angle x from radians to degrees.
erf(x, /)
Error function at x.
erfc(x, /)
Complementary error function at x.
exp(x, /)
Return e raised to the power of x.
expm1(x, /)
Return exp(x)-1.
fabs(x, /)
Return the absolute value of the float x.
factorial(x, /)
Find x!.
Raise a ValueError if x is negative or non-integral.
floor(x, /)
Return the floor of x as an Integral.
This is the largest integer <= x.
fmod(x, y, /)
Return fmod(x, y), according to platform C.
x % y may differ.
frexp(x, /)
Return the mantissa and exponent of x, as pair (m, e).
m is a float and e is an int, such that x = m * 2.**e.
If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
fsum(seq, /)
Return an accurate floating point sum of values in the iterable seq.
Assumes IEEE-754 floating point arithmetic.
gamma(x, /)
Gamma function at x.
gcd(x, y, /)
greatest common divisor of x and y
hypot(x, y, /)
Return the Euclidean distance, sqrt(x*x + y*y).
isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
Determine whether two floating point numbers are close in value.
rel_tol
maximum difference for being considered "close", relative to the
magnitude of the input values
abs_tol
maximum difference for being considered "close", regardless of the
magnitude of the input values
Return True if a is close in value to b, and False otherwise.
For the values to be considered close, the difference between them
must be smaller than at least one of the tolerances.
-inf, inf and NaN behave similarly to the IEEE 754 Standard. That
is, NaN is not close to anything, even itself. inf and -inf are
only close to themselves.
isfinite(x, /)
Return True if x is neither an infinity nor a NaN, and False otherwise.
isinf(x, /)
Return True if x is a positive or negative infinity, and False otherwise.
isnan(x, /)
Return True if x is a NaN (not a number), and False otherwise.
ldexp(x, i, /)
Return x * (2**i).
This is essentially the inverse of frexp().
lgamma(x, /)
Natural logarithm of absolute value of Gamma function at x.
log(...)
log(x, [base=math.e])
Return the logarithm of x to the given base.
If the base not specified, returns the natural logarithm (base e) of x.
log10(x, /)
Return the base 10 logarithm of x.
log1p(x, /)
Return the natural logarithm of 1+x (base e).
The result is computed in a way which is accurate for x near zero.
log2(x, /)
Return the base 2 logarithm of x.
modf(x, /)
Return the fractional and integer parts of x.
Both results carry the sign of x and are floats.
pow(x, y, /)
Return x**y (x to the power of y).
radians(x, /)
Convert angle x from degrees to radians.
remainder(x, y, /)
Difference between x and the closest integer multiple of y.
Return x - n*y where n*y is the closest integer multiple of y.
In the case where x is exactly halfway between two multiples of
y, the nearest even value of n is used. The result is always exact.
sin(x, /)
Return the sine of x (measured in radians).
sinh(x, /)
Return the hyperbolic sine of x.
sqrt(x, /)
Return the square root of x.
tan(x, /)
Return the tangent of x (measured in radians).
tanh(x, /)
Return the hyperbolic tangent of x.
trunc(x, /)
Truncates the Real x to the nearest Integral toward 0.
os
References:help('os') / https://docs.python.org/3/library/os.html
Methods
Examples
os.rename(current_filename,new_filename)
os.remove(filename)
os.mkdir(dirname)
create directory
os.chdir(dirpath)
change directory
os.getcwd()
get current directory
os.rmdir(dirname)
remove the directory
os.path.exists(path)
determine if the dir or file specified in the path exists
os.system(command)
run Operating System command
os.mkdir(path)
creates new dir
Eg. 1 list files and then rename to specific format
find files that match "p (n).jpg" and rename to "p00n.jpg"
import os
import re
for f in os.listdir():
m = re.match(r'(p) [(]([0-9]+)[)](.jpg)',f)
if m is not None:
nf = "{}{:03d}{}".format(m.group(1),int(m.group(2)),m.group(3))
os.rename(f,nf)
re.search = search(pattern, string, flags=0)
Scan through string looking for a match to the pattern, returning
a Match object, or None if no match was found
re.match = match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a Match object, or None if no match was found.
re.fullmatch = fullmatch(pattern, string, flags=0)
Try to apply the pattern to all of the string, returning
a Match object, or None if no match was found.
re.split = split(pattern, string, maxsplit=0, flags=0)
Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings. If
capturing parentheses are used in pattern, then the text of all
groups in the pattern are also returned as part of the resulting
list. If maxsplit is nonzero, at most maxsplit splits occur,
and the remainder of the string is returned as the final element
of the list.
re.findall = findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.
If one or more capturing groups are present in the pattern, return
a list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result
re.finditer = finditer(pattern, string, flags=0)
Return an iterator over all non-overlapping matches in the
string. For each match, the iterator returns a Match object.
Empty matches are included in the result.
re.sub = sub(pattern, repl, string, count=0, flags=0)
Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable;
if a string, backslash escapes in it are processed. If it is
a callable, it's passed the Match object and must return
a replacement string to be used.
re.subn = subn(pattern, repl, string, count=0, flags=0)
Return a 2-tuple containing (new_string, number).
new_string is the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in the source
string by the replacement repl. number is the number of
substitutions that were made. repl can be either a string or a
callable; if a string, backslash escapes in it are processed.
If it is a callable, it's passed the Match object and must
return a replacement string to be used.
re.escape = escape(pattern)
Escape special characters in a string.
re.purge = purge()
Clear the regular expression caches
exception re.error(msg, pattern=None, pos=None)
Exception raised when a string passed to one of the functions here is not a valid regular expression (for example, it might contain unmatched parentheses) or when some other error occurs during compilation or matching.