Showing posts with label django training in guntur. Show all posts
Showing posts with label django training in guntur. Show all posts

Adding and changing elements in a tuple using insert() and append methods

 Adding and changing elements in a tuple using insert and append:

A list is a mutable type, which means we can add or modify values in it,

but tuples are immutable, so they cannot be changed.

#how to add new element in tuple object using index based

tup=(6,75,66,34,23)

print(tup) #print all tuple elements

newlist=list(tup) #tuple to list convert

newlist.insert(2,40) #add new element in list

tup=tuple(newlist) #convert list to tuple

print(tup) #print all elements in tuple

output:

(6, 75, 66, 34, 23)

(6, 75, 40, 66, 34, 23)

#how to add new element in tuple object using apped() method

tup=(6,75,66,34,23)

newlist=list(tup)

newlist.append(22)

tup=tuple(newlist)

print(tup)

output:

(6, 75, 66, 34, 23, 22)

#Modify nested items of a tuple:

then we can change its values in the case of a nested tuple.

Ex:

tuple1 = (10, 20, [25, 75, 85])

print(tuple1)

tuple1[2][0] = 250

print(tuple1)

output:

(10,20,[250,75,85])

    



#python tuples, #python training in guntur, #python jobs in guntur, #python jobs in vijayawada, #python jobs supporting, #python training and development

Python tuple packing and unpacking - Python coding


Python tuple packing and unpacking

python tuple can also be created without using a tuple() class or enclosing the

items inside the parentheses. it is called the variable packing

#python tuple packing into tuple

tuple=23,44,66,'django'

print(tuple)

print(type(tuple))

#unpacking tuple into variable

a,b,c,d=tuple

print("a is =",a)

print("b is =",b)

print("c is =",c)

print("d is =",d)

print(type(a))

print(type(b))

print(type(c))

print(type(d))

output : 

(23, 44, 66, 'django')

<class 'tuple'>

a is = 23

b is = 44

c is = 66

d is = django

<class 'int'>

<class 'int'>

<class 'int'>

<class 'str'>


Latest Post

Don’t Sweat It – Chill with Crompton Air Cooler Deals

 ðŸŒž Don’t Sweat It – Chill with Crompton Air Cooler Deals  More Details : https://www.amazon.in/s?k=laptops+under+70000&crid=29QIFPXQYB3...