# Create a homogeneous int tuple
T_int = (10, -4, 59, 58, 23, 50)
# stores in tuple
type(T_int)
# type
# Create a homogeneous string tuple
T_string = ("word", "letter", "vowel", "spell", "book", "write", "read")
# stores in tuple
type(T_string)
# type
# Create heterogeneous tuples
T = ("Tobias", 23, 25.3, [])
# stores in tuple
type(T)
# type
# Create heterogeneous tuples
# A datetime object can be a tuple element
from datetime import datetime
# from datetime, import datetime
now = datetime.today()
# stores today's date
T = ((1.5,2.6), "home", now)
# stores in tuple
type(T)
# type
T = ("switch") # This is not a tuple
# stores
type(T)
# str
T = ("switch",) # Note the comma after the string makes T a tuple
# stores in tuple
type(T)
# type
# List of employee names
names_list = ["Suresh", "Colette", "Skye", "Hiroto", "Tobias", "Tamara", "Jin", "Joana", "Alton"]
# stores in list
# Sort the names alphabetically
sorted_list = sorted(names_list)
# sorts and stores
# Convert list into tuple
names_tuple = tuple(sorted_list)
# stores type
# List converted into a tuple
print(type(names_tuple))
# prints type
# Print the first and last name in the tuple
print("First name is: {:s}".format(names_tuple[0]))
# prints, formats
print("Last name is: {:s}".format(names_tuple[-1]))
# prints, formats
# Collect 3 int numbers from a user
L = []
# empty list
for i in range(3):
# for i range of 0-3
tmp = int(input("Enter an int {:d}/3: ".format(i)))
# stores user input, formatted, and converted to int form
L.append(tmp ** 2)
# tmp to the power of 2, appends
# Convert the list into a tuple
T = tuple(L)
# converts L to tuple list and stores
# Print the content of the tuple
print("Tuple of squares is:", T)
# prints
# Print each of the tuple elements on a new line
for i in range(3):
# for i in range of 0-3
print("T[{0:d}] = {1:d}".format(i, T[i]))
# prints, formats
T = ("Tobias", 23, 25.3, [])
# stores in tuple
# Tuples are immutable and cannot be changed
T[0] = "hello"
# replaces index 0 with "hello" - error
T = ("Tobias", 23, 25.3, [])
# stores in tuple
# A list inside a tuple can change
T[-1].append(44)
# appends 44 at the last item
# The tuple did NOT change, it still refers to the same list; only the list was changed
print(T)
# prints
# [ ] Create a tuple that consists of the following variables
x = 5
# stores
l = [4.4, 5.3]
# stores in list
s = "something"
# stores
t = (9, 0)
# stores in tuple
tuple_var = (x, l, s, t)
# places all the variables in tuple
print(tuple_var)
# prints
# [ ] Change the third element of T to [59, 20.4]
T = ([43.6, 34], [49, 59], [50, 34.6], [39, 49])
# TODO: your code here
T[2][0] = 59
# in index 2 of T, replace index 0 with 59
T[2][1] = 20.4
# in index 2 of T, replace index 1 with 20.4
print(T)
# print
# [ ] 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
# Hint: Use list to/from tuple conversion
T1 = (5, 4, 3)
# stores in tuple
T2 = (9, 2, 12)
# stores in tuple
T = T1 + T2
# adds, stores
print("T =", T)
# prints
T = (12, 24, 'name', 'city')
# stores in tuple
# Slice the tuple into numerical and textual tuples
numerical_tuple = T[0:2]
# stores index 0-1 of tuple
print(numerical_tuple)
# prints
textual_tuple = T[-2:]
# stores index -2 to end of tuple
print(textual_tuple)
# prints
# Swap variables using tuple unpacking
e1 = 5
# stores
e2 = 109
# stores
print("\nBefore swapping:")
# prints
print("e1 = {:3d}\t e2 = {:3d}".format(e1, e2))
# prints, formats
e1, e2 = e2, e1
# e1 is e2, e2 is e1
print("\nAfter swapping:")
# prints
print("e1 = {:3d}\t e2 = {:3d}".format(e1, e2))
# prints, formats
# Split a full name into the first and last names
def split_name(name):
# creates new function with parameter
names = name.split(" ")
# splits wherever there is " " - a gap
first_name = names[0]
# the first index will the stored
last_name = names[-1]
# the last index will be stored
# pack the variables into a tuple, then return the tuple
return (first_name, last_name)
# returns
# Ask user for input
name = input("Enter your full name: ")
# asks user
# Unpack the returned tuples into first, last variables
# looks like the function returns 2 variables
first, last = split_name(name)
# calls function, stores
# Unpacked variables can be used separately
print("First name: {:s}".format(first))
# prints, formats
print("Last name: {:s}".format(last))
# prints, formats
# [ ] Write a program to split the content of T into T1 and T2
# T1 = (5, 4, 3)
# T2 = (9, 2, 12)
T = (5, 4, 3, 9, 2, 12)
# stores in tuple
T1 = T[:3]
# stores
T2 = T[3:]
print("T1 =", T1)
# prints
print("T2 =", T2)
# prints
# [ ] Write an expression to unpack `T` into:
# 1) x = 5
# 2) l = [3, 5.3]
# 3) s = 'something
# 4) t = (9, 0)
T = (5, [3, 5.3], 'something', (9, 0))
# stores in tuple
#TODO: Your code goes here
x = T[0]
# stores index 0
l = T[1]
# stores index 1
s = T[2]
# stores index 2
t = T[3]
# stores index 3
print("After unpacking the tuple:", T)
# prints
print("x =", x)
# prints
print("l =", l)
# prints
print("s =", s)
# prints
print("t =", t)
# prints
# [ ] Complete the function `current_date` to return today's month, day, and year
# Hint: Use an appropriate function from the datetime module
from datetime import date
def current_date():
current = date.today()
year = current.timetuple()[0]
month = current.timetuple()[1]
day = current.timetuple()[2]
return month, day, year
#TODO return month, day, year
m, d, y = current_date()
print("Today's date is: {:2d}/{:2d}/{:4d}".format(m, d, y))
T = (4, [5, 6], 'name', 3.5, True)
# stores in tuple
print("4 contained in T?", 4 in T)
# prints True
print("5 not contained in T?", 5 not in T )
# prints True
print("False contained in T?", False in T)
# prints False
# Equivalent tuples, not identical
T1 = (10, [2, 4], 59)
# stores in tuple
T2 = (10, [2, 4], 59)
# stores in tuple
if (T1 == T2):
# if T1 equals T2
print("Equal tuples")
# print this
else:
# else
print("Not equal tuples")
# print this
if (T1 is T2):
# if T1 is T2
print("Identical tuples")
# print this
else:
# else
print("Not identical tuples")
# print this
# Identical tuples (also equivalent)
T1 = (10, [2, 4], 59)
# stores in tuple
T2 = T1
# T2 is T1
if (T1 == T2):
# if T1 equals to T2
print("Equal tuples")
# print this
else:
# else
print("Not equal tuples")
# print this
if (T1 is T2):
# if T1 is T2
print("Identical tuples")
# print this
else:
# else
print("Not identical tuples")
# print this
# Changing one of 2 identical tuples
T1 = (10, [2, 4], 59)
# stores in tuple
T2 = T1
# T2 is T1
# A change in T1 is a change in T2
T1[1][0] = 20
# in index 1 of T1, replace index 0 of index 1 with 20
print("T1 = ", T1)
# prints
print("T2 = ", T2)
# prints
T1 = ("First", "Last")
# stores in tuple
T2 = ("Middle",) # single element tuple
# stores in tuple
# Concatenate two tuples
T = T1 + T2
# adds
print(T)
# prints
T1 = (10, [2, 4], 59)
# stores in tuple
T2 = (59, [2, 4], 'name', 3.5, True)
# stores in tuple
# Concatenate sliced tuples
T = T1[1:] + T2[0:2]
# adds index 1-End of T1 and 0-1 of T2
print(T)
# prints
# length of tuple
T1 = (10, [2, 4], 59)
# stores in tuple
print(len(T1))
# prints length
# Iterate over elements of a tuple
T1 = (10, [2,4], 59)
# stores in tuple
for i in range(len(T1)):
# for i in range of the length of T1
print("T1[{:d}] = {}".format(i, T1[i]))
# prints, formats
# [ ] 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
# You should NOT use lists in your solution but concatenation
T1 = (5, 4, 3)
# stores in tuple
T2 = (9, 2, 12)
# stores in tuple
T = T1 + T2
# adds, stores
print("T =", T)
# prints
# [ ] Write a program to prompt the user for a number, then test if the number is contained in T
T = (23, 45, 93, 59, 35, 58, 19, 3)
# stores in tuple
user = int(input("Type in a Number:- "))
# stores user input in int form
if user in T:
# if user is in T
print(user, "is in the tuple!")
# print this
else:
# else
print(user, "is not in the tuple!")
# print this
# Write a function to return the largest element in a tuple T
T = (23, 45, 93, 59, 35, 58, 19, 3)
# stores in tuple
num1 = 0
# stores
for num in T:
# for num in T
if num > num1:
# if num is greater than num1
num1 = num
# num is now num1
else:
# else
pass
# pass
print(num1)
# prints
# [ ] Write a program to compute the average of the elements in T
T = (23, 45, 93, 59, 35, 58, 19, 3)
# stores in tuple
sum = 0
# stores
length = len(T)
# stores len
for num in T:
# for num in T
sum += num
# add num to sum
answer = sum/length
# divide sum by length
print("Average of the elements in T:-", answer)
# prints