Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: python-variables

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: python-variables
Showing posts with label python-variables. Show all posts
Showing posts with label python-variables. Show all posts

Python Modifying Values

Python Modifying Values



Modifying values

You can modify the value associated with any key in a dictionary. To do so give the name of the dictionary and enclose the key in sqaure brackets, then provide the new value for that key.

#Modifying values in a dictionary
alien_0 = {'color': 'green', 'points':5}
print(alien_0)

#change the aliein's color and point value.
alien_0['color'] = 'yellow'
alien_0['points'] = 10
print(alien_0)

Screenshot 

 

Python Removing key-value pairs

Python Removing key-value pairs




Removing key-value pairs

You can remove any key-value pair you want from a dictionary. To do so use the del keywords and dictionary name, followed by the key in square brackets. This will delete the key and its associated value.

#Deleting a key-value pair
alien_0 = {'color': 'green', 'points': 5}
print(alien_0)

del alien_0['points']
print(alien_0)