Python Cheat Sheet Page 2

ADVERTISEMENT

x not in s 
if item x is not found in item s returns True 
s[i] 
returns item in position i of s 
s[i:j] 
returns slice of s from position i, up to but not including position j 
s[i:j:k] 
returns slice of s as above, in steps of k 
s.count(x) 
returns number of instances of x in s 
s[i] = x 
item i in s is replaced with x 
s[i:j] = t 
items in range i to j of s are replaced with t (t must be same 
length 
as slice) 
del s[i:j] 
delete items in s range i to j 
s.append(x) 
appends x to the end of s 
s.clear() 
removes all instances from s 
s.insert(i,x) 
inserts x into s at position i 
s.pop() 
removes item from s 
s.remove() 
removes the first instance in s 
  
Lists 
list[ ] 
create new empty list 
list[1,2,3] 
create list populated with 1, 2 & 3 
 
Strings 
str.capitalize() 
returns string with first letter capitalised 
str.join(x,y,z) 
returns concatenation of strings x, y & z 
str.lower() 
returns the string in lowercase 
str.upper() 
returns the string in uppercase 
str.lstrip() 
returns string with leading input removed 
str.rstrip() 
returns string with trailing input removed 
str.strip() 
returns string with all input removed 
  
Dictionaries 
dict() 
creates new empty dictionary 
dict(‘one’:1, ‘two:2’) 
creates new populated dictionary including ‘key’:value 
d[key] 
returns value for key in d 
d[key] = value 
sets value for key in d 
del d[key] 
removes value for key in d 
key in d 
returns True if key found in d 
key not in d 
returns True if key not found in d 
clear() 
remove all items from dictionary 
get(key) 
return value for key 
  
Loops 
for i in x 
loops through items in x 
if 
action if returns True, if False continue to elif or else 

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 3