0% found this document useful (0 votes)
8 views

Tuples and Dictionary - Python C11

A tuple is an immutable ordered collection of elements of different data types. Tuples are defined using round brackets and elements within a tuple are separated by comma. Tuples can be nested within other tuples. Common tuple operations include accessing elements using indexes, slicing tuples, concatenating tuples, and packing and unpacking tuple values.

Uploaded by

DuKe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Tuples and Dictionary - Python C11

A tuple is an immutable ordered collection of elements of different data types. Tuples are defined using round brackets and elements within a tuple are separated by comma. Tuples can be nested within other tuples. Common tuple operations include accessing elements using indexes, slicing tuples, concatenating tuples, and packing and unpacking tuple values.

Uploaded by

DuKe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Tuple

Permit different
Immutable / Permit duplicate
Ordered data type values
Read-only values
/ Heterogeneous
Tuple

tuple() can be used to convert a sequence into a tuple.

Tup =10, 20, 30 #A tuple can be created without parentheses as well


Tup1 = ((0,2,40), (“hello”)) #Nested Tuple, a tuple defined inside another tuple

Tup = () #Syntax, strings in ‘ ‘


Tup = (value1, value2………., valuen)
Tuple
Tuple

# Singleton Tuple,
A tuple having a single element
Tuple
Tuple

Use ‘ + ‘ operator with tuple slice also

>>>tup1 = (1, 2, 3, 4)
>>>tup1 [1:3] + (5,6)
(2, 3, 5, 6)

>>>t1= (1, 2, ‘a’, 4)


>>>t1 [-3:-1] + t1[0] #error

>>>t1 [-3:-1] + (t[0], ) #we can only add tuple to a tuple


(2, ‘a’, 1)
Tuple
Tuple

# if an element of a tuple is of mutable type, e.g., a


list, then we can modify that element
Tuple
Tuple
Tuple
Tuple
Tuple
Tuple

Packing and Unpacking


Tuple
Tuple

Q1- A tuple is declared as T = (12,5,6,9,8) What will be the value of sum(T)?

Q2- Suppose t = (1, 2, 4, 3), which of the following is incorrect?


A. print(t[3))
B. t[3] = 45
C. print(max(t))
D. print(len(t))

Q3- State the output t=(1,2,3,4,5) print(t[1:4])


Tuple

Q- WAP to create a tuple by accepting elements from the user using while loop
Tuple

Q- WAP to create a tuple by accepting elements from the user using for loop
Tuple

Q- WAP to create a tuple by accepting elements from the user using for loop

Alternative Question -
WAP to store ‘n’ number of school subjects in a tuple
Tuple

Q- WAP to create and display a nested tuple to store roll no., name and marks of
students.

st=((111,'sonia', 90), (112,'Rohan', 80), (113,'Tushi', 75))


print('S_no','Roll no.','Name','Marks')
for i in range(0, len(st)):
print((i+1)," ",st[i][0]," ",st[i][1]," ",st[i][2])

You might also like