Semper Phone

Effortless
LEARNING

  • Improve effortlessly – just by living your life
  • Learn while waiting for your apps to load
  • Recommended by 5 universities
  • Community of over 1,000,000 learners
  • 50,000+ expert-made packs, or create your own
"One of the best learning apps" - CNET
  • Apple Play Store
  • Install Semper from the Play Store
Python programming cheat sheet

Python programming cheat sheet

Last update 

python programming

Items (65)

  • x in s

    True if an item of s is equal to x, else False

  • x not in s

    False if an item of s is equal to x, else True

  • s1 + s2

    the concatenation of s1 and s2

  • s * n, n*s

    n copies of s concatenated

  • s[i]

    i'th item of s, origin 0

  • s[i: j]

    Slice of s from i (included) to j(excluded). Optional step value, possibly negative (default: 1).

  • s.count(x)

    returns number of i's for which s[i] == x

  • s.index(x[, start[, stop]])

    returns smallest i such that s[i]==x. start and stop limit search to only part of the sequence.

  • len(s)

    Length of s

  • min(s)

    Smallest item of s

  • max(s)

    Largest item of s

  • reversed(s)

    [2.4] Returns an iterator on s in reverse order. s must be a sequence, not an iterator (use reversed(list(s)) in this case.[PEP 322]

  • sorted(iterable [, cmp]  [, cmp=cmpFunc]  [, key=keyGetter]  [, reverse=bool])

    [2.4] works like the new in-place list.sort(), but sorts a new list created from the iterable.

  • s.capitalize()

    Returns a copy of s with its first character capitalized, and the rest of the characters lowercased.

  • s.center(width[, fillChar=' '])

    Returns a copy of s centered in a string of length width, surrounded by the appropriate number of fillChar characters.

  • s.count(sub[, start[, end]])

    Returns the number of occurrences of substring sub in string s.

  • s.decode([encoding[, errors]])

    Returns a unicode string representing the decoded version of str s, using the given codec (encoding). Useful when reading from a file or a I/O function that handles only str. Inverse of encode.

  • s.encode([encoding[, errors]])

    Returns a str representing an encoded version of s. Mostly used to encode a unicode string to a str in order to print it or write it to a file (since these I/O functions only acceptstr), e.g. u'légère'.encode('utf8'). Also used to encode a str to a str, e.g. to zip (codec 'zip') or uuencode (codec 'uu') it. Inverse of decode.

  • s.endswith(suffix [, start[, end]])

    Returns True if s ends with the specified suffix, otherwise return false. Since 2.5 suffix can also be a tuple of strings to try.

  • s.expandtabs([tabsize])

    Returns a copy of s where all tab characters are expanded using spaces.

  • s.find(sub [,start[,end]])

    Returns the lowest index in s where substring sub is found. Returns -1 if sub is not found.

  • s.format(*args, *kwargs)

    Returns s after replacing numeric and named formatting references found in braces {}. (details)

  • s.index(sub[, start[, end]])

    like find(), but raises ValueError when the substring is not found.

  • s.isalnum()

    Returns True if all characters in s are alphanumeric, False otherwise.

  • s.isalpha()

    Returns True if all characters in s are alphabetic, False otherwise.

  • s.isdigit()

    Returns True if all characters in s are digit characters, False otherwise.

  • s.islower()

    Returns True if all characters in s are lowercase, False otherwise.

  • s.isspace()

    Returns True if all characters in s are whitespace characters, False otherwise.

  • s.istitle()

    Returns True if string s is a titlecased string, False otherwise.

  • s.isupper()

    Returns True if all characters in s are uppercase, False otherwise.

  • separator.join(seq)

    Returns a concatenation of the strings in the sequence seq, separated by string separator, e.g.: ",".join(['A', 'B', 'C']) -> "A,B,C"

  • s.ljust/rjust/center(width[, fillChar=' '])

    Returns s left/right justified/centered in a string of length width.

  • s.lower()

    Returns a copy of s converted to lowercase.

  • s.lstrip([chars] )

    Returns a copy of s with leading chars (default: blank chars) removed.

  • s.partition(separ)

    Searches for the separator separ in s, and returns a tuple (head, sep, tail) containing the part before it, the separator itself, and the part after it. If the separator is not found, returns (s, '', '').

  • s.replace(old, new[, maxCount =-1])

    Returns a copy of s with the first maxCount (-1: unlimited) occurrences of substring old replaced by new.

  • s.rfind(sub[ , start[, end]])

    Returns the highest index in s where substring sub is found. Returns -1 if sub is not found.

  • s.rindex(sub[ , start[, end]])

    like rfind(), but raises ValueError when the substring is not found.

  • s.rpartition(separ)

    Searches for the separator separ in s, starting at the end of s, and returns a tuple (head, sep, tail) containing the (left) part before it, the separator itself, and the (right) part after it. If the separator is not found, returns ('', '', s).

  • s.rstrip([chars])

    Returns a copy of s with trailing chars(default: blank chars) removed, e.g. aPath.rstrip('/') will remove the trailing '/'from aPath if it exists

  • s.split([ separator[, maxsplit]])

    Returns a list of the words in s, using separator as the delimiter string.

  • s.rsplit([ separator[, maxsplit]])

    Same as split, but splits from the end of the string.

  • s.splitlines([ keepends])

    Returns a list of the lines in s, breaking at line boundaries.

  • s.startswith(prefix [, start[, end]])

    Returns True if s starts with the specified prefix, otherwise returns False. Negative numbers may be used for start and end. Since 2.5 prefix can also be a tuple of strings to try.

  • s.strip([chars])

    Returns a copy of s with leading and trailing chars(default: blank chars) removed.

  • s.swapcase()

    Returns a copy of s with uppercase characters converted to lowercase and vice versa.

  • s.title()

    Returns a titlecased copy of s, i.e. words start with uppercase characters, all remaining cased characters are lowercase.

  • s.translate(table[, deletechars=''])

    Returns a copy of s mapped through translation table table. Characters from deletechars are removed from the copy prior to the mapping. Since 2.6 table may also be None(identity transformation) - useful for using translate to delete chars only.

  • s.upper()

    Returns a copy of s converted to uppercase.

  • s.zfill(width)

    Returns the numeric string left filled with zeros in a string of length width.

  • f.close()

    Close file f.

  • f.fileno()

    Get fileno (fd) for file f.

  • f.flush()

    Flush file f's internal buffer.

  • f.isatty()

    1 if file f is connected to a tty-like dev, else 0.

  • f.next()

    Returns the next input line of file f, or raises StopIteration when EOF is hit. Files are their own iterators. next is implicitly called by constructs like for line in f: print line.

  • f.read([size])

    Read at most size bytes from file f and return as a string object. If size omitted, read to EOF.

  • f.readline()

    Read one entire line from file f. The returned line has a trailing , except possibly at EOF. Return '' on EOF.

  • f.readlines()

    Read until EOF with readline() and return a list of lines read.

  • f.xreadlines()

    Return a sequence-like object for reading a file line-by-line without reading the entire file into memory. From 2.2, use rather: for line in f (see below).

  • for line in f: do something...

    Iterate over the lines of a file (using readline)

  • f.seek(offset[, whence=0])

    Set file f's position, like "stdio's fseek()".whence == 0 then use absolute indexing.whence == 1 then offset relative to current pos.

  • f.tell()

    Return file f's current position (byte offset).

  • f.truncate([size])

    Truncate f's size. If size is present, f is truncated to (at most) that size, otherwise f is truncated at current position (which remains unchanged).

  • f.write(str)

    Write string to file f.

  • f.writelines(list)

    Write list of strings to file f. No EOL are added.