tuple in python
#a tuple is basically the same thing as a
#list, except that it can not be modified.
tup = ('a','b','c')
tuple in python
#a tuple is basically the same thing as a
#list, except that it can not be modified.
tup = ('a','b','c')
make a tuple
#you can put a lotta stuff in tuples
tup = ("string", 2456, True)
tuple() python
example = [1, 2, 3, 4]
# Here is a list above! As we both know, lists can change in value
# unlike toples, which are not using [] but () instead and cannot
# change in value, because their values are static.
# list() converts your tuple into a list.
tupleexample = ('a', 'b', 'c')
print(list(tupleexample))
>> ['a', 'b', 'c']
# tuple() does the same thing, but converts your list into a tuple instead.
print(example)
>> [1, 2, 3, 4]
print(tuple(example))
>> (1, 2, 3, 4)
how to create tuple in python
# Let's have a look here first.
coffee = (3.14,) # coffee is a VARIABLE, 3.14 is a FLOAT NUMBER
# What is the TYPE inside the VARIABLE coffee?
#Let's see
print(type(coffee)) #It will print what TYPE the VARIABLE it is
# It will print: <class 'tuple'>
# Why it printed <class 'tuple'> and not <class 'float'>???
# ANSWER: #
# Because we put the comma --> , <-- after the 3.14
# That's why it became a tuple type. (not a float type)
# HOW TO CREATE a TUPLE?
# To create a tuple, create a Parenthesis --> () <--
# Then add value inside, for example I add 4 inside the parenthesis --> (4) <--
# Last, add comma --> , <-- like this --> (4,) <--
# Then add more like this --> (4, 5) <--
# If you want to add more, just use --> , <-- like this --> (4, 5, 6) <--
# Wait, I don't understand, what's the purpose of TUPLE?
# it let you store an ordered sequence of items.
# It's similar to LIST but data inside the TUPLE can't be change.
# NOTES
# STRING is also possible in TUPLE
food = ('cake', 'cupcake', 1997, 2000);
# As you can see, the 'cake' and 'cupcake' are STRINGS, while 1997 and 2000 are INTEGER NUMBERS.
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))
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us