Section 1

Preview this deck

Sequence Type: slice of s from i to j

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

2

All-time users

2

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (11)

Section 1

(11 cards)

Sequence Type: slice of s from i to j

Front

There are seven sequence types: strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects. s[i:j:k] If i or j is negative, the index is relative to the end of sequence s: len(s) + i or len(s) + j is substituted. But note that -0 is still 0. The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j. If i or j is greater than len(s), use len(s). If i is omitted or None, use 0. If j is omitted or None, use len(s). If i is greater than or equal to j, the slice is empty. k cannot be zero. If k is None, it is treated like 1.

Back

re.search(pattern, string, flags=0)

Front

Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding MatchObjectinstance. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string. m = re.search(regex_string, text_line) if m: s = m.group('a_string")

Back

datetime difference in second

Front

(datetime_obj_a - datetime_obj_b).total_seconds()

Back

sorted

Front

sorted(iterable[, cmp[, key[, reverse]]]) Return a new sorted list from the items in iterable. cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None. key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None(compare the elements directly). reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed. The built-in sorted() function is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).

Back

datetime string

Front

datetime_obj.strptime('Dec 25 22:42:54.437', '%b %d %H:%M:%S.%f') print datetime_obj.strftime('%b %d %H:%M:%S.%f')

Back

string strips a string

Front

string.strip(s[, chars]) Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on.

Back

datetime import

Front

from datetime import datetime

Back

string strips a set of characters

Front

str.strip([chars]) Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:

Back

open file and read lines

Front

for line in sys.stdin.readlines():

Back

Mutable Sequence Types: (reverse) sort

Front

list, bytearray + The sort() and reverse() methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don't return the sorted or reversed list. Starting with Python 2.3, the sort() method is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).

Back

re.match(pattern, string, flags=0)

Front

If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.

Back