String Basics
Dictionary basics
100

what is the purpose of the function len()?

The function len(), which stands for length(variable), is meant to find the number of characters in a variable. 

For example:                         

hollowing = '31/October'

print(len(hollowing))               output: 10

100

Are dictionary mutable? 

Yes. You can change the content of the key and variable.  

200

What function formatting is used to find a character from a variable by knowing its index number? 

Hint: it is the opposite of len()  

print(variable[index])

For example: 

hollowing = '31/October'

print(hollowing[0:2])                         output: 31

* 0:2 =  [0,2)

200

prices = {'apples': 1.99, 'oranges': 1.49} 

print(f"The price of apples is {prices['kiwi']}")

what is the output?  

Traceback (most recent call last):  File "main.py", line 3, in <module>    print(f"The price of apples is {prices['kiwi']}")                                    ~~~~~~^^^^^^^^ KeyError: 'kiwi'

 If it doesn't exist, then you can't use it. 

300

how can you output a simple string with a variable?

print('string'/ Varble + 'string' / Varble) 

for Example: 

hollowing = '31/October' 

print('what date is hollowing in 2024: ' + hollowing)

output: what date is hollowing in 2024: 31/October 

300

prices = {'apples': 1.99, 'oranges': 1.49} 

del prices ['apples']

what is the output? 

There is no output. we did not have a print statement. 

print(prices)         

output:  {'oranges': 1.49} 

 

400

what does the format string do? And how do you construct one?  


The format string is the same as the + function. It allows you to add variables to strings in the same print statement. 

For Example: 

a = '5'

b = '6'

print(f'The sum of {a} and {b} is {a + b}.')   

print('The sum of ' + a + ' and ' + b + ' is ' + a + b +'.')


a = 'abc'

b = 'def'

print(f'The sum of {a} and {b} is {a + b}.')   

print('The sum of ' + a + ' and ' + b + ' is ' + a + b +'.')

400

what is the syntax of dictionary? 


Hint: You have a variable that is assigned to key(s) and those key(s) are assigned to a value(element).

Varble = {key:value}  

Letters = 'key' or 'value'

Numbers = NO quotes on key or values 

For example:

jet = {'jet1':'f-35', 'jet2':'f-22',52: 35, 53:42,}

print(jet['jet1'],jet[52],jet['jet2'])

output: f-35 35 f-22

500

lists are mutable [you can modify them after they are created]. How can you construct a list using list methods? 

Hint: remove() or pop() = removes an element from                                         a variable 

        append() = add an element to a variable 

        

variable = elements 

print(Varble)

list method.variable(elements)

print(variable)

For example: 

jet = ['f-12', 'f-22']

print(jet)

jet.append('t-99')

print(jet)

*Notice: [] = lists                   {} = sets  

500

How do you access a dictionary entry or a key value using f-string?

 

Hint: Dictionary entries cannot be accessed by indexing. 

variable['key'] 

prices = {'apples': 1.99, 'oranges': 1.49} 

print(f"The price of apples is {prices['apples']}")

output: The price of apples is 1.49

(  ${prices['apples']}.:2f  )  for $1.49