Answers for "python create tuple"

2

tuples in python

my_tuple = ("hello")
print(type(my_tuple))  # <class 'str'>

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # <class 'tuple'>

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # <class 'tuple'>
Posted by: Guest on October-12-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
0

make a tuple

#you can put a lotta stuff in tuples
tup = ("string", 2456, True)
Posted by: Guest on January-21-2021
0

ways to create tuple in python

# There are more ways to create a TUPLE in Python.

# First of all, a TUPLE is usually created by:
# (STEP 1) creating a Parethesis ()
# (STEP 2) putting a value in Parenthesis. Example: (3)
# (STEP 3) putting more value by adding Comma. Example: (3, 4)
# (STEP 4) Repeat Step 3 if u want to add more value. Example: (3, 4, coffee)
# That's how to create TUPLE

# EXAMPLE
adsdfawea = (123, 56.3, 'cheese')

# But did you know, you can create TUPLE without any value inside the () ???
wallet = ()
print(type(wallet))

# But did you know, you can create TUPLE without parenthesis () ???
plate = 'cake',
print(type(plate))

# As you can see, STRING is possible in TUPLE, not just INTEGERS or FLOATS.
Posted by: Guest on September-05-2021
0

how to make a tuple

thistuple = ("apple", "banana", "cherry")

print(thistuple)
Posted by: Guest on May-19-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

Browse Popular Code Answers by Language