Cheatography

# Python@Work (By Build-2-Master) Cheat Sheet by yaohongkok

Python Cheat Sheet for Work

### @ Command Line

 Python Shell \$ python Run Script­/File \$ python .py

### Data Types

 Boolean True; False Numeric int(str); float(str) String 'abcdef'; str(nu­mber) List ['abc', 1, 2]; list(col) Tuple (cannot update) ('abc', 1, 2); tuple(col) Dictionary {'name': 'john', 'num': 1}; dict['­key'] = val; obj.__­dict__ Set (unique) set(col)
- 'col' means collection

### Operations

 Modulus 1 % 2 == 1 Exponent 9 ** 2 == 81 Increment c += a ~~ c=c+a Decrement c -= a ~~ c=c-a Logical AND (True and False) == False Logical OR (True or False) == True Membership 1 in [1,2,3] == True; 1 not in [1,2,3] == False Object Identity Obj1 is Obj2

### String

 Substring 'abcd'[1:] == 'bcd'; 'abcd'[:-1] == 'abc' Find str.find(otherstr) Check if number str.isdigit() Join "­-".join(["a­"­,"b"]) == "­a-b­"

### String (cont')

 Length len(str) Lower, Upper Case str.lower(); str.upper() Split "a b".split(" ") ==['a'­,'b']

### List

 Index ['a', 'b', 'c'][1:] == ['b','c'] Length len([1,2,3]) == 3 Concon­tenate [1,2] + [3,4] == [1,2,3,4] Append [1,2].append(3) == [1,2,3] Pop [1,2,3].pop(1) == 2 Remove [1,2,3].remove(1) Sort [2,1,3].sort() == [1,2,3]