6. Strings and dictionaries Flashcards

1
Q
x = 'Pluto is a planet'
y = "Pluto is a planet"
x == y

True or False?

A

True

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

The following returns an error. How to fix it?

'Pluto's a planet!'

A

'Pluto\'s a planet!'

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

How would you do hello world on two separate lines?

Define variable and then print, using double quotes

A
hello = "hello\nworld"
print(hello)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to do hello world on two separate lines, using triple quotes?

A
triplequoted_hello = """hello
world"""
print(triplequoted_hello)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you do 2 separate print statments with words on the same line?

A
print("hello", end='')
print("pluto", end='')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How would you slice the last 3 letters of this word?
planet = 'Pluto'

A

planet[-3:]

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

How would you turn the following into a list of 5 items, one for each character, each one accompanied by an exclamation mark?
planet = 'Pluto'

A

[char+'! ' for char in planet]

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

What’s the result of this? and why?
planet[0] = 'B'

A

TypeError
B/c strings are immutable!

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

How would you turn this into upper case?
claim = "Pluto is a planet!"

A

claim.upper()

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

How would you find the position of “uto” in the following…

claim = "Pluto is a planet!"
A

claim.index('uto')

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

How do you create a list of four items from the following?

claim = "Pluto is a planet!"
A

words = claim.split()

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

How would you split the following into 3 separate variables:

datestr = '1956-01-31'

output variables should be year, month, day

A
datestr = '1956-01-31'
split_date = datestr.split(sep = "-") 
year, month, day = split_date
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Join year, month, day into one string, separated by “/”

A

’/’.join([year,month,day])

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

How do you concantenate “what do you” with b = “mean” with “?”

A

d = “what do you” + b + “?”

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

b = 10
How would you say that “I am 10 years old”

Using str()

A

c = "I am " + str(b) + " years old"

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

b = 10
How would you say that “I am 10 years old”

Using .format method

A

c = "I am {} years old".format(b)

17
Q

What will the following produce?

planet = "Jupiter"
pluto_mass = 1.303 * 10**22
earth_mass = 5.9722 * 10**24
population = 52910390

"{} weighs about {:,.0f} kilograms ({:.3%} of Earth's mass). It is home to {:,} Plutonians.".format(
    planet, pluto_mass, pluto_mass / earth_mass, population,
)
A

‘Jupiter weighs about 13,029,999,999,999,998,427,136 kilograms (0.218% of Earth’s mass). It is home to 52,910,390 Plutonians.’

18
Q

What does this produce?

# Referring to format() arguments by index, starting from 0
s = """Pluto's a {0}.
No, it's a {1}.
{0}!
{1}!""".format('planet', 'dwarf planet')
print(s)
A

Pluto’s a planet.
No, it’s a dwarf planet.
planet!
dwarf planet!

19
Q

How do you set up a dictionary with “one” as key and 1 as value, for first 3 numbers?

A

numbers = {'one':1, 'two':2, 'three':3}

20
Q

How do you access the 2 value from the following?
numbers = {'one':1, 'two':2, 'three':3}

A

numbers['two']

21
Q

How do you append ‘eleven’ as a key to the following dictionary?
numbers = {'one':1, 'two':2, 'three':3}

A
numbers['eleven'] = 11
22
Q

How do you change value 1 to “chulent”
numbers = {'one':1, 'two':2, 'three':3}

A

numbers[‘one’] = ‘chulent’

23
Q

How do you produce the following dictionary from a list of planets?

{'Mercury': 'M',
 'Venus': 'V',
 'Earth': 'E',
 'Mars': 'M',
 'Jupiter': 'J',
 'Saturn': 'S',
 'Uranus': 'U',
 'Neptune': 'N'}

Using a for loop

A
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
planet_to_initial = {planet: planet[0] for planet in planets}
planet_to_initial
24
Q

How do you access the keys, values, and items in a dictionary (“dict”)?

A
dict.keys
dicts.values
dict.items