mirror of
https://github.com/Asabeneh/30-Days-Of-Python.git
synced 2026-06-12 21:01:48 +08:00
some changes
This commit is contained in:
parent
11849a63f4
commit
4178fab7bd
@ -25,15 +25,15 @@
|
||||
- [Getting Set's Length](#getting-sets-length)
|
||||
- [Accessing Items in a Set](#accessing-items-in-a-set)
|
||||
- [Checking an Item](#checking-an-item)
|
||||
- [Adding Items to a List](#adding-items-to-a-list)
|
||||
- [Removing Items from a List](#removing-items-from-a-list)
|
||||
- [Adding Items to a Set](#adding-items-to-a-set)
|
||||
- [Removing Items from a Set](#removing-items-from-a-set)
|
||||
- [Clearing Items in a Set](#clearing-item-in-a-set)
|
||||
- [Deleting a Set](#deleting-a-set)
|
||||
- [Converting List to a Set](#converting-list-to-a-set)
|
||||
- [Joining Sets](#joining-sets)
|
||||
- [Finding Intersection Items](#finding-intersection-items)
|
||||
- [Checking Subsets and Super Sets](#checking-subsets-and-super-sets)
|
||||
- [Checking Difference Between Two Sets](#checking-difference-between-two-sets)
|
||||
- [Checking the Difference Between Two Sets](#checking-the-difference-between-two-sets)
|
||||
- [Finding Symmetric Difference Between Two Sets](#finding-symmetric-difference-between-two-sets)
|
||||
- [Joining Sets](#joining-sets)
|
||||
- [💻 Exercises: Day 7](#%f0%9f%92%bb-exercises-day-7)
|
||||
@ -42,11 +42,11 @@
|
||||
|
||||
## Set
|
||||
|
||||
Let me take you back to your elementary or high school Mathematics lesson. The Mathematics definition of set can be applied also in python. Set is a collection of unordered and unindexed distinct elements. In python set uses to store unique items, and it is possible to find the _union_, _intersection_, _difference_, _symmetric difference_, _subset_, _super set_ and _disjoint set_ among sets.
|
||||
Let me take you back to your elementary or high school Mathematics lesson. The Mathematics definition of a set can be applied also in python. Set is a collection of unordered and unindexed distinct elements. In python set is used to store unique items, and it is possible to find the _union_, _intersection_, _difference_, _symmetric difference_, _subset_, _super set_ and _disjoint set_ among sets.
|
||||
|
||||
### Creating a Set
|
||||
|
||||
We use curly bracket, {} to create a set.
|
||||
We use curly brackets, {} to create a set.
|
||||
|
||||
- Creating an empty set
|
||||
|
||||
@ -88,18 +88,18 @@ fruits = {'banana', 'orange', 'mango', 'lemon'}
|
||||
len(fruits)
|
||||
```
|
||||
|
||||
### Accessing Items in set
|
||||
### Accessing Items in a Set
|
||||
|
||||
We use loops to access items. We will see this in loop section
|
||||
|
||||
### Checking an item
|
||||
### Checking an Item
|
||||
|
||||
To check if an item exist in a list use use _in_.
|
||||
|
||||
```py
|
||||
# syntax
|
||||
st = {'item1', 'item2', 'item3', 'item4'}
|
||||
'item3' in st
|
||||
print("Does set st contain item3? ", 'item3' in st) # Does set st contain item3? True
|
||||
```
|
||||
|
||||
**Example:**
|
||||
@ -109,9 +109,9 @@ fruits = {'banana', 'orange', 'mango', 'lemon'}
|
||||
'mango' in fruits
|
||||
```
|
||||
|
||||
### Adding items to a list
|
||||
### Adding Items to a Set
|
||||
|
||||
Once a list is created we can not change an item but we can add additional items.
|
||||
Once a set is created we cannot change any items but we can add additional ones.
|
||||
|
||||
- Add one item using _add()_
|
||||
|
||||
@ -128,7 +128,7 @@ fruits = {'banana', 'orange', 'mango', 'lemon'}
|
||||
fruits.add('lime')
|
||||
```
|
||||
|
||||
- Add multiple items or using _update()_
|
||||
- Add multiple items using _update()_
|
||||
|
||||
```py
|
||||
# syntax
|
||||
@ -140,28 +140,28 @@ st.update(['item5','item6','item7'])
|
||||
|
||||
```py
|
||||
fruits = {'banana', 'orange', 'mango', 'lemon'}
|
||||
vegetables = ('Tomato', 'Potato', 'Cabbage','Onion', 'Carrot')
|
||||
vegetables = ('tomato', 'potato', 'cabbage','onion', 'carrot')
|
||||
fruits.update(vegetables)
|
||||
```
|
||||
|
||||
### Removing item from a list
|
||||
### Removing Items from a Set
|
||||
|
||||
We can remove an item from a list using _remove()_ method. If the item is not found _remove()_ method raise an errors, so it is good to check if the item exist or not. However, \*discard() method doesn't raise an error.
|
||||
We can remove an item from a set using _remove()_ method. If the item is not found _remove()_ method will raise errors, so it is good to check if the item exist in the given set. However, _discard()_ method doesn't raise any errors.
|
||||
|
||||
```py
|
||||
# syntax
|
||||
st = {'item1', 'item2', 'item3', 'item4'}
|
||||
st.remove('item2")
|
||||
st.remove('item2')
|
||||
```
|
||||
|
||||
**Example:**
|
||||
|
||||
```py
|
||||
fruits = {'banana', 'orange', 'mango', 'lemon'}
|
||||
fruits.pop()
|
||||
fruits.pop() # removes the last element from the set
|
||||
```
|
||||
|
||||
### Clearing item in a set
|
||||
### Clearing Items in a Set
|
||||
|
||||
If we want to clear or empty the set we use _clear_ method.
|
||||
|
||||
@ -176,9 +176,10 @@ st.clear()
|
||||
```py
|
||||
fruits = {'banana', 'orange', 'mango', 'lemon'}
|
||||
fruits.clear()
|
||||
print(fruits) # set()
|
||||
```
|
||||
|
||||
### Deleting a set
|
||||
### Deleting a Set
|
||||
|
||||
If we want to delete the set itself we use _del_ operator.
|
||||
|
||||
@ -195,14 +196,14 @@ fruits = {'banana', 'orange', 'mango', 'lemon'}
|
||||
del fruits
|
||||
```
|
||||
|
||||
### Converting list to set
|
||||
### Converting List to Set
|
||||
|
||||
We can convert list to set and set to list back. Converting list to set removes duplicates and only unique items will be reserved.
|
||||
|
||||
```py
|
||||
# syntax
|
||||
lst = ['item1', 'item2', 'item3', 'item4', 'item1']
|
||||
st = set(lst) # {'item2', 'item4', 'item1', 'item3'}
|
||||
st = set(lst) # {'item2', 'item4', 'item1', 'item3'} - the order is random, because sets in general are unordered
|
||||
```
|
||||
|
||||
**Example:**
|
||||
@ -212,9 +213,9 @@ fruits = ['banana', 'orange', 'mango', 'lemon','orange', 'banana']
|
||||
fruits = set(fruits) # {'mango', 'lemon', 'banana', 'orange'}
|
||||
```
|
||||
|
||||
### Joining sets
|
||||
### Joining Sets
|
||||
|
||||
We can join two using the _union()_ or \*update() method.
|
||||
We can join two sets using the _union()_ or _update()_ method.
|
||||
|
||||
- Union
|
||||
This method returns a new set
|
||||
@ -230,30 +231,30 @@ st3 = st1.union(st2)
|
||||
|
||||
```py
|
||||
fruits = {'banana', 'orange', 'mango', 'lemon'}
|
||||
vegetables = {'Tomato', 'Potato', 'Cabbage','Onion', 'Carrot'}
|
||||
fruits.union(vegetables) # {'lemon', 'Carrot', 'Tomato', 'banana', 'mango', 'orange', 'Cabbage', 'Potato', 'Onion'}
|
||||
vegetables = {'tomato', 'potato', 'cabbage','onion', 'carrot'}
|
||||
print(fruits.union(vegetables)) # {'lemon', 'carrot', 'tomato', 'banana', 'mango', 'orange', 'cabbage', 'potato', 'onion'}
|
||||
```
|
||||
|
||||
- Update
|
||||
This method insert an other set
|
||||
This method inserts a set into a given set
|
||||
|
||||
```py
|
||||
# syntax
|
||||
st1 = {'item1', 'item2', 'item3', 'item4'}
|
||||
st2 = {'item5', 'item6', 'item7', 'item8'}
|
||||
st1.update(st2)
|
||||
st1.update(st2) # st2 contents are added to st1
|
||||
```
|
||||
|
||||
**Example:**
|
||||
|
||||
```py
|
||||
fruits = {'banana', 'orange', 'mango', 'lemon'}
|
||||
vegetables = {'Tomato', 'Potato', 'Cabbage','Onion', 'Carrot'}
|
||||
vegetables = {'tomato', 'potato', 'cabbage','onion', 'carrot'}
|
||||
fruits.update(vegetables)
|
||||
print(fruits) # {'lemon', 'Carrot', 'Tomato', 'banana', 'mango', 'orange', 'Cabbage', 'Potato', 'Onion'}
|
||||
print(fruits) # {'lemon', 'carrot', 'tomato', 'banana', 'mango', 'orange', 'cabbage', 'potato', 'onion'}
|
||||
```
|
||||
|
||||
### Finding intersection items
|
||||
### Finding Intersection Items
|
||||
|
||||
Intersection returns a set of items which are in both the sets. See the example
|
||||
|
||||
@ -267,16 +268,16 @@ st1.intersection(st2) # {'item3', 'item2'}
|
||||
**Example:**
|
||||
|
||||
```py
|
||||
whole_numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 10}
|
||||
whole_numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
even_numbers = {0, 2, 4, 6, 8, 10}
|
||||
whole_numbers.intersection(even_numbers) # {0, 2, 4, 6, 8, 10}
|
||||
|
||||
python = {'p', 'y', 't', 'o','n'}
|
||||
python = {'p', 'y', 't', 'h', 'o','n'}
|
||||
dragon = {'d', 'r', 'a', 'g', 'o','n'}
|
||||
python.intersection(dragon) # {'o', 'n'}
|
||||
```
|
||||
|
||||
### Checking subset and super set
|
||||
### Checking Subset and Super Set
|
||||
|
||||
A set can be a subset or super set of other sets:
|
||||
|
||||
@ -296,23 +297,24 @@ st1.issuperset(st2) # True
|
||||
```py
|
||||
whole_numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
even_numbers = {0, 2, 4, 6, 8, 10}
|
||||
whole_numbers.issubset(even_numbers) # False, because it is super set
|
||||
whole_numbers.issubset(even_numbers) # False, because it is a super set
|
||||
whole_numbers.issuperset(even_numbers) # True
|
||||
|
||||
python = {'p', 'y', 't', 'o','n'}
|
||||
python = {'p', 'y', 't', 'h', 'o','n'}
|
||||
dragon = {'d', 'r', 'a', 'g', 'o','n'}
|
||||
python.issubset(dragon) # False
|
||||
```
|
||||
|
||||
### Checking difference between two sets
|
||||
### Checking the Difference Between Two Sets
|
||||
|
||||
It return the difference between the two sets.
|
||||
It returns the difference between two sets.
|
||||
|
||||
```py
|
||||
# syntax
|
||||
st1 = {'item1', 'item2', 'item3', 'item4'}
|
||||
st2 = {'item2', 'item3'}
|
||||
st2.difference(st1) # {'item1', 'item4'} => st1\st2
|
||||
st2.difference(st1) # set()
|
||||
st1.difference(st2) # {'item1', 'item4'} => st1\st2
|
||||
```
|
||||
|
||||
**Example:**
|
||||
@ -320,23 +322,23 @@ st2.difference(st1) # {'item1', 'item4'} => st1\st2
|
||||
```py
|
||||
whole_numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
even_numbers = {0, 2, 4, 6, 8, 10}
|
||||
whole_numbers.difference(even_numbers) # {1, 3, 5, 7}
|
||||
whole_numbers.difference(even_numbers) # {1, 3, 5, 7, 9}
|
||||
|
||||
python = {'p', 'y', 't', 'o','n'}
|
||||
dragon = {'d', 'r', 'a', 'g', 'o','n'}
|
||||
python.difference(dragon) # {'p', 'y', 't'}
|
||||
python.difference(dragon) # {'p', 'y', 't'} - the result is unordered (characteristic of sets)
|
||||
dragon.difference(python) # {'d', 'r', 'a', 'g'}
|
||||
```
|
||||
|
||||
### Finding Symmetric difference between two sets
|
||||
### Finding Symmetric Difference Between Two Sets
|
||||
|
||||
It return the the symmetric difference between the two sets, it means that it return a set that contains all items from both sets, except items that are present in both set, mathematically: (A\B) U (B\A)
|
||||
It returns the the symmetric difference between two sets. It means that it returns a set that contains all items from both sets, except items that are present in both sets, mathematically: (A\B) ∪ (B\A)
|
||||
|
||||
```py
|
||||
# syntax
|
||||
st1 = {'item1', 'item2', 'item3', 'item4'}
|
||||
st2 = {'item2', 'item3'}
|
||||
# it mean (A\B)U(B)
|
||||
# it means (A\B)∪(B)
|
||||
st2.symmetric_difference(st1) # {'item1', 'item4'}
|
||||
```
|
||||
|
||||
@ -344,17 +346,17 @@ st2.symmetric_difference(st1) # {'item1', 'item4'}
|
||||
|
||||
```py
|
||||
whole_numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
even_numbers = {1, 2, 3, 4, 5}
|
||||
whole_numbers.symmetric_difference(even_numbers) # {0, 6, 7, 8, 9, 10}
|
||||
some_numbers = {1, 2, 3, 4, 5}
|
||||
whole_numbers.symmetric_difference(some_numbers) # {0, 6, 7, 8, 9, 10}
|
||||
|
||||
python = {'p', 'y', 't', 'o','n'}
|
||||
python = {'p', 'y', 't', 'h', 'o','n'}
|
||||
dragon = {'d', 'r', 'a', 'g', 'o','n'}
|
||||
python.symmetric_difference(dragon) # {'r', 't', 'p', 'y', 'g', 'a', 'd'}
|
||||
python.symmetric_difference(dragon) # {'r', 't', 'p', 'y', 'g', 'a', 'd', 'h'}
|
||||
```
|
||||
|
||||
### Joining set
|
||||
### Joining Sets
|
||||
|
||||
If two set do not have common item or items we call it disjoint set. We can check if two sets are joint or disjoint using _isdisjoint()_ method.
|
||||
If two sets do not have a common item or items we call them disjoint sets. We can check if two sets are joint or disjoint using _isdisjoint()_ method.
|
||||
|
||||
```py
|
||||
# syntax
|
||||
@ -370,9 +372,9 @@ even_numbers = {0, 2, 4 ,6, 8}
|
||||
even_numbers = {1, 3, 5, 7, 9}
|
||||
even_numbers.isdisjoint(odd_numbers) # True, because no common item
|
||||
|
||||
python = {'p', 'y', 't', 'o','n'}
|
||||
python = {'p', 'y', 't', 'h', 'o','n'}
|
||||
dragon = {'d', 'r', 'a', 'g', 'o','n'}
|
||||
python.disjoint(dragon) # False, there is common items {'o', 'n'}
|
||||
python.disjoint(dragon) # False, there are common items {'o', 'n'}
|
||||
```
|
||||
|
||||
## 💻 Exercises: Day 7
|
||||
@ -385,10 +387,10 @@ B = {19, 22, 20, 25, 26, 24, 28, 27}
|
||||
age = [22, 19, 24, 25, 26, 24, 25, 24]
|
||||
```
|
||||
|
||||
1. Find the length of the set, it_companies
|
||||
2. Add 'Twitter' to it companies
|
||||
3. Insert multiple it companies at once to the set, it_companies
|
||||
4. Remove one of the companies from the set, it_companies
|
||||
1. Find the length of the set it_companies
|
||||
2. Add 'Twitter' to it_companies
|
||||
3. Insert multiple IT companies at once to the set it_companies
|
||||
4. Remove one of the companies from the set it_companies
|
||||
5. What is the difference between remove and discard
|
||||
6. Join A and B
|
||||
7. Find A intersection B
|
||||
@ -397,9 +399,9 @@ age = [22, 19, 24, 25, 26, 24, 25, 24]
|
||||
10. Join A with B and B with A
|
||||
11. What is the symmetric difference between A and B
|
||||
12. Delete the sets completely
|
||||
13. Convert the ages to set and compare the length of the list and the set, which is larger ?
|
||||
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.
|
||||
13. Convert the ages to a set and compare the length of the list and the set, which one is bigger?
|
||||
14. Explain the difference between 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?
|
||||
|
||||
🎉 CONGRATULATIONS ! 🎉
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user