How do you ➡️ access keys & values in an OBJECT? #python

preview_player
Показать описание
Let's say that you want to access the keys & values in an OBJECT. This video shows a way to do that.

In the sample code, an object is created with 2 keys called 'make' and 'year'. In the object creation and assignment, a value is also assigned to each of the keys. The value of each key will be accessed in order to print that value.

The 1st output value will be 'Ford', and the 2nd output value will be '25'.

✨ LEARNING RESOURCES THAT ARE PROJECT BASED ✨

✨ E-BOOK: LEARN ANY PROGRAMMING LANGUAGE IN A DAY ✨

#python #object #keyvaluepair

*** SAMPLE CODE ***
my_car = (
{"make":"Ford","year":25})

print(my_car["make"])
# Ford

print(my_car["year"])
# 25
Рекомендации по теме
Комментарии
Автор

that works but because you put the dictionary in a tuple you can't have more than one item and if you did you would have to put the index then the key

my_car = ({"make":"Ford", "year":25}, {"make":"Sedan", "year":20})

print(my_car[0]["make"]) # Ford
print(my_car[1]["make"]) # Sedan

snakesnuggles
visit shbcf.ru