Strings and Dictionaries Flashcards

1
Q

Python allows us to concatenate using what operator?

A

+

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

This is super useful for taking you from one big string to a list of words

A

str.split()
[‘Pluto’, ‘is’, ‘a’, ‘planet!’]

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

Turns a string into a list of smaller strings, breaking on whitespace by default

A

str.split()

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

This puts a list of strings up into one long string, using the string it was called on as a separator

A

str.join()

‘/‘.join([month, day, year])
‘01/31/1956’

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

In a “format string” where the Python values we want to insert are represented by what?

A

{} curly braces

“{}, you’ll always be the {}th planet to me. “.format(planet, position)

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

Built-in Python data structure for mapping keys to values

A

What are dictionaries

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

The in operator tells us whether something is a what?

A

A key in the dictionary

‘Saturn’ in planet_to_initial

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

How can you access a collection of all the keys or all the values?

A

dict.keys() and dict.values()

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

Get all the initials, sort them alphabetically, and put them in a space-separated string.

A

‘ ‘.join(sorted(planet_to_initial.values()))

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

What does an item refer to?

A

A key value pair

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

Returns the string right justified in a string of length width

A

rjust()

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

def is_valid_zip(zip_code):
“””Return whether the input string is a valid (5 digit) zip code”””

A

return len(zip_code) == 5 and zip_code.isdigit()

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

What does (str.isdigit) do?

A

Returns True if string is digit, False otherwise

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

How do you create a dictionary?

A

{‘key1’:’value’,’key2’:123} curly braces

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

How would you grab a value in a key?

d = {‘key1’:’value’,’key2’:123}

A

d[‘key1’]

You have to call the actual key

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

What is this?

d = {‘k1’:{‘innerkey’:[1,2,3]}}

A

A list inside of a list

17
Q

Do dictionaries retain any order?

A

No they’re just key value pair mappings

18
Q

In Python, a dict is an (blank blank) of items.

A

Unordered collection

19
Q

A nested dict is a what?

A

A dictionary inside of a dictionary

20
Q

How would you access elements of a nested dictionary?

A

Indexing [ ]