Answers for "tuple in python"

4

changing tuple values

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x)
Posted by: Guest on May-17-2020
52

what is a tuple in python

# A tuple is a sequence of immutable Python objects. Tuples are
# sequences, just like lists. The differences between tuples
# and lists are, the tuples cannot be changed unlike lists and
# tuples use parentheses, whereas lists use square brackets.
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = "a", "b", "c", "d";

# To access values in tuple, use the square brackets for
# slicing along with the index or indices to obtain value
# available at that index.
tup1[0] # Output: 'physics'
Posted by: Guest on March-03-2020
4

tuples in python

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
Posted by: Guest on October-12-2020
3

how to use tupels python

tupel = ('banana',10,True)
print(tupel[2])
Posted by: Guest on June-13-2020
4

tuple in python

#a tuple is basically the same thing as a
#list, except that it can not be modified.
tup = ('a','b','c')
Posted by: Guest on March-14-2020
-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