Python/Python Crash Course

Python Crash Course: 챕터 6

1june 2024. 6. 28. 01:35

Python Crash Course, 3rd Edition

 

  • Dictionary
alien_0 = {'color': 'green', 'points': 5}

Open and close by { } braces, key-value pair is distinguished by : colon

print(alien_0['color'])

How to access 'color' key to get the value

alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)

How to add key-value pair to the dictionary

you can also modify the existing key using the same codeline format above

del alien_0['x_position']
del alien_0['y_position'] 
print(alien_0)

How to remove the key-value pair from the dictionary, del here is permanent

 

 

  • Looping through a dictionary
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}

for key, value in user_0.items():
    print(f"\nKey: {key}")
    print(f"Value: {value}")

key, value is written after for. It can also be like:

for k, v in user_0.items() 

.items() method is used to return a sequence of key-value pairs

 

  • Looping through a dictionary but only accessing keys
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}

for key in user_0.keys():
    print(f"\nKey: {key}")

Use key.() method to access all keys in the dictionary

You can omit key.() method because the codeline below:

for key in user_0:

will give the same output

 

  • Looping through a dictionary but only accessing values
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}

for value in user_0.values():
    print(f"\nValues: {value}")

Use value.() method to access all values in the dictionary

You cannot omit value.() method like key.() method because this method is not a default behavior like the latter.

for value in set(user_0.values()):
    print(f"\nValues: {value}")

*If the values printed duplicate and shows messy output, you can use set() function like above

 

  • Nesting
  • Nesting is used when you want to store multiple dictionaries in a list or a list of items as a value in a dictionary.
alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'yellow', 'points': 10}
alien_2 = {'color': 'red', 'points': 15}

aliens = [alien_0, alien_1, alien_2]

for alien in aliens:
	print(alien)
# Make an empty list for storing aliens.
aliens = []

# Make 15 green aliens and 15 yellow aliens
for alien_number in range(30):
	if alien_number < 15:
    	new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
        
    else:
    	new_alien = {'color': 'green', 'points': 5, 'speed': 'medium'}
        
	aliens.append(new_alien)

 

List could be in a dictionary *

Dictionary could be in a list 

Dictionary could be in a dictionary *

users = {
	'aeinstein': {
	'first': 'albert',
    'last': 'einstein',
'location': 'princeton',} ,

'mcurie': {
'first': 'marie',
'last': 'curie',
'location': 'paris',} ,
		}

'Python > Python Crash Course' 카테고리의 다른 글

Python Crash Course: 챕터 8  (1) 2024.07.01
Python Crash Course: 챕터 7  (0) 2024.06.29
Python Crash Course: 챕터 5  (0) 2024.06.27
Python Crash Course: 챕터 4  (0) 2024.06.26
Python Crash Course: 챕터 3  (0) 2024.06.25