Python 5 Flashcards
What does a list look like?
a = [35, 18, 69]
print (a)
[35, 18, 69]
If it is a string
b = [‘mouse’, ‘cat’, ‘dog’]
print (b)
[‘mouse’,’cat’,’dog’]
How to:
-concatenate 2 lists
-count total length of list
-append a list
- Use +
ex:[35, 18, 69] + [35, 18, 69]
[35, 18, 69,35, 18, 69]
-Use len()
ex: a = [35, 18, 69]
len(a)
3
Use append() (Can only be added to the end of the list)
a = [35, 18, 69]
a.append(30)
a = [35, 18, 69, 30]
How to:
-Insert an object in an index position
-Delete an object in a list
-Delete an object in index position
-Remove and return last object
-Removes and returns last object with index
-Use .insert()
ex: friends = [‘Mary’, ‘Ben’]
friends.insert(1, ‘Adam’)
print (friends)
[‘Mary’,’Adam’,’Ben’]
-Use .remove()
ex: friends = [‘Mary’, ‘Ben’]
friends.remove(‘Ben’)
print (friends)
[‘Mary]
-Use del syntax
ex: friends = [‘Mary’, ‘Adam’, ‘Ben’]
del letters[1]
print (friends)
[‘Mary’, ‘Ben]
-Use .pop()
ex: friends = [‘Mary’, ‘Ben’]
last = friends.pop()
print (last, friends)
‘Ben’ [‘Mary’]
-Use pop with index
ex: friends = [‘Mary’, ‘Adam’, ‘Ben’]
second = friend.pop(1)
print (friends)
‘Adam’ [‘Mary’, ‘Ben’]
With numbers no quotes
How to sort objects: a=[‘d’, ‘j’, ‘a’, ‘b’, ‘c’, ‘f’]
-In order
-In reverse
-Use .sort()
a=[‘d’, ‘j’, ‘a’, ‘b’, ‘c’, ‘f’]
a.sort()
print(a)
[‘a’, ‘b’, ‘c’, ‘d’, ‘f’, ‘j’]
-Use .reverse()
print(a)
[‘j’, ‘f’, ‘d’, ‘c’, ‘b’, ‘a’]
What are other methods other than .sort()? to sort
a=[5, 2, 3, 1, 4]
-sorted() function
a=[5, 2, 3, 1, 4]
b = sortted (a)
print (b)
[1, 2, 3, 4 , 5]
How to search a list to find something and how to return the lowest index in list that object appears?
-Use in syntax
»>a=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
»> ‘a’ in a
True
»> ‘z’ in a
False
-Use .index()
»>a=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
»> a.index(‘b’)
1
»> a.index(‘f’)
Value error
How to make a loop using list or strings?
-Use the for syntax
»>string1 = ‘123456’
»>for x in string 1:
print (x)
1
2
3
…
list1 = [1, 2, 3, 5, 5, 6]
for x in list1:
print (x)
1
2
3
…
Important notes:Dont forget semi colon after for statement as forgetting will lead to syntax error and 4 spaces before print as it is indentation
How to convert range object to list? and make range from 1-10 while squaring each value
-Use list() to convert to list
ex: list(range(5))
for i in range(10)
print(i**2)
0
1
4
9
16
25
36
49
64
81
What is the notation of a dictionary
d={key1 : value1, key2 : value2}
How to use the dict class?
Use 23 as key Ben as name
James as key Old as name
Stephan as key 58 as name
> > > dict1 = {23 : ‘Ben’,’James’ : ‘old’, ‘Stephan’ : 58}
dict2 = dict{‘key1’:’value1’,key2’:’value2’,’key3’:value3}
dict1
{‘Stephan’:58, ‘James’: ‘old’, 23: ‘Ben’)
How to add key-value pairs and how to retrieve them?
Name variable age
Ben key 29 value
Mary key 18 value
Stephan key 58 value
> > > age = {}
age[‘Ben’] = 29
age[‘Mary] = 18
age[‘Stephan] = 58
age
[‘Stephan’ : 58, ‘ not sure ask nila
To access you can use:
-age[‘Ben’]
29
or use .get(key)
age.get(‘Mary’)
How to:
Ben key 29 value
Mary key 18 value
Stephan key 58 value
-
Update dictionary
-Add new entry
-Delete dictionary elements
Update dicitonary:
-{‘Ben’:29, ‘Mary’:18, ‘Stephan’:58}
-Use age[‘Ben’]=40
Add new entry:
-age=[‘Tom’]=30 or age.update({‘Robert’:15})
Delete dictionary elements:
del age [‘Ben’] or to delete the entire dictionary: del age
How to:
Ben key 29 value
Mary key 18 value
Stephan key 58 value
-Count number of entries
-Return all keys
-Return all values
-Retrun a list of keys and values as tuple pairs
-Looping keys
{‘Ben’:29, ‘Mary’:18, ‘Stephan’:58}
-Use len(age)
-Use age.keys()
-Use age.values()
-Use age.items()
-Use for key in age
print(age)