Skip to content

Commit

Permalink
second version -set
Browse files Browse the repository at this point in the history
  • Loading branch information
Asabeneh committed Jul 7, 2021
1 parent 3b62f23 commit 1a7d228
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
22 changes: 15 additions & 7 deletions 06_Day_Tuples/06_tuples.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
- [Joining Tuples](#joining-tuples)
- [Deleting Tuples](#deleting-tuples)
- [💻 Exercises: Day 6](#-exercises-day-6)
- [Exercises: Level 1](#exercises-level-1)
- [Exercises: Level 2](#exercises-level-2)

# Day 6:

Expand Down Expand Up @@ -223,18 +225,23 @@ del fruits

## 💻 Exercises: Day 6

### Exercises: Level 1

1. Create an empty tuple
2. Create a tuple containing names of your sisters and your brothers (imaginary siblings are fine)
3. Join brothers and sisters tuples and assign it to siblings
4. How many siblings do you have?
5. Modify the siblings tuple and add the name of your father and mother and assign it to family_members
6. Unpack siblings and parents from family_members
7. Create fruits, vegetables and animal products tuples. Join the three tuples and assign it to a variable called food_stuff_tp.
8. Change the about food_stuff_tp tuple to a food_stuff_lt list
9. Slice out the middle item or items from the food_stuff_tp tuple or food_stuff_lt list.
10. Slice out the first three items and the last three items from food_staff_lt list
11. Delete the food_staff_tp tuple completely
12. Check if an item exists in tuple:

### Exercises: Level 2

1. Unpack siblings and parents from family_members
1. Create fruits, vegetables and animal products tuples. Join the three tuples and assign it to a variable called food_stuff_tp.
1. Change the about food_stuff_tp tuple to a food_stuff_lt list
1. Slice out the middle item or items from the food_stuff_tp tuple or food_stuff_lt list.
1. Slice out the first three items and the last three items from food_staff_lt list
1. Delete the food_staff_tp tuple completely
1. Check if an item exists in tuple:

- Check if 'Estonia' is a nordic country
- Check if 'Iceland' is a nordic country
Expand All @@ -243,4 +250,5 @@ del fruits
nordic_countries = ('Denmark', 'Finland','Iceland', 'Norway', 'Sweden')
```


[<< Day 5](../05_Day_Lists/05_lists.md) | [Day 7 >>](../07_Day_Sets/07_sets.md)
54 changes: 33 additions & 21 deletions 07_Day_Sets/07_sets.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

<sub>Author:
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
<small> First Edition: Nov 22 - Dec 22, 2019</small>
<small> Second Edition: July, 2021</small>
</sub>

</div>
</div>

[<< Day 6](../06_Day_Tuples/06_tuples.md) | [Day 8 >>](../08_Day_Dictionaries/08_dictionaries.md)
Expand All @@ -37,16 +36,19 @@
- [Finding Symmetric Difference Between Two Sets](#finding-symmetric-difference-between-two-sets)
- [Joining Sets](#joining-sets-1)
- [💻 Exercises: Day 7](#-exercises-day-7)
- [Exercises: Level 1](#exercises-level-1)
- [Exercises: Level 2](#exercises-level-2)
- [Exercises: Level 3](#exercises-level-3)

# 📘 Day 7

## 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.
Set is a collection of items. 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 un-indexed 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 brackets, {} to create a set.
We use curly brackets, {} to create a set or the *set()* built-in function.

- Creating an empty set

Expand Down Expand Up @@ -94,7 +96,7 @@ We use loops to access items. We will see this in loop section

### Checking an Item

To check if an item exist in a list use use _in_.
To check if an item exist in a list we use _in_ membership operator.

```py
# syntax
Expand All @@ -106,12 +108,12 @@ print("Does set st contain item3? ", 'item3' in st) # Does set st contain item3?

```py
fruits = {'banana', 'orange', 'mango', 'lemon'}
'mango' in fruits
print('mango' in fruits ) # True
```

### Adding Items to a Set

Once a set is created we cannot change any items but we can add additional ones.
Once a set is created we cannot change any items and we can also add additional items.

- Add one item using _add()_

Expand All @@ -129,6 +131,7 @@ fruits.add('lime')
```

- Add multiple items using _update()_
The *update()* allows to add multiple items to a set. The *update()* takes a list argument.

```py
# syntax
Expand Down Expand Up @@ -186,7 +189,7 @@ If we want to delete the set itself we use _del_ operator.
```py
# syntax
st = {'item1', 'item2', 'item3', 'item4'}
del set
del st
```

**Example:**
Expand All @@ -198,7 +201,7 @@ del fruits

### 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.
We can convert list to set and set to list. Converting list to set removes duplicates and only unique items will be reserved.

```py
# syntax
Expand Down Expand Up @@ -338,7 +341,7 @@ It returns the the symmetric difference between two sets. It means that it retur
# syntax
st1 = {'item1', 'item2', 'item3', 'item4'}
st2 = {'item2', 'item3'}
# it means (A\B)∪(B)
# it means (A\B)∪(B\A)
st2.symmetric_difference(st1) # {'item1', 'item4'}
```

Expand Down Expand Up @@ -377,7 +380,7 @@ dragon = {'d', 'r', 'a', 'g', 'o','n'}
python.isdisjoint(dragon) # False, there are common items {'o', 'n'}
```

🌕 You are a rising star . You have just completed day 7 challenges and you are 7 steps ahead in to your way to greatness. Now do some exercises for your brain and for your muscle.
🌕 You are a rising star . You have just completed day 7 challenges and you are 7 steps ahead in to your way to greatness. Now do some exercises for your brain and muscles.

## 💻 Exercises: Day 7

Expand All @@ -389,21 +392,30 @@ B = {19, 22, 20, 25, 26, 24, 28, 27}
age = [22, 19, 24, 25, 26, 24, 25, 24]
```

### Exercises: Level 1

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
8. Is A subset of B
9. Are A and B disjoint sets
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 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? You did not learn loops yet you can do it manually.

### Exercises: Level 2

1. Join A and B
1. Find A intersection B
1. Is A subset of B
1. Are A and B disjoint sets
1. Join A with B and B with A
1. What is the symmetric difference between A and B
1. Delete the sets completely

### Exercises: Level 3

1. Convert the ages to a set and compare the length of the list and the set, which one is bigger?
1. Explain the difference between the following data types: string, list, tuple and set
2. _I am a teacher and I love to inspire and teach people._ How many unique words have been used in the sentence? Use the split methods and set to get the unique words.


🎉 CONGRATULATIONS ! 🎉

Expand Down

0 comments on commit 1a7d228

Please sign in to comment.