Show Menu
Cheatography

Python 3 Beginner's Reference Cheat Sheet by

Python 3 Beginner's reference

Operators

Assignment
=
Arithmetic
+
,
-
,
*
,
/
,
%
Comparison
>
,
>=
,
<
,
<=
,
==
,
!=
Logical
not, and, or

String operations (string s)

s.coun­t(s­ubs­tring)
Count occurences
s.find­(su­bst­ring)
Index of first occurence
s.join­(se­quence)
Concat­enate sequence
s.spli­t([­del­imi­ter])
Split into list

List operations (list l, element e)

l.appe­nd(e)
Add e
l.remo­ve(e)
Remove e
l.pop(e)
Remove and return e
l.count(e)
Count occurences
l.reve­rse()
Reverse l
l.sort()
Sort l

Dictionary operations (dict d, key k)

d.clear()
Clear d
d.get(k)
Return d[k]
d.keys()
Return keys in d
d.values()
Return values in d
d.items()
Return key-value pairs in d
 

File operations (file f)

f = open(path)
Open f
f.read()
Read f
f.read­line()
Read line from f
f.read­lines()
Return list of lines in f
f.write(s)
Write s to f
f.close()
Close f

In-built functions

int()
,
float()
,
str()
,
bool()
...
Type casting
len(data)
Length
min(va­lues)
,
max(va­lues)
Minimum / Maximum
pow(x,y, [z])
X to the power Y [mod Z]
range(­start, stop, [step])
Ordered list
input()
,
print()
Console Input/­Output
filter­(fu­nction, array)
Filter array
map(fu­nction, array)
Map function onto array
id(object)
Unique object ID
round(n, [x])
Round n [x decimal places]

Module import

import module
from module import submodule
 

Control Flow

if(cond): <co­de> else: <co­de>
If-else
if(cond1): <co­de> elif(c­ond1): <co­de> else: <co­de>
If-eli­f-else
for i in range(­[st­art], stop, [step]): <co­de>
For loop over range
for i in items: <co­de>
For loop over iterable
while(­con­dit­ion): <co­de>
While loop
break
Exit first enclosing loop
continue
Skip to next iteration

Useful standard library modules

math
,
numpy
,
scipy
Math
matplotlib
Graph plotting
random
Random generators
datetime
Date and time
timeit
Perfor­mance
re
Regular expres­sions
os
OS intera­ction
sys
stdin, stdout, stderr, version
urllib
Internet access
zlib
Data compre­ssion

Object­-or­iented

class Person:
Class definition
x = Person­(age, height)
Object creation
x.age
Field access
x.birt­hday()
Method access
                   
 

Comments

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