<jupyter_start><jupyter_text>Python Basic Assignment - 5<jupyter_code># 1. What does an empty dictionary's code look like?
# Empty dictionary
emptyDict = {}
type(emptyDict)
# 2. What is the value of a dictionary value with the key 'foo' and the value 42?
# The value will be 42
my_dict = {'foo': 42}
my_dict['foo']
# 3. What is the most significant distinction between a dictionary and a list?
# The dictionary is created using {} or dict()
dict3 = {'k1': 123, 'k2': 2+3j}

# The list is created using [] or list()
list3 = [12,23,45]
# 4. What happens if you try to access spam['foo'] if spam is {'bar': 100}?
# It will throw an error

spam = {'bar': 100}
spam['foo']
# 5. If a dictionary is stored in spam, 
# what is the difference between the expressions 'cat' in spam and 'cat' in spam.keys()?
# There is no difference
# 'cat' in spam: This expression checks whether there is a 'cat' KEY in the dictionary
# 'cat' in spam.keys(): This will checks whether there is a key 'cat' in spam.

dict5 = {'k1': 123, 'k2': 2+3j}
a = 'k1' in dict5
b = 'k1' in dict5.keys()
print(a, b)
# 6. If a dictionary is stored in spam,
# what is the difference between the expressions 'cat' in spam and 'cat' in spam.values()?

# Difference:
# 'cat' in spam: This expression checks whether there is a 'cat' KEY in the dictionary
# 'cat' in spam.values(): This will checks whether there is a value 'cat' in spam.

spam = {'cat': 123, 'k2': 2+3j}
a = 'cat' in spam
b = 'cat' in spam.values()
print(a, b)
# 7. What is a shortcut for the following code?
# if 'color' not in spam:
# spam['color'] = 'black'

spam
# shortcut code 
spam.setdefault('color', 'black')
spam
# 8. How do you "pretty print" dictionary values using which module and function?
# By importing pprint and using pprint.pprint(dict_name)

import pprint
pprint.pprint(spam)<jupyter_output>{'cat': 123, 'color': 'black', 'k2': (2+3j)}
{'fraction_non_alphanumeric': 0.13099041533546327, 'fraction_numerical': 0.03194888178913738, 'mean_word_length': 4.292957746478873, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '629159', 'license': 'MIT', 'n_tokens_mistral': 699, 'n_tokens_neox': 632, 'n_words': 286, 'path': 'Python Basic/Python Basic Assignment - 5.ipynb', 'repo_name': 'Sayan97/Python', 'size': '1878'}