Introduction to Python Flashcards
Name = {key: value, key: value}
Dictionary
dictionary[key] = value
How to add an item to a dictionary or overwrite an existing value
dictionary.update({key:value, key:value })
Add multiple items to a dictionary at once
Py: How to overwrite an existing value associated to a key in a dictionary
Using the single add method to a key with the same name will overwrite it.
dictionary[key] = value
dictionary = {key: value for key, value in zip(varkey, varvalue)}
Create a dictionary using comprehension
zip(iterator1a, iterator2a, …)
Takes multiple iterators (list) inputs and returns as a single list of tuples
my_dict.get(key)
How to safely retrieve a key from a dictionary (won’t return an error if not there)
Can add an optional default return value if not in the dictionary by (key, value)
my_dict.pop(key, default return)
Safely checked for key and removes from dictionary if true. Default return is optional
list(my_dict)
Returns a list of all dictionary keys
my_dict.keys()
Can be used (And by iteration) to return a viewable (list) key objects
my _dict.values()
Return a list of values much like dict_keys
my_dict.items()
.keys() and .values() combined returning elements of tuples
with open(‘your doc.txt’) as a_doc:
some_contents = a_doc.read()
print(some_contents)
Print contents of a text file
with open(‘your doc.txt’) as a_doc:
for line in a_doc.readlines ():
print(line)
Print a single line from a text doc
with open(‘your doc.txt’) as a_doc:
first_line = a_doc.readline ()
second _line = a_doc.readline()
print(second_line)
Extract just a single line from
open(‘a_file.txt’, ‘w’) as some_file:
some_file.write(‘some text’)
Open a file in write mode - default is r for read
‘a’ to append
json.load(file_variable)
Read a json file variable after ‘with open() as’
json.dump(input_dict, file_var)
Write to a json file from a dictionary and into an “with open(‘file_var’, ‘w’)” called
(a,b,)
if type my_tuple == tuple:
c, d = tuple
Checks if a tuple then assigns to independent variables. You have to do entire contents of tuple at same time
.py: return a,b
returns as a tuple
Implied by the comma, can use () if you wish
.strip()
.lstrip()
.rstrip()
Remove white space from a variable (str)
.removeprefix(‘https://‘)
Remove a prefix from a string - in this case part of the URL but could be ‘Mr/£/$ etc’
.removesuffix(‘.py’)
Ideal for removing file types or similar. In this case .py
py: add something to the end of a list
.append(something)
py: remove something from a list
.remove(something)
py: combine lists (use to add multiple items)
new_list = list1 + list2
py: add a single item to a list other than .append
my_list + [item]
pi: my_list.remove(item)
Remove specified item from a list. If used on a list with duplicates it’ll will remove the first instance.
.sort()
permanent sorting of a list
Reverse it by .sort(reverse=True)
.sorted()
Presentational (temp) sorting of a list
.reverse()
Reverse the order permanently
But you can always repeat to back
py: for value in range (1, 5):
print(value)
Returns 1-4 due to zero indexing
could’ve also just given it range(5) for the same result
A 3rd unit would provide a range increment
py: variable = list(input)
Convert the input into a list
py: min(list)
max(list)
sum(list)
Quick mathematics on list inputs
py:
list1 = list2[:]
How to copy a list (as opposed to just sharing it)