Python Cheat Sheet for Work
|
Python Shell
|
$ python
|
|
Run Script/File
|
$ python <filename>.py
|
|
Boolean
|
True; False
|
|
Numeric
|
int(str); float(str)
|
|
String
|
'abcdef';
str(number) |
|
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)
|
|
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
|
|
Substring
|
'abcd'[1:] == 'bcd';
'abcd'[:-1] == 'abc' |
|
Find
|
str.find(otherstr)
|
|
Check if number
|
str.isdigit()
|
|
Join
|
"-".join(["a","b"])
== "a-b" |
|
Length
|
len(str)
|
|
Lower, Upper Case
|
str.lower();
str.upper() |
|
Split
|
"a b".split(" ")
==['a','b'] |
|
Index
|
['a', 'b', 'c'][1:]
== ['b','c'] |
|
Length
|
len([1,2,3]) == 3
|
|
Concontenate
|
[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]
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.