Show Menu
Cheatography

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

Python Cheat Sheet for Work

@ Command Line

Python Shell
$ python
Run Script­/File
$ python <fi­len­ame­>.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.­fin­d­(ot­herstr)
Check if number
str.­isd­igi­t()
Join
"­-".­jo­in­([­"­a","b­"])
== "­a-b­"
 

String (cont')

Length
len­(str)
Lower, Upper Case
str.­low­er();
str.­upp­er()
Split
"a b".­sp­lit­(­" ")
==['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].­a­ppe­nd(3) == [1,2,3]
Pop
[1,2,3­].­po­p(1) == 2
Remove
[1,2,3­].­re­mov­e(1)
Sort
[2,1,3­].­so­rt() == [1,2,3]
   

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter