Python Task β Tuple
2 August 2024 at 03:42
- Create a tuple containing the names of three fruits. Print the tuple and its type.
- Given a tuple
t = ("apple", "banana", "cherry")
, access and print the second element. - Unpack the tuple
t = (1, 2, 3)
into variablesa
,b
, andc
, and print the variables. - Concatenate two tuples
t1 = (1, 2)
andt2 = (3, 4)
and print the result. - Create a tuple
t = ("repeat",)
and repeat it three times. Print the resulting tuple. - Given a tuple
t = (1, 2, 3, 2, 2, 4)
, count the number of times the number2
appears. - Given a tuple
t = ("a", "b", "c", "d")
, find the index of the element"c"
. - Check if the element
5
is present in the tuplet = (1, 2, 3, 4)
- Find and print the length of the tuple
t = ("one", "two", "three")
. - Slice the tuple
t = (0, 1, 2, 3, 4, 5)
to obtain a sub-tuple containing only the elements from index 2 to 4. - Create a nested tuple representing a 2D point
(x, y) = ((1, 2), (3, 4))
. Access and print the second coordinate of the second point. - Try to change the value of the first element in the tuple
t = (1, 2, 3)
and observe what happens. - Convert a list
l = [1, 2, 3]
to a tuple and print the result. Then convert a tuplet = (4, 5, 6)
to a list and print the result. - Create a tuple with a single item
5
and verify its type is a tuple. - Iterate over the tuple
t = ("ParottaSalna", "is", "good")
and print each element. - Convert the string
"hello"
into a tuple of characters. - Convert a dictionary
d = {"one": 1, "two": 2}
into a tuple of its items. - Write a function that takes a tuple of numbers and returns the sum of the numbers.
- Use tuples as keys in a dictionary to represent points on a grid. For example,
grid = {(0, 0): "origin", (1, 2): "point A"}
.