Python Cheatsheet For Beginners

Libraries

Import libraries

import numpy
import numpy as np

Selective import

from math import pi

Asking for Help

>>> help(str)

Variables and Data Types

Variable Assignment

>>> x=5
>>> x
5

Calculations With Variables

Sum of two variables
>>> x+2
7
Subtraction of two variables
>>> x-2
3
Multiplication of two variables
>>> x*2
10
Exponentiation of a variable
>>> x**2
25
Remainder of a variable
>>> x%2
1
Division of a variable
>>> x/float(2)
2.5

Types and Type Conversion

Variables to strings
str()
'5', '3.45', 'True'
Variables to integers
int()
5, 3, 1
Variables to floats
float()
5.0, 1.0
Variables to booleans
bool()
True, True, True

Strings

>>> my_string = 'thisStringIsAwesome'
>>> my_string
'thisStringIsAwesome'

String Operations

>>> my_string * 2
'thisStringIsAwesomethisStringIsAwesome'
>>> my_string + 'Innit'
'thisStringIsAwesomeInnit'
>>> 'm' in my_string
'True'

Selecting String Characters

>>> my_string[3]
>>> my_string[4:9]

String Methods

String to uppercase
>>> my_string.upper()
String to lowercase
>>> my_string.lower()
Count String elements
>>> my_string.count('w')
Replace String elements
>>> my_string.replace('e', 'i')
Strip whitespace from ends
>>> my_string.strip()

Lists

>>> a = 'is'
>>> b = 'nice'
>>> my_list = ['my', 'list', a, b]
>>> my_list2 = [[4,5,6,7], [3,4,5,6]]

Selecting List Elements

Subset

Select item at index 1
>>> my_list[1]
Select 3rd last item
>>> my_list[-3]

Slice

Select items at index 1 and 2
>>> my_list[1:3]
Select items after index 0
>>> my_list[1:]
Select items before index 3
>>> my_list[:3]
Copy my_list
>>> my_list[:]

Subset Lists of Lists

my_list[list][itemOfList]
>>> my_list2[1][0]
>>> my_list2[1][:2]

Lists Operations

>>> my_list + my_list
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list * 2
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list2 > 4
>>> True

List Methods

Get the index of an item
>>> my_list.index(a)
Count an item
>>> my_list.count(a)
Append an item at a time
>>> my_list.append('!')
Remove an item
>>> my_list.remove('!')
Remove an item
>>> del(my_list[0:1])
Reverse the list
>>> my_list.reverse()
Append an item
>>> my_list.extend('!')
Remove an item
>>> my_list.pop(-1)
Insert an item
>>> my_list.insert(0,'!')
Sort the list
>>> my_list.sort()

Numpy Arrays

>>> my_list = [1, 2, 3, 4]
>>> my_array = np.array(my_list)
>>> my_2darray = np.array([[1,2,3],[4,5,6]])

Selecting Numpy Array Elements

Subset

Select item at index 1
>>> my_array[1]
2

Slice

Select items at index 0 and 1
>>> my_array[0:2]
array([1, 2])

Subset 2D Numpy arrays

my_2darray[rows, columns]
>>> my_2darray[:,0]
array([1, 4])

Numpy Array Operations

>>> my_array > 3
array([False, False, False, True], dtype=bool)
>>> my_array * 2
array([2, 4, 6, 8])
>>> my_array + np.array([5, 6, 7, 8])
array([6, 8, 10, 12])])

Numpy Array Functions

Get the dimensions of the array
>>> my_array.shape
Append items to an array
>>> np.append(other_array)
Insert items in an array
>>> np.insert(my_array, 1, 5)
Delete items in an array
>>> np.delete(my_array,[1])
Mean of the array
>>> np.mean(my_array)
Median of the array
>>> np.median(my_array)
Correlation coefficient
>>> my_array.corrcoef()
Standard deviation
>>> np.std(my_array)

 

Python sys Variables

argv
Command line args
builti­n_m­odu­le_­names
Linked C modules
byteorder
Native byte order
check_­int­erval
Signal check frequency
exec_p­refix
Root directory
executable
Name of executable
exitfunc
Exit function name
modules
Loaded modules
path
Search path
platform
Current platform
stdin, stdout, stderr
File objects for I/O
versio­n_info
Python version info
winver
Version number

Python sys.argv

sys.ar­gv[0]
foo.py
sys.ar­gv[1]
bar
sys.ar­gv[2]
-c
sys.ar­gv[3]
qux
sys.ar­gv[4]
–h
sys.argv for the command:

