Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today: Let’s dive into Python "Dictionaries", the toolbox for storing data with labels and values. If you are an absolute beginner you can glance through previous issues as well
Think of Python Dictionaries like a real world dictionary: you look up a word (key) and find its meaning (value). Data is basically stored as key-value pair. Use when you want to connect a label (like a name) to information (like a score, age, etc.).
🚀 Create a dictionary and explore its elements/items Cat, Dog are keys (animal). Meow, Bark are values (sounds)
In [1]:
my_dict = {
"cat": "meow",
"dog": "bark"
}
print(my_dict)
Out [1]: {'cat': 'meow', 'dog': 'bark'}
💳 Access value (bark) using the key (dog)
In [2]:
print(my_dict["dog"])
Out [2]: bark
Change value (meow ➡️ purr) of a key
In [3]:
my_dict["cat"] = "purr"
print(my_dict)
Out [3]: {'cat': 'purr', 'dog': 'bark'}
✅ Super easy to add a new key value pair
In [4]:
# Add a new key-value pair
my_dict["cow"] = "moo" print(my_dict)
Out [4]: {'cat': 'purr', 'dog': 'bark', 'cow': 'moo'}
Likewise super easy to delete ❌ key value pair
In [5]:
del my_dict["cow"]
print(my_dict)
Out [5]: {'cat': 'purr', 'dog': 'bark'}
🧠 To get keys, values or all items ➡️ use "list", yes the one we went through yesterday
In [6]:
# This was not the output we needed
my_dict.keys
Out [6]:
In [7]:
print(list(my_dict.keys()))
Out [7]: ['cat', 'dog']
In [8]:
print(list(my_dict.values()))
Out [8]: ['purr', 'bark']
In [9]:
print(list(my_dict.items()))
Out [9]: [('cat', 'purr'), ('dog', 'bark')]
💡 To access values, you can also use "get" function. But why? You'll know
In [10]:
print(my_dict.get("cat"))
Out [10]: purr
❌🛡️ "lion" is not a key, so traditional access method will throw error which can be avoided using "get"
In [11]:
print(my_dict["lion"])
KeyError: 'lion'
In [12]:
print(my_dict.get("lion"))
Out [12]: None
🦒 Length of dictionary is number of keys (or values)
In [13]:
print(len(my_dict))
Out [13]: 2
❓Quiz time: What do we use to confirm the datatype
In [14]:
print(type(my_dict))
Out [14]:
🗑️ Empty everything from the dictionary
In [15]:
my_dict.clear()
print(my_dict)
Out [15]: {}
🚀 💪 Mega "list" containing multiple dictionaries
In [16]:
people = [
{"name": "Mike", "age": 15}, {"name": "Sarah", "age": 21} ] print(people)
Out [16]: [{'name': 'Mike', 'age': 15}, {'name': 'Sarah', 'age': 21}]
🪐🚨Lets print everything together. Also see which datatype is for what!!
In [17]:
print(people) print(type(people)) print(people[0]) print(type(people[0]))
Out [17]:
[{'name': 'Mike', 'age': 15}, {'name': 'Sarah', 'age': 21}] {'name': 'Mike', 'age': 15} 🎺🎺 Its a wrap for today’s Python adventure—go ahead and play with what you’ve learned! Have a fantastic weekend! ⭐📣 If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼
|
Beginner to Expert in Python in just 5 minutes
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today: Get hands-on with Python lists — master storing and managing of multiple items in one place. If you are an absolute beginner you can glance through previous issues as well A "list" is a datatype that can store multiple values as one variable. Yes you can stuff numbers, strings, anything together 🔍 What does it look like? Reminder: print() is often used to display output In [1]: # List is...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today: Dive into Python strings — learn how to create, manipulate, and explore them. "String" is any sequence of characters (letters, numbers, symbols etc.) in quotes. Can be represented in single ☝️ or double quotes ✌️ In [1]: # String in single quotes 'I love Pandas Daily' Out [1]: 'I love Pandas Daily' In [2]: # String in double quotes "I love Pandas Daily" Out [2]: 'I love Pandas Daily' ↩️ Slight...
Welcome to the very first issue of Pandas Daily — your 5 minute daily dose of Python. Today: Understand some of Python’s data types with examples and clean output. “Data Types” help the computer understand what you're working with—numbers, text, or something else. Common Data Types 1. Numbers Reminder: As seen below, "In [1]" is the code input we will type in and "Out [1]" is the output of code once executed In [1]: # Integers (anything starting with '#' is a comment and not executed) 8 Out...