- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10784 Articles for Python
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
8K+ Views
Python includes the following tuple functions −Sr.NoFunction with Description1cmp(tuple1, tuple2)Compares elements of both tuples.2len(tuple)Gives the total length of the tuple.3max(tuple)Returns item from the tuple with max value.4min(tuple)Returns item from the tuple with min value.5tuple(seq)Converts a list into tuple.
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
216 Views
Any set of multiple objects, comma-separated, written without identifying symbols, i.e., brackets for lists, parentheses for tuples, etc., default to tuples, as indicated in these short examples −Example Live Demo#!/usr/bin/python print 'abc', -4.24e93, 18+6.6j, 'xyz'; x, y = 1, 2; print "Value of x , y : ", x,y;OutputWhen the above code is executed, it produces the following result −abc -4.24e+93 (18+6.6j) xyz Value of x , y : 1 2
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
366 Views
Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new tuple, not a string.In fact, tuples respond to all of the general sequence operations we used on strings in the prior chapter −Python ExpressionResultsDescriptionlen((1, 2, 3))3Length(1, 2, 3) + (4, 5, 6)(1, 2, 3, 4, 5, 6)Concatenation('Hi!',) * 4('Hi!', 'Hi!', 'Hi!', 'Hi!')Repetition3 in (1, 2, 3)TrueMembershipfor x in (1, 2, 3): print x,1 2 3Iteration
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
7K+ Views
Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded.To explicitly remove an entire tuple, just use the del statement.Example Live Demo#!/usr/bin/python tup = ('physics', 'chemistry', 1997, 2000); print tup; del tup; print "After deleting tup : "; print tup;OutputThis produces the following result. Note an exception raised, this is because after del tup tuple does not exist any more −('physics', 'chemistry', 1997, 2000) After deleting tup : Traceback (most recent call last): File "test.py", line 9, in print tup; NameError: name 'tup' is not defined
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
9K+ Views
To access values in tuple, use the square brackets for slicing along with the index or indices to obtain value available at that index.Example Live Demo#!/usr/bin/python tup1 = ('physics', 'chemistry', 1997, 2000); tup2 = (1, 2, 3, 4, 5, 6, 7 ); print "tup1[0]: ", tup1[0]; print "tup2[1:5]: ", tup2[1:5];OutputWhen the above code is executed, it produces the following result −tup1[0]: physics tup2[1:5]: [2, 3, 4, 5]
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
19K+ Views
Python includes the following list functions −Sr.NoFunction with Description1cmp(list1, list2)Compares elements of both lists.2len(list)Gives the total length of the list.p>3max(list)Returns item from the list with max value.4min(list)Returns item from the list with min value.5list(seq)Converts a tuple into list.Python includes following list methodsSr.NoMethods with Description1list.append(obj)Appends object obj to list2list.count(obj)Returns count of how many times obj occurs in list3list.extend(seq)Appends the contents of seq to list4list.index(obj)Returns the lowest index in list that obj appears5list.insert(index, obj)Inserts object obj into list at offset index6list.pop(obj=list[-1])Removes and returns last object or obj from list7list.remove(obj)Removes object obj from list8list.reverse()Reverses objects of list in place9list.sort([func])Sorts objects of list, use ... Read More
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
1K+ Views
Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string.In fact, lists respond to all of the general sequence operations we used on strings in the prior chapter.Python ExpressionResultsDescriptionlen([1, 2, 3])3Length[1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6]Concatenation['Hi!'] * 4['Hi!', 'Hi!', 'Hi!', 'Hi!']Repetition3 in [1, 2, 3]TrueMembershipfor x in [1, 2, 3]: print x,1 2 3Iteration
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
339 Views
To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know.Example Live Demo#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000]; print list1 del list1[2]; print "After deleting value at index 2 : " print list1OutputWhen the above code is executed, it produces the following result −['physics', 'chemistry', 1997, 2000] After deleting value at index 2 : ['physics', 'chemistry', 2000]Note − remove() method is discussed in subsequent section.
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
41K+ Views
You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method.Example Live Demo#!/usr/bin/python list = ['physics', 'chemistry', 1997, 2000]; print "Value available at index 2 : " print list[2] list[2] = 2001; print "New value available at index 2 : " print list[2]Note − append() method is discussed in subsequent section.OutputWhen the above code is executed, it produces the following result −Value available at index 2 : 1997 New value available at index 2 : 2001
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
351 Views
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index.Example Live Demo#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]OutputWhen the above code is executed, it produces the following result −list1[0]: physics list2[1:5]: [2, 3, 4, 5]