Introduction to Python Flashcards

1
Q

Name = {key: value, key: value}

A

Dictionary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

dictionary[key] = value

A

How to add an item to a dictionary or overwrite an existing value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

dictionary.update({key:value, key:value })

A

Add multiple items to a dictionary at once

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Py: How to overwrite an existing value associated to a key in a dictionary

A

Using the single add method to a key with the same name will overwrite it.
dictionary[key] = value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

dictionary = {key: value for key, value in zip(varkey, varvalue)}

A

Create a dictionary using comprehension

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

zip(iterator1a, iterator2a, …)

A

Takes multiple iterators (list) inputs and returns as a single list of tuples

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

my_dict.get(key)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

my_dict.pop(key, default return)

A

Safely checked for key and removes from dictionary if true. Default return is optional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

list(my_dict)

A

Returns a list of all dictionary keys

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

my_dict.keys()

A

Can be used (And by iteration) to return a viewable (list) key objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

my _dict.values()

A

Return a list of values much like dict_keys

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

my_dict.items()

A

.keys() and .values() combined returning elements of tuples

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

with open(‘your doc.txt’) as a_doc:
some_contents = a_doc.read()
print(some_contents)

A

Print contents of a text file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

with open(‘your doc.txt’) as a_doc:
for line in a_doc.readlines ():
print(line)

A

Print a single line from a text doc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

with open(‘your doc.txt’) as a_doc:
first_line = a_doc.readline ()
second _line = a_doc.readline()
print(second_line)

A

Extract just a single line from

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

open(‘a_file.txt’, ‘w’) as some_file:
some_file.write(‘some text’)

A

Open a file in write mode - default is r for read
‘a’ to append

17
Q

json.load(file_variable)

A

Read a json file variable after ‘with open() as’

18
Q

json.dump(input_dict, file_var)

A

Write to a json file from a dictionary and into an “with open(‘file_var’, ‘w’)” called

19
Q

(a,b,)
if type my_tuple == tuple:
c, d = tuple

A

Checks if a tuple then assigns to independent variables. You have to do entire contents of tuple at same time

20
Q

.py: return a,b

A

returns as a tuple
Implied by the comma, can use () if you wish

21
Q

.strip()
.lstrip()
.rstrip()

A

Remove white space from a variable (str)

22
Q

.removeprefix(‘https://‘)

A

Remove a prefix from a string - in this case part of the URL but could be ‘Mr/£/$ etc’

23
Q

.removesuffix(‘.py’)

A

Ideal for removing file types or similar. In this case .py

24
Q

py: add something to the end of a list

A

.append(something)

25
Q

py: remove something from a list

A

.remove(something)

26
Q

py: combine lists (use to add multiple items)

A

new_list = list1 + list2

27
Q

py: add a single item to a list other than .append

A

my_list + [item]

28
Q

pi: my_list.remove(item)

A

Remove specified item from a list. If used on a list with duplicates it’ll will remove the first instance.

29
Q

.sort()

A

permanent sorting of a list
Reverse it by .sort(reverse=True)

30
Q

.sorted()

A

Presentational (temp) sorting of a list

31
Q

.reverse()

A

Reverse the order permanently
But you can always repeat to back

32
Q

py: for value in range (1, 5):
print(value)

A

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

33
Q

py: variable = list(input)

A

Convert the input into a list

34
Q

py: min(list)
max(list)
sum(list)

A

Quick mathematics on list inputs

35
Q

py:
list1 = list2[:]

A

How to copy a list (as opposed to just sharing it)