Answers for "tuple attributes in python"

-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

Browse Popular Code Answers by Language