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

A Programming › Python cheat sheet in English by yaohongkok

Python Cheat Sheet for Work

10,052 views 3,062 downloads 31 Mar 2016 History
3.7 avg · 0 ratings

@ Command Line

Python Shell
$ python
Run Script­/File
$ python <fi­len­ame­>.py

Data Types

Boolean
True; False
Numeric
int(str); float(str)
String
'­abc­def­';
str(nu­mber)
List
['ab­c', 1, 2];
list(col)
Tuple (cannot update)
('ab­c', 1, 2);
tuple(col)
Dictionary
{'na­me': '­joh­n', '­num­': 1};
dict['k­ey'] = 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
'­abc­d'[1:] == '­bcd­';
'­abc­d'[:-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]

Badges

Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 2

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.