mirror of
https://github.com/Asabeneh/30-Days-Of-Python.git
synced 2026-06-12 21:01:48 +08:00
day 8
This commit is contained in:
parent
14d4f0f6f7
commit
761dc115c3
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,5 +2,6 @@ readme-draft.md
|
||||
readme13-18.md
|
||||
readme19-24.md
|
||||
readme25-30.md
|
||||
playground.py
|
||||
backup.md
|
||||
.DS_Store
|
||||
@ -1,5 +1,4 @@
|
||||
[Part 1](https://github.com/Asabeneh/30-Days-Of-Python) | [Part 2](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-12.md)| [Part 3](#)| [Part 4](#)| [Part 5](#)
|
||||
***
|
||||
[Part 1](https://github.com/Asabeneh/30-Days-Of-Python) | [Part 2](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme4-6.md)| [Part 3](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-9.md)| [Part 4](#)| [Part 5](#)
|
||||
|
||||

|
||||
- [Day 1](#day-1)
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
[Part 1](https://github.com/Asabeneh/30-Days-Of-Python) | [Part 2](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-12.md)| [Part 3](#)| [Part 4](#)| [Part 5](#)
|
||||
***
|
||||
[Part 1](https://github.com/Asabeneh/30-Days-Of-Python) | [Part 2](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme4-6.md)| [Part 3](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-9.md)| [Part 4](#)| [Part 5](#)
|
||||

|
||||
- [Day 7](#day-7)
|
||||
- [Set](#set)
|
||||
@ -19,6 +18,22 @@
|
||||
- [Finding Symmetric difference between two sets](#finding-symmetric-difference-between-two-sets)
|
||||
- [Joining set](#joining-set)
|
||||
- [Exercises: Day 7](#exercises-day-7)
|
||||
- [Day 8](#day-8)
|
||||
- [Dictionary](#dictionary)
|
||||
- [Creating a dictionary](#creating-a-dictionary)
|
||||
- [Accessing a dictionary items](#accessing-a-dictionary-items)
|
||||
- [Dictionary Length](#dictionary-length)
|
||||
- [Adding Item to a dictionary](#adding-item-to-a-dictionary)
|
||||
- [Modifying Item in a dictionary](#modifying-item-in-a-dictionary)
|
||||
- [Checking a key in a dictionary](#checking-a-key-in-a-dictionary)
|
||||
- [Removing key items from dictionary](#removing-key-items-from-dictionary)
|
||||
- [Changing dictionary to list items](#changing-dictionary-to-list-items)
|
||||
- [Changing dictionary to list items](#changing-dictionary-to-list-items-1)
|
||||
- [Deleting dictionary](#deleting-dictionary)
|
||||
- [Copy a dictionary](#copy-a-dictionary)
|
||||
- [Getting dictionary keys as list](#getting-dictionary-keys-as-list)
|
||||
- [Getting dictionary values as list](#getting-dictionary-values-as-list)
|
||||
- [Exercises: Day 8](#exercises-day-8)
|
||||
|
||||
# Day 7
|
||||
## Set
|
||||
@ -308,5 +323,282 @@ age = [22, 19, 24, 25, 26, 24, 25, 24]
|
||||
14. Explain the difference among the following data types: string, list, tuple and set
|
||||
15. *I am a teacher and I love to inspire and teach people.* How many unique words have been used in the sentence.
|
||||
|
||||
# Day 8
|
||||
## Dictionary
|
||||
A dictionary is a collection of unordered, modifiable(mutable) key value paired data type.
|
||||
### Creating a dictionary
|
||||
To create a dictionary we use a curly bracket, {}.
|
||||
```py
|
||||
# syntax
|
||||
empty_dict = {}
|
||||
# Dictionary with data values
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
```
|
||||
**Example:**
|
||||
```py
|
||||
person = {
|
||||
'first_name':'Asabeneh',
|
||||
'last_name':'Yetayeh',
|
||||
'age':250,
|
||||
'country':'Finland',
|
||||
'is_marred':True,
|
||||
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python']
|
||||
'address':{
|
||||
'street':'Space street',
|
||||
'zipcode':02210
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The dictionary above shows that a value could be any different data type:string, boolean, list, tuple, set or a dictionary.
|
||||
|
||||
### Accessing a dictionary items
|
||||
We can access a dictionary items by referring to its key name.
|
||||
```py
|
||||
# syntax
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
print(dct['key1']) # item1
|
||||
print(dct['key4']) # item4
|
||||
```
|
||||
**Example:**
|
||||
```py
|
||||
person = {
|
||||
'first_name':'Asabeneh',
|
||||
'last_name':'Yetayeh',
|
||||
'age':250,
|
||||
'country':'Finland',
|
||||
'is_marred':True,
|
||||
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
|
||||
'address':{
|
||||
'street':'Space street',
|
||||
'zipcode':02210
|
||||
}
|
||||
}
|
||||
print(person['first_name']) # Asabeneh
|
||||
print(person['country']) # Finland
|
||||
print(person['skills']) # ['HTML','CSS','JavaScript', 'React', 'Node', 'MongoDB', 'Python']
|
||||
print(person['city']) # Error
|
||||
```
|
||||
Accessing an item by key name raises an error if the key does not exist. To avoid this error first we have to check if a key exist or we can use the _get_ method.The get method returns None, which is a NoneType object data type.
|
||||
object if the data
|
||||
```py
|
||||
person = {
|
||||
'first_name':'Asabeneh',
|
||||
'last_name':'Yetayeh',
|
||||
'age':250,
|
||||
'country':'Finland',
|
||||
'is_marred':True,
|
||||
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
|
||||
'address':{
|
||||
'street':'Space street',
|
||||
'zipcode':02210
|
||||
}
|
||||
}
|
||||
print(person.get('first_name')) # Asabeneh
|
||||
print(person.get('country')) # Finland
|
||||
print(person.get('skills')) #['HTML','CSS','JavaScript', 'React', 'Node', 'MongoDB', 'Python']
|
||||
print(person.get('city')) # None
|
||||
```
|
||||
### Dictionary Length
|
||||
It checks the number of key value pairs in the dictionary.
|
||||
```py
|
||||
# syntax
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
print(len(dct)) # 4
|
||||
```
|
||||
**Example:**
|
||||
```py
|
||||
person = {
|
||||
'first_name':'Asabeneh',
|
||||
'last_name':'Yetayeh',
|
||||
'age':250,
|
||||
'country':'Finland',
|
||||
'is_marred':True,
|
||||
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
|
||||
'address':{
|
||||
'street':'Space street',
|
||||
'zipcode':02210
|
||||
}
|
||||
}
|
||||
print(len(person)) # 7
|
||||
|
||||
```
|
||||
|
||||
### Adding Item to a dictionary
|
||||
|
||||
We can add new key and value pain to a dictionary
|
||||
|
||||
```py
|
||||
# syntax
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
dct['key5'] = 'item5'
|
||||
|
||||
```
|
||||
**Example:**
|
||||
|
||||
```py
|
||||
person = {
|
||||
'first_name':'Asabeneh',
|
||||
'last_name':'Yetayeh',
|
||||
'age':250,
|
||||
'country':'Finland',
|
||||
'is_marred':True,
|
||||
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
|
||||
'address':{
|
||||
'street':'Space street',
|
||||
'zipcode':'02210'
|
||||
}
|
||||
}
|
||||
person['job_title'] = 'Instructor'
|
||||
person['skills'].append('HTML')
|
||||
print(person)
|
||||
```
|
||||
|
||||
### Modifying Item in a dictionary
|
||||
We can add modify item in a dictionary
|
||||
```py
|
||||
# syntax
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
dct['key1'] = 'item-one'
|
||||
```
|
||||
**Example:**
|
||||
```py
|
||||
person = {
|
||||
'first_name':'Asabeneh',
|
||||
'last_name':'Yetayeh',
|
||||
'age':250,
|
||||
'country':'Finland',
|
||||
'is_marred':True,
|
||||
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
|
||||
'address':{
|
||||
'street':'Space street',
|
||||
'zipcode':'02210'
|
||||
}
|
||||
}
|
||||
person['first_name'] = 'Eyob'
|
||||
person['age']
|
||||
```
|
||||
|
||||
### Checking a key in a dictionary
|
||||
|
||||
We use the _in_ operator to check if a key exist in a dictionary
|
||||
|
||||
```py
|
||||
# syntax
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
print('key2' in dct) # True
|
||||
print('key5' in dct) # False
|
||||
```
|
||||
|
||||
### Removing key items from dictionary
|
||||
|
||||
- _pop(key)_: removes the item with the specified key name:
|
||||
- _popitem()_: remove the last time
|
||||
- _del_: removes the item with the specified key name
|
||||
|
||||
```py
|
||||
# syntax
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
dct.pop('key1') # the first key pair removed
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
dct.popitem() # remove the last item
|
||||
del dct['key2'] # remove key 2 item
|
||||
```
|
||||
|
||||
**Example:**
|
||||
|
||||
```py
|
||||
person = {
|
||||
'first_name':'Asabeneh',
|
||||
'last_name':'Yetayeh',
|
||||
'age':250,
|
||||
'country':'Finland',
|
||||
'is_marred':True,
|
||||
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
|
||||
'address':{
|
||||
'street':'Space street',
|
||||
'zipcode':'02210'
|
||||
}
|
||||
}
|
||||
person.pop('first_name') # Remove the firstname item
|
||||
person.popitem() # Remove the lastname item
|
||||
del person['is_married'] # Remove the is married
|
||||
```
|
||||
|
||||
### Changing dictionary to list items
|
||||
|
||||
The \*items() method change a dictionary to list of tuples.
|
||||
|
||||
```py
|
||||
# syntax
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
print(dct.items()) # dict_items([('key1', 'item1'), ('key2', 'item2'), ('key3', 'item3'), ('key4', 'item4')])
|
||||
```
|
||||
|
||||
### Changing dictionary to list items
|
||||
|
||||
If we don't want the items in a dictionary we can clear them using _clear()_ methods
|
||||
|
||||
```py
|
||||
# syntax
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
print(dct.clear()) # {}
|
||||
```
|
||||
|
||||
### Deleting dictionary
|
||||
|
||||
If we do not use the dictionary we can delete it completely
|
||||
|
||||
```py
|
||||
# syntax
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
del dct
|
||||
```
|
||||
|
||||
### Copy a dictionary
|
||||
|
||||
We copy a dictionary using a _copy()_ method. Using copy we can avoid mutation of the original dictionary.
|
||||
|
||||
```py
|
||||
# syntax
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
dct_copy = dct.copy() # {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
```
|
||||
|
||||
### Getting dictionary keys as list
|
||||
|
||||
The _keys()_ method gives us all the keys of a a dictionary as a list.
|
||||
|
||||
```py
|
||||
# syntax
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
keys = dct.keys()
|
||||
print(keys) # dict_keys(['key1', 'key2', 'key3', 'key4'])
|
||||
```
|
||||
|
||||
### Getting dictionary values as list
|
||||
|
||||
The _values_ method gives us all the values of a a dictionary as a list.
|
||||
|
||||
```py
|
||||
# syntax
|
||||
dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
values = dct.values()
|
||||
print(values) # dict_values(['item1', 'item2', 'item3', 'item4'])
|
||||
```
|
||||
|
||||
## Exercises: Day 8
|
||||
1. Create a an empty dictionary called dog
|
||||
2. Add name, color, breed, legs, age to the dog, dictionary
|
||||
3. Create a student dictionary and add first_name, last_name, gender, age, marital status, skills, country, city and address as key for the dictionary
|
||||
4. Get the length of student dictionary
|
||||
5. Get the value of skills and check the data type, it should be list
|
||||
6. Modify the skills value by adding one or two skills
|
||||
7. Get the dictionary keys as list
|
||||
8. Get the dictionary values as list
|
||||
9. Change the dictionary to a list of tuples using *items() method
|
||||
10. Delete one of the item in the dictionary
|
||||
11. Delete the dictionary completely
|
||||
|
||||
[<< Part 1 ](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme.md)| [Part 3 >>](#)
|
||||
***
|
||||
1116
reame4-6.md
Normal file
1116
reame4-6.md
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user