Snabb referens

Lists
my_list = [4, 3, 2, 1]
print(my_list[3])  # slicing my_list[start:end+1]

print(my_list[0:2])
#[4, 3]

my_list.append(5) # add value to list

print(my_list.pop(0)) # get the first value and remove it from the list
print(my_list)

my_list.remove(3) # removes the value 3
print(my_list)

my_list.insert(1, 100) # inserts value 100 in place 1
print(my_list)

my_list.clear() # clear the list to an empty list
print(my_list)

print(my_list.index(1)) # when i want to know the index of value 1

print(my_list.count(1)) # counts how many values of 1 in my_list

my_list.sort() # sorting the list
print(my_list)

 

Dictionaries
# dictionaires are a data structure which are indexed by keys
# Keys can be anything, from strings to integers and objects
# syntax to declare an empty dictionary in python is {}

my_dict = {‘school’:’definition of school’,’coding’:’definition of coding’,’python’:’definition of python’}
print(my_dict)

print(my_dict[‘school’]) # keys are unique

my_dict[‘pen’] = ‘definition of pen’  # how to add a key and value to my_dict

for key,value in my_dict.items(): # how to print out each key and value
print(key,’   ‘,value)

 

30-days-of-python