$ python foo.py bar -c qux –h

Python os Variables

altsep
Altern­ative sep
curdir
Current dir string
defpath
Default search path
devnull
Path of null device
extsep
Extension separator
linesep
Line separator
name
Name of OS
pardir
Parent dir string
pathsep
Patch separator
sep
Path separator
Registered OS names: “­pos­ix”, “­nt”,

“­mac­”, “­os2­”, “­ce”, “­jav­a”, “­ris­cos­”

Python Class Special Methods

__new_­_(cls)
__lt__­(self, other)
__init­__(­self, args)
__le__­(self, other)
__del_­_(self)
__gt__­(self, other)
__repr­__(­self)
__ge__­(self, other)
__str_­_(self)
__eq__­(self, other)
__cmp_­_(self, other)
__ne__­(self, other)
__inde­x__­(self)
__nonz­ero­__(­self)
__hash­__(­self)
__geta­ttr­__(­self, name)
__geta­ttr­ibu­te_­_(self, name)
__seta­ttr­__(­self, name, attr)
__dela­ttr­__(­self, name)
__call­__(­self, args, kwargs)

Python List Methods

append­(item)
pop(po­sition)
count(­item)
remove­(item)
extend­(list)
reverse()
index(­item)
sort()
insert­(po­sition, item)

Python String Methods

capita­lize() *
lstrip()
center­(width)
partit­ion­(sep)
count(sub, start, end)
replac­e(old, new)
decode()
rfind(sub, start ,end)
encode()
rindex­(sub, start, end)
endswi­th(sub)
rjust(­width)
expand­tabs()
rparti­tio­n(sep)
find(sub, start, end)
rsplit­(sep)
index(sub, start, end)
rstrip()
isalnum() *
split(sep)
isalpha() *
splitl­ines()
isdigit() *
starts­wit­h(sub)
islower() *
strip()
isspace() *
swapcase() *
istitle() *
title() *
isupper() *
transl­ate­(table)
join()
upper() *
ljust(­width)
zfill(­width)
lower() *
Methods marked * are locale dependant for 8-bit strings.

Python File Methods

close()
readli­nes­(size)
flush()
seek(o­ffset)
fileno()
tell()
isatty()
trunca­te(­size)
next()
write(­string)
read(size)
writel­ine­s(list)
readli­ne(­size)

Python Indexes and Slices

len(a)
6
a[0]
0
a[5]
5
a[-1]
5
a[-2]
4
a[1:]
[1,2,3­,4,5]
a[:5]
[0,1,2­,3,4]
a[:-2]
[0,1,2,3]
a[1:3]
[1,2]
a[1:-1]
[1,2,3,4]
b=a[:]
Shallow copy of a
Indexes and Slices of a=[0,1­,2,­3,4,5]

Python Datetime Methods

today()
fromor­din­al(­ord­inal)
now(ti­mez­one­info)
combin­e(date, time)
utcnow()
strpti­me(­date, format)
fromti­mes­tam­p(t­ime­stamp)
utcfro­mti­mes­tam­p(t­ime­stamp)

Python Time Methods

replace()
utcoff­set()
isofor­mat()
dst()
__str__()
tzname()
strfti­me(­format)

Python Date Formatting

%a
Abbrev­iated weekday (Sun)
%A
Weekday (Sunday)
%b
Abbrev­iated month name (Jan)
%B
Month name (January)
%c
Date and time
%d
Day (leading zeros) (01 to 31)
%H
24 hour (leading zeros) (00 to 23)
%I
12 hour (leading zeros) (01 to 12)
%j
Day of year (001 to 366)
%m
Month (01 to 12)
%M
Minute (00 to 59)
%p
AM or PM
%S
Second (00 to 61⁴)
%U
Week number¹ (00 to 53)
%w
Weekday² (0 to 6)
%W
Week number³ (00 to 53)
%x
Date
%X
Time
%y
Year without century (00 to 99)
%Y
Year (2008)
%Z
Time zone (GMT)
%%
A literal “­%” character (%)
¹ Sunday as start of week. All days in a new year preceding the first Sunday are considered to be in week 0.

² 0 is Sunday, 6 is Saturday.

³ Monday as start of week. All days in a new year preceding the first Monday are considered to be in week 0.

⁴ This is not a mistake. Range takes account of leap and double­-leap seconds.

Leave a Reply

Your email address will not be published. Required fields are marked *