Answers for "PYTHON TUPLE IN LIST"

5

python list of tuples

#List of Tuples
list_tuples =  [('Nagendra',18),('Nitesh',28),('Sathya',29)]

#To print the list of tuples using for loop you can print by unpacking them
for name,age in list_tuples:
  print(name,age)

#To print with enumerate--->enumerate is nothing but gives the index of the array.
for index,(name,age) in list_tuples:
  #print using fstring
  print(f'My name is {name} and age is {age} and index is {index}')
  #print using .format
  print('My name is {n} and age is {a} and index is {i}'.format(n=name,a=age,i=index))
Posted by: Guest on June-16-2020
1

tuple and list in python

#to create a tuple you use ( ) :
myTuple = ()
#but to create a list you use [ ] :
myList = []
#tuples cannot be changed while the lists can be modified but Tuples are
#more memory efficient
Posted by: Guest on July-09-2021
0

what is tuple in python

tuple
Posted by: Guest on June-26-2021
-1

tuple in python

Tup1 = (1) 

print(Tup1)

print(type(Tup1))


# To crate a tuple you must have at least 2 ELEMENTS

tup3 = (1, 2, 3, 4, 5, 6, 7, 8, 9)
tup4 = ('a', 'b', 'c')

defseq = 2,5
print(defseq)
print(type(defseq))

# 2 or more integers assigned to a variable will become tuple automaticaly 


tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)

print("Original", tuple)

print("Single element", tuple[2])

print("Definate range", tuple[2:5])

#print("Start a definate", tuple[])

# tup = (3, 5, 7, 9)
# del tup
# print("It'll show an error")
# print(tup, "It'll show an error")

# print("Length of tuple is ", len(tup))


# CONCATENATE 

tup3 = (8,2)
print(tup3+tuple)

print(("Hello Students ") * 3)

print("Membership =", 8 in tup3)

print(max(tup3))
print(min(tup3))

quo, rem  = divmod(100, 3)

print("Quotient =", quo)
print("Remainder = ", rem)

com_tup1 = (1, 2, 3, 4) # sum is 10
com_tup2 = (7, 8, 9, 2) # sum is 26 

print(com_tup1, com_tup2)
print(com_tup1 > com_tup2, "Tuple 1 is greater than tuple 2")
print(com_tup1 < com_tup2, "Tuple 2 is greater than tuple 2")
print(com_tup1 == com_tup2, "They're both the same ") 

if com_tup1 > com_tup2:
    print("Tuple 1 is greater than tuple 2")
elif com_tup1 < com_tup2:
    print("Tuple 2 is greater than tuple 1")
else:
    print("They're both the same")

# Comparision is not the length, it's the sum  

# Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are 
# not equal (i.e. the first is greater or smaller than the second) then that's 
# the result of the comparison, else the second item is considered, then the third and so on.

# Nested Tuples

toppers = ('Arnav', 101, 'High School', ('999-6969-420', '[email protected]')), ('Mayank', 107, 'Inter', ('999-6969-420', '[email protected]')), ('Manish', 103, 'Higher Sec', ('999-6969-420', '[email protected]'))

print(toppers)

for i in toppers:
    print(i)

print(com_tup1.index(3))
print(com_tup1.count(3))

# Zip() function - takes 2 or more sequences and zips into a list (creats a list with tuples)

tup_zip = (1, 2, 3, 4)
list_zip = ['a', 'b', 'c', 'd']

print(list(zip(tup_zip, list_zip)))

# Sorted() function. The ssort() function doesn't work because it is immutable 

# sorted to sort a tuple of values  

tup_sort = (1, 4, 3, 5, 3, 4, 7, 2, 3, 9)
print(sorted(tup_sort))
Posted by: Guest on July-21-2021

Python Answers by Framework

Browse Popular Code Answers by Language