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
50 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,120 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

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
43 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,500 points

edited Dec 11, 2018 by Nymeria 133 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,500 points

edited Dec 10, 2018 by Nymeria 52 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
25 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,120 points
779 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,120 points
30 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.