ModuleSevenLessonEightActivity.ipynb - Colaboratory

pdf

School

Smithfield-selma High *

*We aren’t endorsed by this school

Course

10

Subject

Computer Science

Date

Nov 24, 2024

Type

pdf

Pages

17

Uploaded by DukeDanger9183

Report
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 1/17 I learned about tuples I did not have tourble with this lesson Lucas DuBois 12/15/23 () +, in, not in, len, min, max Students will be able to: Differentiate between tuples and lists Create tuples Access tuple elements Convert lists into tuples and tuples into lists Delete, slice, and unpack tuples Use basic tuple operations and functions Before submission : Remember for max credit, you need to add a complete header comment in a code cell at the very top of this ±le and descriptive comments in each code cell below (examples AND tasks) that explains what your code is doing. ALL functions need a formatted docstring – see the bottom of Mod 3 Lesson 1 Activity for the requirements. Module Seven Lesson Eight Activity: Tuples Concepts View Tuple Basics video A list is a very useful data structure that can manipulate a sequence of elements. A tuple is another data structure type that can also store and manipulate a sequence of elements. Like lists, the Tuple Basics
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 2/17 elements of a tuple can be of any type (i.e. int , float , string , list , another tuple ). However, unlike lists, a tuple is an immutable data structure, which means that after you de±ne a tuple, you are not allowed to change it. In other words, you cannot reorder the elements of a tuple, cannot append additional elements, and cannot delete elements. You might be wondering, if tuples are like restricted lists, when and why should I use them? The importance of tuples is most obvious when passing to and returning from a function. They are also important to communicate to people reading your code that you do not intend for the content of the tuple to be mutated (changed). Creating a tuple You can create a tuple by enclosing its elements within parentheses, for example the following is a tuple containing 3 elements (13, 5, 92) . Note that a list was de±ned using square brackets [13, 5, 92] . >>> T = (13, 5, 92) >>> type(T) <class 'tuple'> >>> L = [13, 5, 92] >>> type(L) <class 'list'> Creating an empty tuple You can create an empty tuple using empty parentheses. >>> T = () >>> type(T) <class 'tuple'> Creating a tuple with a single element When creating a tuple with a single element, you have to add a comma , after the single element. Otherwise, Python will assume you are using the parentheses as part of an expression rather than to create a tuple. >>> T1 = (5) >>> type(T1) <class 'int'> >>> T2 = (5,) # note the comma after 5
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 3/17 >>> type(T2) <class 'tuple'> Accessing elements of a tuple You can access the individual elements of a tuple in the same way you access the elements of a list or a string, by using the index of the element of interest. >>> T = (13, 5, 92) >>> T[0] 13 >>> T[-1] 92 Converting a list into a tuple You can convert a list into a tuple by passing it into the tuple function. >>> L = [4, 5.3, 'name'] #list >>> T = tuple(L) #tuple >>> print(T) (4, 5.3, 'name') >>> type(T) <class 'tuple'> Converting a tuple into a list You can convert a tuple into a list by passing it into the list function. >>> T = ('name', [2, 4], 5.3, 19) #tuple >>> L = list(T) #list >>> print(L) ['name', [2, 4], 5.3, 19] >>> type(L) <class 'list'> Changing a tuple element Tuples are immutable objects and cannot be changed. If you try to change the content of a tuple, a TypeError exception will be raised.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 4/17 >>> T = (13, 5, 92) >>> T[0] = 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment A list can be one of the elements of a tuple; the tuple doesn't store the whole list, but stores a reference to the storage location of the list in memory. Therefore, if the memory location storing the list is changed, the tuple references the changed list. In the following example, the second element in the tuple T is a list [2, 4] . T[1] stores the location of the list rather than the list itself; therefore, if you change the content of the list, (i.e. change 2 to a 5 ), the tuple T now references the new list [5, 4] . Technically speaking, the tuple did not change; it still references the same memory location. However, the memory location changed. This concept will be more relevant when you use tuples with functions or copy tuples. >>> T = ('name', [2, 4], 5.3, 19) >>> T[1][0] = 5 >>> T ('name', [5, 4], 5.3, 19) Deleting a tuple Like any other variable, a tuple can be deleted by using the del() function. >>> del(T) >>> type(T) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'T' is not defined Examples Creating Tuples Creating homogeneous tuples
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 5/17 Homogeneous tuples contain elements of the same type; in the following example, you will see how to create a tuple of int values and another tuple of string values. # Create a homogeneous int tuple T_int = (10, -4, 59, 58, 23, 50) type(T_int) # Create a homogeneous string tuple T_string = ("word", "letter", "vowel", "spell", "book", "write", "read") type(T_string) Tuples can contain elements of different types. In the following example, you will see how to create tuples containing different types. Creating heterogeneous tuples # Create heterogeneous tuples T = ("Tobias", 23, 25.3, []) type(T) # Create heterogeneous tuples # A datetime object can be a tuple element from datetime import datetime now = datetime.today() T = ((1.5,2.6), "home", now) type(T) Creating single-element tuples T = ("switch") # This is not a tuple type(T) T = ("switch",) # Note the comma after the string makes T a tuple type(T) Converting lists to/from tuples
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 6/17 # List of employee names names_list = ["Suresh", "Colette", "Skye", "Hiroto", "Tobias", "Tamara", "Jin", "Joana", # Sort the names alphabetically sorted_list = sorted(names_list) # Convert list into tuple names_tuple = tuple(sorted_list) # List converted into a tuple print(type(names_tuple)) # Print the first and last name in the tuple print("First name is: {:s}".format(names_tuple[0])) print("Last name is: {:s}".format(names_tuple[-1])) In this example, the user is asked to enter 3 integers, the program then saves the squares of these integers into a tuple and print its content. Creating Tuples from User Input # Collect 3 int numbers from a user L = [] for i in range(3): tmp = int(input("Enter an int {:d}/3: ".format(i))) L.append(tmp ** 2) # Convert the list into a tuple T = tuple(L) # Print the content of the tuple print("Tuple of squares is:", T) # Print each of the tuple elements on a new line for i in range(3): print("T[{0:d}] = {1:d}".format(i, T[i])) Changing Tuple Elements T = ("Tobias", 23, 25.3, []) # Tuples are immutable and cannot be changed T[0] = "hello"
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 7/17 T = ("Tobias", 23, 25.3, []) # A list inside a tuple can change T[-1].append(44) # The tuple did NOT change, it still refers to the same list; only the list was changed print(T) Task 1 Create a tuple that consists of the variables below Creating Tuple Basics # In this code, a tuple containing different variables is created and saved as tuple1. # Each variable has a different data type value. The tuple is displayed. # [ ] Create a tuple that consists of the following variables x = 5 l = [4.4, 5.3] s = "something" t = (9, 0) tuple1 = (x,l,s,t) print(tuple1) (5, [4.4, 5.3], 'something', (9, 0)) Task 2 Change the third element of T to [59, 20.4] Modifying Tuple Basics
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 8/17 # In this code, the third item in the "T" tuple is changed using slicing. Since this item # we first indicate the index of the item in the list we want to change and then re-assig # The tuple with the modified list is displayed T = ([43.6, 34], [49, 59], [50, 34.6], [39, 49]) # TODO: your code here T[2][0] = 59 T[2][1] = 20.4 print(T) ([43.6, 34], [49, 59], [59, 20.4], [39, 49]) Task 3 Write a program to merge the content of T1 and T2 into one tuple T Correct output should be T = (5, 4, 3, 9, 2, 12) Note: T = ((5, 4, 3), (9, 2, 12)) is an incorrect output Hint: Use list to/from tuple conversion Merging Tuple Basics # In this code, the contents of the variables T1 and T2 are combined. First, these tuple # lists, added together, and saved in T. Then, the list "T" is converted into a tuple. T # tuple "T" is displayed. T1 = (5, 4, 3) T2 = (9, 2, 12) T = list(T1) + list(T2) T = tuple(T) print("T =",T) T = (5, 4, 3, 9, 2, 12) Concepts
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 9/17 View Slicing and Unpacking Tuples video Slicing tuples Slicing a tuple (or a list) lets you access a subset of a tuple. You can then assign this subset as a new tuple, pass it to a function, or do anything you would do with the full tuple. You can slice a tuple using the following syntax T[initial_index:final_index] : The initial_index is optional; if left empty, slicing starts from 0 The final_index is optional; if left empty, slicing goes to the last element of the tuple The T[initial_index] element is included in the sliced tuple; the T[final_index] is excluded You can use negative numbers for both indices, where the ±nal tuple element has index -1, the preceding has index -2, and so on Whether you use negative or positive indices you should always keep initial_index < final_index >>> T = ('name', [2, 4], 5.3, 19) >>> T[1:2] # from index 1 (included) to index 2 (excluded) ([2, 4],) >>> T[1:] # from index 1 (included) to the last element ([2, 4], 5.3, 19) >>> T[:2] # from the start of a tuple to index 2 (excluded) ('name', [2, 4]) >>> T[-3:-1] # from index -3 (included) to index -1 (excluded) ([2, 4], 5.3) >>> T[-3:] # from index -3 to the last element ([2, 4], 5.3, 19) >>> T[:] # all elements ('name', [2, 4], 5.3, 19) Unpacking tuples A tuple can be unpacked and its values can be assigned to multiple variables at the same time. This is especially useful when a function returns a single tuple containing all the returned variables. >>> (a, b) = (4, 5) >>> print(a) 4 >>> print(b) Slicing and Unpacking Tuples
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 10/17 5 >>> The parentheses can be left off when unpacking tuples. >>> x, y, z = 1, 2, 3 >>> x 1 >>> y 2 >>> z 3 Examples Slicing tuples T = (12, 24, 'name', 'city') # Slice the tuple into numerical and textual tuples numerical_tuple = T[0:2] print(numerical_tuple) textual_tuple = T[-2:] print(textual_tuple) Unpacking tuples # Swap variables using tuple unpacking e1 = 5 e2 = 109 print("\nBefore swapping:") print("e1 = {:3d}\t e2 = {:3d}".format(e1, e2)) e1, e2 = e2, e1 print("\nAfter swapping:") print("e1 = {:3d}\t e2 = {:3d}".format(e1, e2))
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 11/17 Tuples can be used in functions to return multiple values. In this example, you will see the function split_name splits a full name then returns the ±rst and last names as a packed tuple. You can then unpack the returned tuple and use its content as separate variables. Returning function values # Split a full name into the first and last names def split_name(name): names = name.split(" ") first_name = names[0] last_name = names[-1] # pack the variables into a tuple, then return the tuple return (first_name, last_name) # Ask user for input name = input("Enter your full name: ") # Unpack the returned tuples into first, last variables # looks like the function returns 2 variables first, last = split_name(name) # Unpacked variables can be used separately print("First name: {:s}".format(first)) print("Last name: {:s}".format(last)) Task 4 Write a program to split the content of T into T1 and T2 T1 = (5, 4, 3) T2 = (9, 2, 12) Slicing Tuples
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 12/17 # In this code, the contents of the "T" tuple are split into two different tuples by usi # "T1" starts at index 0 and ends at index 2. "T2" starts at index 3 and ends at the last # The tuples are displayed. T = (5, 4, 3, 9, 2, 12) T1 = T[:3] print("T1 =", T1) T2 = T[3:] print("T2 =", T2) T1 = (5, 4, 3) T2 = (9, 2, 12) Task 5 Write an expression to unpack T into: 1. x = 5 2. l = [3, 5.3] 3. s = 'something 4. 4) t = (9, 0) Unpacking Tuples # In this code, the "T" tuple is unpacked into 4 different variable. Each item in the tu # to a new variable by using slicing. The variables contents are displayed. T = (5, [3, 5.3], 'something', (9, 0)) x = T[0] l = T[1] s = T[2] t = T[3] print("After unpacking the tuple:", T) print("x =", x) print("l =", l) print("s =", s) print("t =", t)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 13/17 After unpacking the tuple: (5, [3, 5.3], 'something', (9, 0)) x = 5 l = [3, 5.3] s = something t = (9, 0) Concepts View Tuple Operations and Functions video Python support several tuple operations, such as containment, identity, and concatenation. In the following examples, you will explore a few of the tuple operations. Tuple Operations and Functions Examples You can test whether an object is contained in a tuple. Containment ( in , not in ) T = (4, [5, 6], 'name', 3.5, True) print("4 contained in T?", 4 in T) print("5 not contained in T?", 5 not in T ) print("False contained in T?", False in T) You can test whether two tuples contain equal elements by using the identity == operator. Similarly, you can test whether tuples are identical by using the is operator. Remember, if two tuples (and lists) are identical, changing an element in one will generate the same change in the other. Identity ( is ) and equality ( == )
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 14/17 # Equivalent tuples, not identical T1 = (10, [2, 4], 59) T2 = (10, [2, 4], 59) if (T1 == T2): print("Equal tuples") else: print("Not equal tuples") if (T1 is T2): print("Identical tuples") else: print("Not identical tuples") # Identical tuples (also equivalent) T1 = (10, [2, 4], 59) T2 = T1 if (T1 == T2): print("Equal tuples") else: print("Not equal tuples") if (T1 is T2): print("Identical tuples") else: print("Not identical tuples") # Changing one of 2 identical tuples T1 = (10, [2, 4], 59) T2 = T1 # A change in T1 is a change in T2 T1[1][0] = 20 print("T1 = ", T1) print("T2 = ", T2) You can concatenate two tuples and save the result in a new tuple. Concatenation ( + )
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 15/17 T1 = ("First", "Last") T2 = ("Middle",) # single element tuple # Concatenate two tuples T = T1 + T2 print(T) You can also concatenate sliced tuples T1 = (10, [2, 4], 59) T2 = (59, [2, 4], 'name', 3.5, True) # Concatenate sliced tuples T = T1[1:] + T2[0:2] print(T) The length of a tuple is the number of elements in it. If a list (or another tuple) is an element of the tuple, it is counted as 1 regardless of how many subelements it actually contains. Knowing the length of a tuple might be helpful when iterating over the elements of the tuple. Length of a tuple ( len ) # length of tuple T1 = (10, [2, 4], 59) print(len(T1)) # Iterate over elements of a tuple T1 = (10, [2,4], 59) for i in range(len(T1)): print("T1[{:d}] = {}".format(i, T1[i])) Task 6 Write a program to merge the content of T1 and T2 into one tuple T Correct output should be T = (5, 4, 3, 9, 2, 12), T = ((5, 4, 3), (9, 2, 12)) is an incorrect output Tuple Operations and Functions
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 16/17 You should NOT use lists in your solution but concatenation # In this code, tuples "T1" and "T2" are concatenated/ added together and saven in the "T # are displayed. T1 = (5, 4, 3) T2 = (9, 2, 12) T = T1 + T2 print("T =", T) T = (5, 4, 3, 9, 2, 12) Task 7 Write a program to prompt the user for a number, then test if the number is contained in T Tuple Operations and Functions # In this code, the user is asked to enter a whole number. Then, the 'in' operator in an # if the number entered is in the tuple "T". If the number is in "T", an statement saying # If the number is not in "T", a statement saying so is displayed.. T = (23, 45, 93, 59, 35, 58, 19, 3) num = input("Enter a whole number: ") if num in T: print(num,"is contained in 'T'") else: print(num,"is not contained in 'T'") Enter a whole number: 23 23 is not contained in 'T' Task 8 Write a function to return the largest element in a tuple T Tuple Operations and Functions
12/15/23, 3:33 PM ModuleSevenLessonEightActivity.ipynb - Colaboratory https://colab.research.google.com/drive/18lnHaZjikR4IYuYnFqVGRmZHSTZ8ZZsp#scrollTo=Q6TROxE4nYOp&printMode=true 17/17 # In this code, the largest_num() function is defined. This function returns the largest # To achieve this, I used a for...in loop to iterate through each item in "T" and then a # to compare each item and see which one is the largest. Once found, the largest number T = (23, 45, 93, 59, 35, 58, 19, 3) def largest_num(): l_num = T[0] for num in T: while num > l_num: l_num = num return l_num largest_num() 93 Task 9 Write a program to compute the average of the elements in T Tuple Operations and Functions # In this code, a for..in loop is used to iterate through each item in the tuple "T" and t