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, 2019 in Python by Trisha
2,015 views

4 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, 2019 by Priyaj
• 58,020 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, 2019 by rajesh
• 1,270 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 18, 2019 by mounika reddy
Hey @Mounika, can you explain your answer a bit with some examples?
0 votes

Tuples  are a  Unchanging sequence of values, each one accessible individually, and a tuple is a basic type in Python. You can recognize tuples when they are created because they are surrounded by ‘()’ parentheses . Tuples contain data such as strings and numbers. Open the python shell and perform the following example.

Program:
>>> filler = ("string", "filled", "by a", "tuple")
>>> print("A %s %s %s %s" % ("string", "filled", "by a", "tuple")
A string filled by a tuple

>>> print("A %s %s %s %s" % filler)
A string filled by a tuple
answered Jun 21, 2020 by sahil
• 500 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,720 points
1,182 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,560 points

edited Dec 11, 2018 by Nymeria 14,054 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,560 points

edited Dec 10, 2018 by Nymeria 1,405 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,463 views
0 votes
1 answer
+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
• 58,020 points
23,141 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
• 58,020 points
2,368 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP