Section 3: Python Object and Data Structure Basics Flashcards

1
Q

What does “Formatted String Literals (f-strings)” look like:

A

name = ‘Fred’

print(f”He said his name is {name}.”)

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

What do Lists look like?

A
# Assign a list to an variable named my_list
my_list = [1,2,3]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to find the number of objects in a List?

A

len(my_list)

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

How do you slice a list?

A

my_list = [‘one’, ‘two’, ‘three’, 4, 5]

# Grab index 1 and everything past it
my_list[1:]
# Grab everything UP TO index 3
my_list[:3]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you add an item to a List?

A
# Append
list1.append('append me!')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you remove the last item in a list and store it in a variable?

A
# Assign the popped element, remember default popped index is -1
popped_item = list1.pop()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you reverse the items in a List?

A
# Use reverse to reverse order (this is permanent!)
new_list.reverse()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to sort a List (so that the sort is permanent, done in place) ?

A
# Use sort to sort the list (in this case alphabetical order, but for numbers it will go ascending)
new_list.sort()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you nest Lists?

A

Let’s make three lists
lst_1=[1,2,3]
lst_2=[4,5,6]
lst_3=[7,8,9]

# Make a list of lists to form a matrix
matrix = [lst_1,lst_2,lst_3]

Show
matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

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

What does List Comprehensions look like?

A
# Show matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Build a list comprehension by deconstructing a for loop within a []
first_col = [row[0] for row in matrix]

first_col
[1, 4, 7]

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

What does a dictionary look like”

A
# Make a dictionary with {} and : to signify a key and a value
my_dict = {'key1':'value1','key2':'value2'}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do call a value with a dictionary?

A
# Call values by their key
my_dict['key2']

produces:
value2

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

How would you subtract from a value in a dictionary?

A
# Subtract 123 from the value
my_dict['key1'] = my_dict['key1'] - 123
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you add a new key/value pair to a dictionary?

A
# Create a new key through assignment
d['animal'] = 'Dog'
# Can do this with any object
d['answer'] = 42
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you create a tuple?

A
# Create a tuple
t = (1,2,3)
# Can also mix object types
t = ('one',2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

For Tuples, how do you find the index of a value?

A
# Use .index to enter a value and return the index
t.index('one')
17
Q

For a Tuple, how can you count the number times a value appears?

A
# Use .count to count the number of times a value appears
t.count('one')
18
Q

Are Tuples Immutable or Mutable

A

Immutable

Because of this immutability, tuples can’t grow. Once a tuple is made we can not add to it.

19
Q

What is a Set?

A

Sets are an unordered collection of unique elements. We can construct them by using the set() function.

20
Q

How do you create an empty set?

A

x = set()

21
Q

How can you add to a Set?

A
# We add to sets with the add() method
x.add(1)
22
Q

What do Sets and Dictionaries have in common?

A

curly brackets { }

23
Q

What happens if you convert a list with repeats into a set?

A

there will be no repeats in a set:

# Create a list with repeats
list1 = [1,1,2,2,3,4,5,6,1,1]
# Cast as set to get unique values
set(list1)

{1, 2, 3, 4, 5, 6}

24
Q

How can you quickly write a file in Jupyter Notebook?

A

This function is specific to jupyter notebooks!

%%writefile test.txt
Hello, this is a quick test file.

25
Q

How do you open a file in python?

A

myfile = open(‘whoops.txt’)

It is very easy to get an error on this step:
To avoid this error,make sure your .txt file is saved in the same location as your notebook, to check your notebook location, use pwd:

26
Q

How to read a file”

A
First:
# Open the text.txt we made earlier
my_file = open('test.txt')
# We can now read the file
my_file.read()
27
Q

What happens if you try to reread a file?

A
# But what happens if we try to read it again?
my_file.read()

You get => “” (an empty string)

This happens because you can imagine the reading “cursor” is at the end of the file after having read it. So there is nothing left to read. We can reset the “cursor” like this:

28
Q

How can we reset a file that has already been read?

A
# Seek to the start of file (index 0)
my_file.seek(0)
29
Q

How do you read a file line by line?

A

Readlines returns a list of the lines in the file
my_file.seek(0)
my_file.readlines()

30
Q

How do you close a file once you are finished with it?

A

my_file.close()

31
Q

By default, the open() function will only ….

A

allow us to read the file. We need to pass the argument ‘w’ to write over the file.

32
Q

How can you write to a file?

A
By default, the open() function will only allow us to read the file. We need to pass the argument 'w' to write over the file. For example:
   # Add a second argument to the function, 'w' which stands for write.
   # Passing 'w+' lets us read and write to the file

my_file = open(‘test.txt’, ‘w+’)

33
Q

Opening a file with ‘w’ or ‘w+’ will ….

A

truncates the original, meaning that anything that was in the original file is deleted!

34
Q

To append to a file with Jupyter:

A

%%writefile -a test.txt

This is text being appended to test.txt
And another line here.

35
Q

How do you append to a file in Python?

A

Passing the argument ‘a’ opens the file and puts the pointer at the end, so anything written is appended. Like ‘w+’, ‘a+’ lets us read and write to a file. If the file does not exist, one will be created.

my_file = open(‘test.txt’,’a+’)
my_file.write(‘\nThis is text being appended to test.txt’)
my_file.write(‘\nAnd another line here.’)

36
Q

How can you iternate through a file:

A

for line in open(‘test.txt’):

print(line)