Anna Syme

Click name ↑ to return to homepage

General python things

list

mylist=[] #make a list
print("here is my empty list")
print(mylist)

mylist.append('coffee') #add an item
print("added coffee")
print(mylist)

dictionary

mydictionary={} #make a dictionary
print("here is my empty dictionary")
print(mydictionary)

print("now adding key and value to dictionary")
mydictionary["Emergency"]=999
#adds name (as the key) and number (as the value)

print("what is value at key Emergency")
print(mydictionary["Emergency"]) #prints the number

print("loop through keys and print value")
for key in mydictionary:
  print(key) #prints name
  print(mydictionary[key]) #prints number

remove spaces in string

mystring='   some text here   '
mystring
mystring.strip() #removes all leading/trailing whitespaces
mystring.rstrip() #just trailing whitespaces
mystring.lstrip() #just leading whitespaces
nextstring='000 more text 000'
nextstring.strip("0") #removes 0s

true and false