--- title: Introduction description: duration: 200 card_type: cue_card --- # Tuples + Sets (2 hours) [Class Recording](https://www.scaler.com/meetings/i/beginner-tuples-sets-5/archive) [Lecture Notes](https://scaler-production-new.s3.ap-southeast-1.amazonaws.com/attachments/attachments/000/015/715/original/Sets_and_Tuples.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIDNNIRGHAQUQRWYA%2F20221206%2Fap-southeast-1%2Fs3%2Faws4_request&X-Amz-Date=20221206T044406Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=9984a7f3003e448901026f2df9906e6f84c68861aae981daf123b2c89946065c) ## Content 1. Motivation behind tuples 2. How to define a tuple 3. Important properties of tuples 4. Sets 5. Set methods 6. Set operations ### Meanwhile when people are joining (5-7 mins) - Instructor's welcome. - Breaking ice with students. - Casual conversation to bond. --- title: Motivation behind tuples description: duration: 700 card_type: cue_card --- ### Motivation behind tuples - A tuple is sort of like an immutable list. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/039/973/original/tuple_immutable_list.png?1689578905" width=300 height=175> \ Code: ``` python= # Can change values of elements in a list a = [1, 2, 3, 4, 5] a[0] = 10 a ``` >Output: ``` [10, 2, 3, 4, 5] ``` Code: ``` python= # Cannot change values a = (1, 2, 3, 4, 5) a[0] = 10 a ``` >Output: ``` --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-38f47f4ea3ce> in <module> 1 # Cannot change values 2 a = (1, 2, 3, 4, 5) ----> 3 a[0] = 10 4 a TypeError: 'tuple' object does not support item assignment ``` #### An example: <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/039/975/original/isro_planets.png?1689579420" width=600 height=150> - Let's say you work at ISRO - You are keeping a list of planets. - This list for most of our lifetimes will remain constant. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/039/976/original/planet_0.png?1689579506" width=600 height=150> - Now a intern comes in and modifies the name of the first planet to **Bipin's planet**. - We would require data like this to be immutable. --- title: How to create a tuple? description: duration: 400 card_type: cue_card --- ### How to create a tuple? - The notation to create tuples is same as lists but instead of **square** brackets we use **parentheses**. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/039/977/original/how_tuple.png?1689579824" width=400 height=200> - We can do **positive indexing**, **negative indexing** on tuples. - We can even **slice** tuples. Code: ``` python= a = (1, 2, 3, 4, 5, 6, 7, 8, 9, 0) a[2:8] ``` >Output: ``` (3, 4, 5, 6, 7, 8) ``` Code: ``` python= a[-2:-8] ``` >Output: ``` () ``` --- title: Some important properties of tuples description: duration: 1800 card_type: cue_card --- ### Some important properties of tuples #### Defining tuples with a single element Code: ``` python= # This will be a tuple t = () type(t) ``` >Output: ``` tuple ``` Code: ``` python= # This will be a tuple t = (1, 2, 3, 4) type(t) ``` >Output: ``` tuple ``` Code: ``` python= # This will not be a tuple t = (45) type(t) ``` >Output: ``` int ``` Code: ``` python= # To define a single element tuple in python, we add a comma at the end of that element. t = (45,) type(t) ``` >Output: ``` tuple ``` #### Typecasting tuples Code: ``` python= a = [1, 2, 3, 4, 5] b = tuple(a) print(a) # List print(b) # Tuple ``` >Output: ``` [1, 2, 3, 4, 5] (1, 2, 3, 4, 5) ``` #### Packing and unpacking in tuples <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/039/995/original/unpacking.png?1689586246" width=500 height=150> \ Code: ``` python= a = (1, 2, 3) # We are allowed to do the following x, y, z = a ``` Code: ``` python= print(x) print(y) print(z) ``` >Output: ``` 1 2 3 ``` Code: ``` python= # Error x, y = (1, 2, 3) ``` >Output: ``` --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-14-d748d0b0a859> in <module> 1 # Error ----> 2 x, y = (1, 2, 3) ValueError: too many values to unpack (expected 2) ``` Code: ```python= # Error x, y, z, m = (1, 2, 3) ``` >Output: ``` --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-15-585d41b5f86e> in <module> 1 # Error ----> 2 x, y, z, m = (1, 2, 3) ValueError: not enough values to unpack (expected 4, got 3) ``` Code: ```python= # Python uses tuples in it's internal implementation def foo(): return 200, 300, 400 bar = foo() print(bar) # Tuple ``` >Output: ``` (200, 300, 400) ``` Code: ``` python a, b, c = foo() print(a) print(b) print(c) ``` >Output: ``` 200 300 400 ``` #### Iterating over a list of tuples Code: ``` python= a = [(1, "Bipin"), (2, "Ranjan"), (3, "Shanoor"), (4, "Shital"), (5, "Tharoor"), ] ``` Code: ``` python= # prints all the tuples for i in a: print(i) ``` >Output: ``` (1, 'Bipin') (2, 'Ranjan') (3, 'Shanoor') (4, 'Shital') (5, 'Tharoor') ``` Code: ``` python= # We can also do something like for roll_no, name in a: print(f"Roll Number - {roll_no} is {name}!") ``` Output: ``` Roll Number - 1 is Bipin! Roll Number - 2 is Ranjan! Roll Number - 3 is Shanoor! Roll Number - 4 is Shital! Roll Number - 5 is Tharoor! ``` --- title: Question - 1 description: duration: 450 card_type: cue_card --- ### Question-1 - Take two elements as input. - Swap the values of these two variables. Code: ``` python= a = 5 b = 10 # First method temp = a a = b b = temp print(a) print(b) ``` >Output: ``` 10 5 ``` Code: ``` python= a = 5 b = 10 # Second method b = a + b # 15 a = b - a # 10 b = b - a # 5 print(a) print(b) ``` >Output: ``` 10 5 ``` Code: ``` python= # Third method a = 5 b = 10 a, b = b, a print(a) print(b) ``` >Output: ``` 10 5 ``` --- title: Quiz-1 description: duration: 60 card_type: quiz_card --- # Question What will be the output of the following code snippet? ``` python= x = (1, 2, 3) y = (4, 5, 6) z = x + y print(len(z)) ``` # Choices - [ ] 3 - [x] 6 - [ ] 9 - [ ] Error --- title: Sets description: duration: 500 card_type: cue_card --- ### Sets - Sets are unique collection of elements. - We represent a set using curly braces `{ }` - Sets can store strings, tuples, booleans inside them all at once. - Sets cannot store Lists, Sets and dictionaries within them. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/039/997/original/set.png?1689589548" width=600 height=125> \ Code: ``` python= s = {1, 2, 3, 4, 5, 5, 5, 5, 4, 4, 4, 7, 7, 7} s ``` >Output: ``` {1, 2, 3, 4, 5, 7} ``` * Sets are not ordered in python. It is a random collection of unique elements. Code: ``` python= # Error: Because there is no order. s[0] ``` >Output: ``` --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-33-c9c96910e542> in <module> ----> 1 s[0] TypeError: 'set' object is not subscriptable ``` - To create an empty set in python we have to use the `set()` function. Code: ``` python= s = set() s ``` >Output: ``` set() ``` --- title: Set Methods description: duration: 600 card_type: cue_card --- ### Set Methods - There are very limited number of methods that a set has. Code: ``` python= s = {1, 2, 3} s ``` >Output: ``` {1, 2, 3} ``` Code: ``` python= # Add something to a set s.add(4) s ``` Output: ``` {1, 2, 3, 4} ``` Code: ``` python= # Remove something from set s.remove(3) s ``` >Output: ``` {1, 2, 4} ``` Code: ``` python s.pop() s ``` >Output: ``` {2, 4} ``` Code: ``` python= # We can update the elements in a set using update s.update((4, 5, 6, 6, 7, 9, 1)) s ``` >Output: ``` {1, 2, 4, 5, 6, 7, 9} ``` --- title: Question-2 description: duration: 180 card_type: cue_card --- ### Question-2 - Take a string as input. - Print the number of unique characters in the string. Code: ``` python= def unique_chars(string): return len(set(string)) unique_chars("This is a sample sentence") ``` >Output: ``` 13 ``` --- title: Quiz-2 description: duration: 60 card_type: quiz_card --- # Question What will be the output of the following code snippet? ``` python= fruits = {"apple", "banana", "cherry"} fruits.add("orange") fruits.update(["mango", "grape"]) print(len(fruits)) ``` # Choices - [ ] 4 - [ ] 5 - [x] 6 - [ ] Error --- title: Set Operations description: duration: 1600 card_type: cue_card --- ### Set Operations - There are many mathematical operations we can do on sets. #### Intersection - We can intersect two sets. - Only contains the common elements in both sets. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/022/original/intersection.png?1689591904" width=300 height=300> \ Code: ``` python= s1 = {2, 3, 4, 5} s2 = {1, 3, 4, 6, 7, 8} ``` Code: ``` python= s1.intersection(s2) ``` >Output: ``` {3, 4} ``` Code: ``` python= s1 & s2 ``` >Output: ``` {3, 4} ``` #### Union - Contains all the elements <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/026/original/union.png?1689592070" width=300 height=300> \ Code: ``` python= s1 = {2, 3, 4, 5} s2 = {1, 3, 4, 6, 7, 8} ``` Code: ``` python= s1.union(s2) ``` >Output: ``` {1, 2, 3, 4, 5, 6, 7, 8} ``` Code: ``` python= s1 | s2 ``` >Output: ``` {1, 2, 3, 4, 5, 6, 7, 8} ``` #### Difference - This only contains all elements in **A** which are not common in **B**. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/027/original/difference.png?1689592302" width=300 height=300> \ Code: ``` python= s1 = {2, 3, 4, 5} s2 = {1, 3, 4, 6, 7, 8} s1.difference(s2) ``` >Output: ``` {2, 5} ``` Code: ``` python= s1 - s2 ``` >Output: ``` {2, 5} ``` #### Symmetric Difference - Contains all the elements in **union** minus the **common elements**. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/028/original/symmetric_difference.png?1689592498" width=300 height=300> \ Code: ``` python= s1 = {2, 3, 4, 5} s2 = {1, 3, 4, 6, 7, 8} s1.symmetric_difference(s2) ``` >Output: ``` {1, 2, 5, 6, 7, 8} ``` Code: ``` python= s1 ^ s2 ``` >Output: ``` {1, 2, 5, 6, 7, 8} ``` --- title: Question - 3 description: duration: 500 card_type: cue_card --- ### Question - 3 - You are given a string as input. - Ascertain wheather that string contains a binary number or not. ``` Sample input-1 "0011010101ab" ``` ``` Sample input-2 "010010101010" ``` ``` Sample output-1 "Not Binary!" ``` ``` Sample output-2 "Binary!" ``` Code: ``` python= # 1 def check_binary(string): for i in string: if not (i == "0" or i == "1"): return "Not Binary!" return "Binary!" ``` Code: ``` python= print(check_binary("adasfafasfas")) print(check_binary("01010101010")) ``` >Output: ``` Not Binary! Binary! ``` Code: ``` python= # 2 def check_binary_cool(string): s = set(string) if s == {"0", "1"}: return "Binary!" return "Not Binary!" ``` Code: ``` python= print(check_binary_cool("adasfafasfas")) print(check_binary_cool("01010101010")) ``` >Output: ``` Not Binary! Binary! ``` --- title: Quiz-3 description: duration: 60 card_type: quiz_card --- # Question What will be the output of the following code snippet? ``` python= set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = set1.intersection(set2) print(len(set3)) ``` # Choices - [ ] 0 - [x] 2 - [ ] 3 - [ ] Error --- title: Quiz-4 description: duration: 60 card_type: quiz_card --- # Question What will be the output of the following code snippet? ``` python= set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = set1.symmetric_difference(set2) print(set3) ``` # Choices - [x] `{1, 2, 3, 6, 7, 8}` - [ ] `{1, 2, 3, 8}` - [ ] `{1, 2, 3, 4, 5}` - [ ] `{4, 5, 6, 7, 8}` --- title: Quiz-5 description: duration: 60 card_type: quiz_card --- # Question What will be the output of the following code snippet? ``` python= set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = set1.union(set2) print(set3) ``` # Choices - [ ] `{4, 5}` - [ ] `{1, 2, 3}` - [ ] `{{1, 2, 3, 6, 7, 8}}` - [x] `{1, 2, 3, 4, 5, 6, 7, 8}`