What is a Tuple in Python and how to use it?

0 votes
Can someone help me in understanding Tuple, I am new to Python and finding it difficult understand Tuple. Please help.
Mar 4 in Python by Trisha
109 views

3 answers to this question.

0 votes

A tuple is created in a similar way as a list but the values are included in ().

Example:

tup3 = "a", "b", "c", "d";
tup1 = ();
Access Tuple
tup1 = ('a', 'b', 1, 2);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print "tup1[0]: ", tup1[0];
print "tup2[1:5]: ", tup2[1:5];

Output:

tup1[0]: a
tup2[1:5]: [2,3,4,5]
answered Mar 4 by Priyaj
• 56,520 points
0 votes

  Tuple is a collection of Python objects.it is similar to a list. The difference between the two is that we cannot change the elements of a tuple once it is assigned whereas in a list, elements can be changed.A tuple is created by placing all the items (elements) inside a parentheses (), separated by comma.

example:

# One way of creation

tup = 'rajesh', 'kumar'

print(tup)

  # Another for doing the same

tup = ('rajesh', 'kumar')

print(tup)

output:

   ('rajesh', 'kumar')
   ('rajesh', 'kumar')

answered Apr 12 by rajesh
• 1,210 points
0 votes
it is a python sequency which stores group of elements or items. tuple are same as list the main difference is they are inmutable.

where as list is mutable.
answered Jul 17 by mounika reddy
Hey @Mounika, can you explain your answer a bit with some examples?

Related Questions In Python

+4 votes
7 answers
0 votes
1 answer

Is there a foreach function in python and is there a way to implement it if there isnt any

Every occurence of "foreach" I've seen (PHP, ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,710 points
73 views
0 votes
1 answer

How do I use urllib to see if a website is 404 or 200 in Python?

For Python 3, try doing this: import urllib.request, ...READ MORE

answered Nov 29, 2018 in Python by Nymeria
• 3,520 points

edited Dec 11, 2018 by Nymeria 429 views
0 votes
1 answer

What is absolute import in Python and how is it used?

An absolute {import, path, URL} tells you exactly how ...READ MORE

answered Nov 30, 2018 in Python by Nymeria
• 3,520 points

edited Dec 10, 2018 by Nymeria 77 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 169 views
+1 vote
1 answer

How to check if a string is null in python

Try this: if cookie and not cookie.isspace(): # the ...READ MORE

answered Aug 20, 2018 in Python by Priyaj
• 56,520 points
2,177 views
0 votes
1 answer

“stub” __objclass__ in a Python class how to implement it?

You want to avoid interfering with this ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 56,520 points
44 views