Nothing Special   »   [go: up one dir, main page]

0% found this document useful (0 votes)
157 views23 pages

Python Tuple

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 23

Python: Tuple

Tuple
• Tuples are used to store multiple items in a single variable.
• Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are
• List,
• Set, and
• Dictionary
• Tuples are like lists, but their elements are fixed; that is,
• once a tuple is created, one cannot add new elements, delete elements, replace elements, or reorder the elements in
the tuple
t1 = () # Create an empty tuple
t2 = (1, 3, 5) # Create a tuple with three elements
# Create a tuple from a list
t3 = tuple([2 * x for x in range(1, 5)])
# Create a tuple from a string
t4 = tuple("abac") # t4 is ['a', 'b', 'a', 'c']
Tuple vs List
Accessing Values in Tuples in Python
• Method 1: Using Positive Index
var = ("Geeks", "for", "Geeks")
  
print("Value in Var[0] = ", var[0])
print("Value in Var[1] = ", var[1])
print("Value in Var[2] = ", var[2])

• Method 2: Using Negative Index


var = ("Geeks", "for", "Geeks")
  
print("Value in Var[-3] = ", var[-3])
print("Value in Var[-2] = ", var[-2])
print("Value in Var[-1] = ", var[-1])
Slicing
# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')
print(my_tuple[1:4])
print(my_tuple[:-7])
print(my_tuple[7:])
print(my_tuple[:])
Changing a Tuple
• Tuples are immutable. This means that elements of a tuple cannot be changed once
they have been assigned but
• if the element is itself a mutable data type like a list, its nested items can be changed
# Changing tuple values
my_tuple = (4, 2, 3, [6, 5])

# However, item of a mutable element can be changed


my_tuple[3][0] = 9
print(my_tuple)

# Tuples can be reassigned


my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple)
Delete Tuple Elements
• Removing individual tuple elements is not possible
• To explicitly remove an entire tuple, just use the del statement
tup = ('physics', 'chemistry', 1997, 2000);
print (tup)
del tup;
print ("After deleting tup : ")
print (tup)
Basic Tuples
Operations
• Tuples respond to the + and *
operators much like strings;
they mean concatenation and
repetition except that the
result is a new tuple, not a
string
Concatenation of Tuples in Python
# Code for concatenating 2 tuples

tuple1 = (0, 1, 2, 3)
tuple2 = ('python', 'geek')

# Concatenating above two


print(tuple1 + tuple2)
Nesting of Tuples
# Code for creating nested tuples

tuple1 = (0, 1, 2, 3)
tuple2 = ('python', 'geek')
tuple3 = (tuple1, tuple2)
print(tuple3)
Repetition Tuples
# Code to create a tuple with repetition

tuple3 = ('python',)*3
print(tuple3)
Finding Length of a Tuple
# Code for printing the length of a tuple

tuple2 = ('python', 'geek')


print(len(tuple2))
Converting list to a Tuple
# Code for converting a list and a string into a tuple

list1 = [0, 1, 2]
print(tuple(list1))
print(tuple('python')) # string 'python'
Obtain a list from a tuple

tuple2 = tuple([7, 1, 2, 23, 4, 5])


list1 = list(tuple2)
Tuples in a loop
# python code for creating tuples in a loop

tup = ('geek',)
n = 5 # Number of time loop runs
for i in range(int(n)):
tup = (tup,)
print(tup)
No Enclosing Delimiters
• Any set of multiple objects, comma-separated, written without
identifying symbols, i.e., brackets for lists, parentheses for tuples, etc.,
default to tuples

print 'abc', -4.24e93, 18+6.6j, 'xyz';


x, y = 1, 2;
print "Value of x , y : ", x,y;
Built-in
Tuple
Functions
Tuple cmp() Method
• If elements are of the same type, perform the comparison and return the
result. If elements are different types, check to see if they are numbers.
• If numbers, perform numeric coercion if necessary and compare
• If either element is a number, then the other element is "larger" (numbers are
"smallest").
• Otherwise, types are sorted alphabetically by name.
tuple1, tuple2 = (123, 'xyz'), (456, 'abc')
print cmp(tuple1, tuple2)
print cmp(tuple2, tuple1)
tuple3 = tuple2 + (786,);
print cmp(tuple2, tuple3)
Tuple max() Method
• This method returns the elements from the tuple with maximum
value
tuple1, tuple2 = (123, 'xyz', 'zara', 'abc'), (456, 700, 200)
print "Max value element : ", max(tuple1)
print "Max value element : ", max(tuple2)
Tuple min() Method
• This method returns the elements from the tuple with minimum
value
tuple1, tuple2 = (123, 'xyz', 'zara', 'abc'), (456, 700, 200)
print "min value element : ", min(tuple1)
print "min value element : ", min(tuple2)
Tuple tuple() Method
• This method returns the tuple

aList = [123, 'xyz', 'zara', 'abc']


aTuple = tuple(aList)
print "Tuple elements : ", aTuple
Questions
• What are the differences between a list and a tuple?
• How do you create a tuple from a list?
• How do you create a list from a tuple?
• What is wrong in the following code?
t = (1, 2, 3)
t.append(4)
t.remove(0)
t[0] = 1
• Is the following code correct?
t1 = (1, 2, 3, 7, 9, 0, 5)
t2 = (1, 2, 5)
t1 = t2
Questions
• Show the printout of the following code:
t = (1, 2, 3, 7, 9, 0, 5)
print(t)
print(t[0])
print(t[1: 3])
print(t[-1])
print(t[ : -1])
print(t[1 : -1])
• Show the printout of the following code:
t1 = (1, 2, 3, 7, 9, 0, 5)
t2 = (1, 3, 22, 7, 9, 0, 5)
print(t1 == t2)
print(t1 != t2)
print(t1 > t2)
print(t1 < t2)

You might also like