CodesJava

Easy learning with example program codes

Python Tuples


Python Tuples:

Python tuple is a collection or an array which is used to store various types of data.

Features of Python Tuples:

  • They are ordered and unchangeable.
  • They are written within round brackets.
  • They can store multiple types of data.
  • They are immutable. Thus, it cannot be modified.
  • Python Tuples supports various operations.
  • The elements of a python tuple are separated by commas and starts with index 0.

Python Tuples Methods:

METHODS USES
cmp() To compare two tuples.
count() Returns the number of times a specified value occurs in a tuple.
index() Searches the tuple for a specified value and returns the position of where it was found.
min() To get the minimum value from the tuple.
max() To get the maximum value from the tuple..
len() To get the number of elements in a tuple.
tuple() To convert a sequence type to a tuple.

Python Tuples Operators:

OPERATORS SYMBOLS USES
Concatenation + To add two tuples.
Replication * To repeat the tuple a specific number of times.
Slicing [i:j] To get a sub-tuple of specified start and end index.
Deleting del To delete an element from the tuple.

Examples:

a =  ("python", "java", "php", "HTML", "javascript", "CSS", "Bootstrap")
print "\n"
print "Tuple is:"
print a
print "\n"
 
print "Get the element at position 0."
print(a[0])
print "\n"
 
print "Get the elements from position 1 to position 4."
print(a[1:4])
print "\n"
 
print "Get the total length."
print(len(a))
print "\n"
 
print "Delete the tuple."
del a
print a
print "\n"

Output:

Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki