Python for Kids: A fun and engaging introduction to programming concepts using Python by Praba Rajah

Python for Kids: A fun and engaging introduction to programming concepts using Python by Praba Rajah

Author:Praba Rajah [Rajah, Praba]
Language: eng
Format: epub
Published: 2024-05-03T00:00:00+00:00


Here, we created a dictionary called character_info that stores information about a character. Each key ("name", "age", "special_ability") acts as a label for its corresponding value ("Alice", 10, "flying").

Accessing and Modifying Values:

We access values in a dictionary using their keys within square brackets. We can also modify existing values or add new key-value pairs:

Python

character_info[ "age" ] = 11 # Update age

character_info[ "hair_color" ] = "brown" # Add a new key-value pair

Common Dictionary Operations:

Dictionaries offer various methods to manage their contents:

Checking for keys: Use the in operator to check if a specific key exists in the dictionary.

Removing key-value pairs: Use the pop() method to remove a key-value pair and return its value.

Looping through dictionaries: Use a for loop to iterate over the keys or key-value pairs in the dictionary.

Python

if "hair_color" in character_info:

print( "Hair color:" , character_info[ "hair_color" ])

favorite_color = character_info.pop( "hair_color" ) # Remove hair_color and store its value

for key in character_info:

print(key, ":" , character_info[key])



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.