Python for Beginners Cheat Sheet - GitHub Pages
Python for Beginners ? Cheat Sheet
Data types and Collections
Numerical Operators
Comparison Operators
integer float boolean string list tuple set dictionary
Operations
Strings: s[i] s[-1]
10 3.14 True/False `abcde' [1, 2, 3, 4, 5] (1, 2, `a', `b') {`a', 'b', 'c'} {`a':1, `b':2}
i:th item of s last item of s
+ addition
-
subtraction
*
multiplication
/
division
** exponent
% modulus
// floor division
Index starts at 0
<
less
greater
>= greater or equal
== equal
!= not equal
Logical Operators
and
logical AND
or
logical OR
not
logical NOT
Membership Operators
Lists: l = [] l[i:j] l[i] = x l[i:j:k]
define empty list slice in range i to j replace i with x slice range i to j, step k
in
value in object
not in value not in object
Conditional Statements
if condition:
Dictionaries:
d = {}
create empty dictionary
d[i]
retrieve item with key i
d[i] = x
store x to key I
i in d
is key i in dictionary
elif condition:
else:
List Methods
l.append(x) l.insert(I, x) l.remove(x) l.reverse()
append x to end of list insert x at position i remove first occurrence of x reverse list in place
Dictionary Methods
d.keys() d.values() d.items()
returns a list of keys returns a list of values returns a list of (key, value)
String Methods
s.strip()
remove trailing whitespace
s.split(x)
return list, delimiter x
s.join(l)
return string, delimiter s
s.startswith(x) return True if s starts with x
s.endswith(x) return True if s ends with x
s.upper()
return copy, uppercase only
s.lower()
return copy, lowercase only
Import from Module
from module import func
import func
from module import func as f import func as f
Python for Beginners ? Cheat Sheet
Built-in Functions
float(x) int(x) str(x) set(x) type(x) len(x) max(x) min(x) sum(x) sorted(x) round(x, d) print(x)
convert x to float convert x to integer convert x to string convert x to set returns type of x returns length of x returns maximum of x returns minimum of x returns sum of values in x returns sorted list returns x rounded to d print object x
Loops
while condition:
for var in list:
Control statements:
break
terminate loop
continue
jump to next iteration
pass
does nothing
String Formatting
Reading and Writing Files
"Put {}into a {}".format("values", "string") `Put values into a string'
"Put whitespace after: {:10}".format("a","b")
`Put whitespace after: a
, or before:
b'
fh = open(,'r') for line in fh:
fh.close()
"Put whitespace around:{:^10}.".format("c")
`Put whitespace around: c
.
Regular Expressions
out = open(,'w') out.write() out.close()
import re
p = pile(pattern) compile search query
p.search(text)
search for all matches
p.sub(sub, text)
substitute match with sub
. * + ? \d \s [abc] [^abc]
any one character repeat previous 0 or more times repeat previous 1 or more times repeat previous 0 or 1 times any digit any whitespace any character in this set {a, b, c} any character *not* in this set
Functions
def Name(param1, param2 = val): #param2 optional, default: val return
sys.argv
import sys sys.argv[0] sys.argv[1]
import module name of script first cmd line arg
[a-z] a|b
any letter between a and z a or b
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- lab manual python programming lab 6cs4 23 jnit
- python basics amazon web services inc
- s˝ˆˇ˘ ncert
- python lists university of michigan
- combining latex with python tex users group tug
- data transformation with cheat sheet github
- introduction to programming in python strings university of texas
- programs on list python class room diary
- p r o g r a m m i n g w i t h p y t h o n university of manchester
- programs list for class xii cs
Related searches
- excel for beginners cheat sheet
- python cheat sheet pdf
- python functions cheat sheet pdf
- python cheat sheet class
- python cheat sheet pdf basics
- python cheat sheet for beginners
- learning python for beginners pdf
- beginners python cheat sheet pdf
- python cheat sheet download
- python 3 7 cheat sheet pdf
- python for beginners pdf
- accounting for beginners cheat sheet