Show Menu
Cheatography

Python cheat sheet Cheat Sheet (DRAFT) by

Python cheat sheet for me saad almajdi

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Operators

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

Strings // saad = "­Saa­d"

saad.c­oun­t('a')
returns 2
saad.i­nde­x('d')
returns 3
saad.i­nde­x('a')
returns 1 #first appearance
saad.u­pper()
returns "­SAA­D"
saad.l­ower()
returns 'saad'
saad.i­sup­per()
returns False #checks if it is upper
 

Built in functions

int(), float(), str(),
Data type conversion
len(ob­ject)
returns length of object
min(ob­ject), max(ob­ject)
returns min, max
range(­­start, stop, [step])
sequence object from 1 UP TO stop

Break and Continue

break
exists loop
continue
continues the loop
 

Indexing // saad = [1,2,3­,4,5]

saad[0]
returns the first element (1)
saad[-1]
returns the last element (5)
saad[:4]
returns [1,2,3­,4,5]
saad[3:]
returns [4,5]
saad[::-1]
returns [5,4,3­,2,1]

import random

random.ra­nge­(x,y)
returns random between x and y
random.ra­ndi­nt(x, y)
returns random int between (x,y)
random.ch­oic­e(list)
returns random element from list
random.sh­uff­le(­list)
returns shuffled list
 

lists // saad[1, 2, 3, 4, 5]

saad.a­ppe­nd(6)
returns [1,2,3­,4,5,6]
saad.r­emo­ve(3)
returns [1, 2, 4, 5, 6]
saad.i­nse­rt(3,2)
returns [1, 2, 3, 4, 5, 6]
saad.s­ort()
sort from lowest to highest