Professional Documents
Culture Documents
Tuple
• Similar to a list except it is immutable.
• Syntactically, it is comma-seperated list of values.
– tuple = 'a', 'b', 'c', 'd', 'e‘
• To create a tuple with a single element, we have to include the comma:
– t1 = ('a',)
– type(t1)
<type 'tuple'>
• Without the comma, Python treats ('a') as a string in parentheses:
– t2 = ('a')
– type(t2) ??
• The operations on tuples are the same as the operations on lists
• The index operator selects an element from a tuple.
– tuple = ('a', 'b', 'c', 'd', 'e')
– tuple[0]
– 'a‘
• The slice operator selects a range of elements.
– tuple[1:3]
– ('b', 'c‘)
• But if we try to modify one of the elements of the tuple, we get an error.
– tuple[0] = 'A'
– TypeError: object doesn't support item assignment
Example
• a and x are aliases for the same value. Changing x inside swap makes x refer
to a different value which is x= value of y, but it has no effect on a in main .
Similarly, changing y has no effect on b in main.
• E.g
>>>def div_mod(a,b):
quotient=a/b
remainder=a%b
return quotient, remainder
>>>x=10
>>>y=3
>>>t=div_mod(x,y)
>>>print t # will get (3,1) as output
>>>type(t) #type will be tuple
Unpack a list1 using
*
• list11 = ["This" , "is", "a", "Python",
"program"]
• list12 = [10, 9,5,3,10]
• x=[*list11 , *list12 ]
– #use comma to merge, * to
unpack
• print(x)
Use input() and get few elements. Now treat them
as a list and tuple and set
• tup = tuple(input("enter tuple"))
• print(tup)
Basic Tuple Operations
• import random
• print(random.randint(0, 10))
• choice() is an inbuilt function in Python programming language that
returns a random item from a list, tuple, or string.
• print(random. choice(t1))
• print(random.randint(7, 99999999999999999))
• L.sort() #correct
• L.sort(reverse = True) #correct but L.sorted() is incorrect
• Consider the tuple (1,2,3,4,5,6,7,8,9,10). Write a program to print half its values in
one line and the other half in the next line.
• Consider the tuple(12,7,38,56,78,47,88,79,23). WAP to print another tuple whose
values are even numbers in the given tuple.
• An email address is provided: hello@python.org. Using tuple assignment, split the
username and domain from the email address. (Use split method).
• Define a function that prints a tuple whose values are the cube of a number
between 1 and 15(both included).
Questions
• Q1. Create a tuple and try to changing value of any one element, also display the length of list.
• Q2. Create a tuple having single element and append two tuples.
• Q3. Create a tuple and sort it.
• Q4. Create a tuple of numbers and print sum of all the elements.
• Q5. Program to compare elements of tuples.
• Q6. Program to find maximum and minimum of tuple.
• Q7. Count the occurrence of element in tuple in a specific range.
• Q8. Reverse a tuple.
• Q9.Write a loop that traverses the previous tuple and prints the length of each element. What happens if you send
an floating number in index?
Counting
• Divide the problem into sub problems and look for sub problems that
fit a computational pattern
• Traverse a list of numbers and count the number of times a value falls
in a given range.
• Eg: def inBucket(t, low, high):
count = 0
for num in t:
if low < num < high:
count = count + 1
return count
– low = inBucket(a, 0.0, 0.5)
– high = inBucket(a, 0.5, 1)