Python Tutorial: Tuples

Wednesday, 12 August 2020

Tuples

Tuples

Tuple is a data structure in Python. It is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Tuples are enclosed within parentheses ( ).

Syntax

                Variable= (tuple1,tuple2,tuple3………)

Example:

                Subject=(“English”, “Physics”, “Maths”, “Chemistry”,“IP”)

Remember  : Tuples are immutable, but member objects may be mutable.

Examples :

>>> Marks=(43,56,87,34)

>>> Marks

(43, 56, 87, 34)

>>> address=(32,"Ratan","kanpur",208022) #Heterogeneous tuple

>>> address

(32, 'Ratan', 'kanpur', 208022)

>>> per=43,76,89,43      #parenthesis are not must

>>> per

(43, 76, 89, 43)

>>> digits=(1,2,3,4,5,)

>>> digits

(1, 2, 3, 4, 5)

>>> nestTuple=(32,45,67),(11,22,33)       #nested tuple- tuples inside a tuple

>>> nestTuple

((32, 45, 67), (11, 22, 33))

>>> tupList=((1,3,6,7),['Welcome','Tuple']) # tuple and list inside a tuple- a nested tuple

>>> tupList

((1, 3, 6, 7), ['Welcome', 'Tuple'])

>>> year=([1999,2000,2001],[2017,2018,2020])

>>> year

([1999, 2000, 2001], [2017, 2018, 2020])

 

Example 1: Creating empty tuple :

>>> Tup=tuple()

>>> print(Tup)

()

OR

>>> d=()

>>> d

()

Example 2: to create tuple containing string values using function tuple().

>>> Languages=(‘C’, ‘C++’, ‘Java’, ‘Python’)

>>> print(Languages)  

(‘C’, ‘C++’, ‘Java’, ‘Python’)

>>> type(Languages)

<class 'tuple'>

Example : Creating tuple by accepting user input using while or for loop

Using while loop

Marks=tuple()

N=int(input("How many marks to be entered ->"))

I=1

while(I<=N):

    A=input("enter marks :")

    Marks=Marks+(A,)

    I=I+1

print("Output ")

print(Marks)

output

How many marks to be entered ->5

enter marks :87

enter marks :54

enter marks :32

enter marks :98

enter marks :43

Output

('87', '54', '32', '98', '43')

 

Using for loop

Marks=tuple()

N=int(input("How many marks to be entered ->"))

for I in range(N):

    A=input("enter marks :")

    Marks=Marks+(A,)

    I=I+1

print("Output ")

print(Marks)

Output :

How many marks to be entered ->5

enter marks :98

enter marks :32

enter marks :65

enter marks :87

enter marks :43

Output

('98', '32', '65', '87', '43')

 Question 1: Write a Python program to store ‘n’ mobile accessories in a tuple.

Accessories =tuple()

N=int(input("How many accessories to be entered -> "))

for I in range(N):

    print("Enter accessories ",I+1," : ", end="")

    A=input()

    Accessories =Accessories+(A,)

print("Output -> ")

print(Accessories)

Output :

How many accessories to be entered -> 5

Enter accessories 1 : Headphone

Enter accessories 2 : Battery

Enter accessories 3 : Cover

Enter accessories 4 : Charger

Enter accessories 5 : SD Card

Output ->

('Headphone', 'Battery', 'Cover', 'Charger', 'SD Card')

 

Accessing a Tuple

Elements of a tuple can be accessed using an indexing method.

Traversing a tuple

 

1.       Using in operator using for loop

teacher=('Pramod','Ankita','Vanshika','Amit')

for i in teacher:

    print(i)

Output:

Pramod

Ankita

Vanshika

Amit

2.       Using range() function

teacher=('Pramod','Ankita','Vanshika','Amit')

n=len(teacher)

for i in range(n):

    print(teacher[i]) # Like lists, tuples also can be created using range() function.

   

Output:

Pramod

Ankita

Vanshika

Amit

 Difference Between List and Tuple in Python

Sr. No.

Key

List

Tuple

1

Type

List is mutable.

Tuple is immutable.

2

Iteration

List iteration is slower and is time consuming.

Tuple iteration is faster.

3

Appropriate for

List is useful for insertion and deletion operations.

Tuple is useful for read only operations like accessing elements.

4

Memory Consumption

List consumes more memory.

Tuples consumes less memory.

5

Methods

List provides many in-built methods.

Tuples have less in-built methods.

6

Error prone

List operations are more error prone.

Tuples operations are safe.

7.

Example

aList=[43,54,43,32,34]

aTuple=(43,54,73,56)


 

 

 

 


No comments:

Post a Comment