Python Dictionaries - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Python Dictionaries - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, December 1, 2018

Python Dictionaries

Python Dictionaries



Introduction

Dictionaries store connections between pieces of information. Each item in a dictionary is a key-value pair.

 #A simple dictionary 
alien = {'color': 'green' , 'point': 5}

#Accessing a value
print("The alien's color is " + alien['color'])

#Adding a new key-value pairs
alien ['x_position'] = 0

#looping through all key- value pairs
fav_numbers = {' eric ': 17, 'ever':4}
for name, number in fav_numbers.items():
 print(name + 'loves' + str (number))


#looping through all keys
fav_numbers = {'eric': 17, 'ever' : 4}
for name in fav_numbers.keys():
 print(name  + ' loves a number')


#looping through all the values
fav_numbers = {'eric': 17, 'ever': 4}
for number in fav_numbers.values():
 print(str(number) + ' is a favorite')

Screenshot 

python dictionary

 

No comments:

Post a Comment

Post Top Ad