dt1 = datetime.strptime('20091031','%Y%m%d')
dt1.date()
dt1.time()
dt1.strftime('%m/%d/%Y %H:%M')
dt2 = dt1.replace(minute = 0, second = 30)
diff = dt1 - dt2
tup1 = 4, 5, 6
or tup1 = (6,7,8)
tup1 = (4,5,6), (7,8)
tup1 = tuple([1,0,2])
tup1 + tup2
a, b, c = tup1
b,a = a,b
list1 = [1,'a',3]
or list1 = list(tup1)
list1 + list2
or list1.extend(list2)
The latter is better as it costs less computationally
list1.append('b')
list1.insert(posIdx,'b')
Computationally expensive compared to append
ValueAtIdx = list1.pop(posIdx)
list1.remove('a')
3 in list1 => True
Expensive for long lists because it uses linear scan
list1.sort()
##sort with user supplied function
list1.sort(key=len)
# sort by length
list1[start:stop]
(exclusive of stop) or
list1[start:stop:step]
if step is used
list1[::2]
str1[::-1]
dict1 = {'key1':'value1','key2':'value2'}
dict(zip(keyList,valueList))
dict1['key1']
returns KeyError if key isn't in dict
dict1['key1'] = newValue
dict1.get('key1', defaultValue)
returns None if key is not present
'key1' in dict1
del dict1['key']
dict1.keys()
dict1.values()
dict1.update(dict2)
dict1 values are replaced by dict2
set([3,6,3])
or {3,6,3}
set1.issubset(set2)
set1.issuperset(set2)
set1 == set2
set1 | set2
set1 & set2
set1 - set2
set1 ^ set2
def func1(a,b,c=None)
where a, b, and c are arguments
783 0 -192 0b010 (binary) 0o642 (octal) OxF3 (hexa)
9.23 0.0 -1.7e-6
True False
"One\nTwo" with new line embedded 'I'm' escaped apostrophe """ X\tY|tZ 1\t2\t3""" multiline string
b"toto\xfe\775" (alpha/hex/octal)
int('15') 15
int('3f',16) 63 (second parameter is base)
int(15.56) 15
float("-11.24e8") 1124000000.0
bool(x) False for null x, empty container, None or False x; True for other x
str(x) = '...' string of whatever is x
chr(64) '@'
ord('@') 64
repr(x) a literal representation string of x
bytes([72,9,64]) b'H\t@'
list('abc') ['a','b','c']
dict([(3,'three'),(1,'one')]) {1:'one',3:'three'}
set(['one','two']) {'one','two'}
separator str and sequence of str gives assembled str
':'.join(['toto','12','pswd']) 'toto:12:pswd'
str splitted on whitespaces list of str
'words with spaces'.split() ['words','with','spaces']
str splitted on separator str list of str
"1,4,8,2".split(",") ['1','4','8','2']
sequence of one type list of another type via list comprehension
[int(x) for x in ('1','29','-3')] = [1,29,-3]
list, tuple, str bytes
dict, set frozenset is an immutable set keys = hashable values
import os v = os.environ['VARIABLENAME']