Placement Python-Tuples
Classes
YoganandSharma
A tuple is a sequence of immutable Python objects. Tuples are sequences, just
like lists. The differences between tuples and lists are, the tuples cannot be
Training > Project >+91-9928016573
changed unlike lists and tuples use parentheses, whereas lists use square
brackets.
y
Training by
d
Creating a tuple is as simple as putting different comma-separated values.
JMD Study Computer
also. For example −
tu
Optionally you can put these comma-separated values between parentheses
S
tup1 = ('physics', 'chemistry', 1997, 2000);
D
tup2 = (1, 2, 3, 4, 5 );
JM
tup3 = "a", "b", "c", "d";
The empty tuple is written as two parentheses containing nothing −
tup1 = ();
To write a tuple containing a single value you have to include a comma, even
though there is only one value −
tup1 = (50,);
Like string indices, tuple indices start at 0, and they can be sliced,
concatenated, and so on.
Python Notes 1
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Tuples
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Accessing Values in Tuples
by Training.
To access values in tuple, use the square brackets for slicing
+91-9928016573
along with the index or indices to obtain value available at that
Live Project
index. For example
−
y
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print ("tup1[0]: ", tup1[0])
St
D
print ("tup2[1:5]: ", tup2[1:5])
result −
tup1[0]: physics
JM
When the above code is executed, it produces the following
tup2[1:5]: [2, 3, 4, 5]
Updating Tuples
Python Notes 2
Assistance Python-Tuples
Classes
YoganandSharma
Tuples are immutable which means you cannot update or
change the values of tuple elements. You are able to take
+91-9928016573
portions of existing tuples to create new tuples as the
following
− example demonstrates
y
Training by
d
JMD Study Computer
Live Project & 100%Job
tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');
tu
S
D
# Following action is not valid for tuples
# tup1[0] = 100;
JM
# So let's create a new tuple as follows
tup3 = tup1 + tup2;
print (tup3)
Output:
(12, 34.56, 'abc', 'xyz')
Python Notes 3
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Tuples-Delete
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Delete Tuple Elements
by Training.
Removing individual tuple elements is not possible. There is, of
+91-9928016573
course, nothing wrong with putting together another tuple
Live Project
y
with the undesired elements discarded.
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
To explicitly remove an entire tuple, just use the del statement.
For example −
St
#!/usr/bin/python
D
JM
tup= ('physics', 'chemistry',
2000); print (tup)
1997,
del tup;
print "After deleting tup: ";
print (tup)
Python Notes 4
Placement Python-Tuples
Classes
YoganandSharma
This produces the following result. Note an
Training > Project >+91-9928016573
exception raised, this is because after del tuptuple
y
Training by
does not exist any more −
d
JMD Study Computer
tu
('physics', 'chemistry', 1997, 2000)
After deleting tup: S
D
Traceback(most recent call last):
JM
File "test.py", line 9, in <module>
print tup;
NameError: name 'tup' is not defined
Python Notes 5
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Tuples :Basic operation
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Basic Tuples Operations
by Training.
Tuples respond to the + and * operators much like strings; they
+91-9928016573
mean concatenation and repetition here too, except that the
Live Project
result is a new tuple, not a string.
y
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
Python Expression Result
len((1, 2, 3))3Length
St
D
JM
(1, 2, 3) + (4, 5, 6)(1, 2, 3, 4, 5, 6)Concatenation ('Hi!',) *
4('Hi!', 'Hi!', 'Hi!', 'Hi!')Repetition
3 in (1, 2, 3)TrueMembership
for x in (1, 2, 3): print x, 1 2 3Iteration
Python Notes 6
Assistance Python-Tuples
Classes
YoganandSharma
Indexing, Slicing, and Matrixes
Because tuples are sequences, indexing and slicing work
+91-9928016573
the same way for tuples as they do for strings. Assuming
y
Training by
−
following input
d
JMD Study Computer
Live Project & 100%Job
tu
L = ('spam', 'Spam', 'SPAM!')
S
D
Python ExpressionResultsDescription
JM
L[2]'SPAM!'Offsets start at zero
L[-2]'Spam‘ Negative: count from the
right
L[1:]['Spam', 'SPAM!']Slicing fetches
sections
No Enclosing Delimiters
Python Notes 7
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Tuples
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Any set of multiple objects, comma-separated,
by Training.
written without identifying symbols, i.e., brackets for
+91-9928016573
Live Project
lists, parentheses for tuples, etc., default to tuples, as
y
indicated in these short examples −
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
x, y = 1, 2; St
print ('abc', -4.24e93, 18+6.6j, 'xyz‘)
D
print ("Value of x , y : ", x,y)
output:
JM
abc-4.24e+93 (18+6.6j) xyz
Value of x , y : 1 2
Python Notes 8
Placement Python-Tuples-Function-len()
Classes
YoganandSharma
Description
Training > Project >+91-9928016573
Python tuple method len() returns the number of
y
Training by
elements in the tuple.
d
JMD Study Computer
Syntax
tu
len(tuple)
S
D
Parameters
JM
tuple −This is a tuple for which number of elements to be
counted.
Return Value
This method returns the number of elements in the tuple.
Python Notes 9
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Tuples
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
by Training.
Example
+91-9928016573
The following example shows the usage of len()
Live Project
method.
y
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
St
tuple1, tuple2 = (123, 'xyz', 'zara'), (456,
'abc') print ("First tuple length : ",
len(tuple1)) D
JM
print ("Second tuple length : ", len(tuple2))
Output:
First tuple length : 3
Second tuple length : 2
Python Notes 10
Assistance Python-Tuples-Function-max()
Classes
YoganandSharma
Description
Python tuple method max() returns the elements from the
+91-9928016573
tuple with maximum value.
y
Training by
Syntax
d
JMD Study Computer
Live Project & 100%Job
u
Following is the syntax for max() method −
t
max(tuple)
S
Parameters
D
tuple −This is a tuple from which max valued element to be
returned.
JM
Return Value
This method returns the elements from the tuple with
maximum value.
Python Notes 11
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Tuples
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
.
by Training.
+91-9928016573
Example
Live Project
y
The following example shows the usage of max() method.
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
Live Demo
#!/usr/bin/python
St
D
tuple1, tuple2 = (‘aad’, 'xyz', 'zara', 'abc'), (456, 700, 200)
JM
print ("Max value element : ", max(tuple1))
print ("Max value element : ", max(tuple2))
When we run above program, it produces following result −
Max value element : zara
Max value element : 700
Python Notes 12
Placement Python-Tuples-Function-min()
Classes
YoganandSharma
Description
Python tuple method min() returns the elements from the
Training > Project >+91-9928016573
tuple with minimum value.
y
Training by
Syntax
d
JMD Study Computer
u
Following is the syntax for min() method −
t
min(tuple)
S
Parameters
D
tuple −This is a tuple from which min valued element to be
returned.
JM
Return Value
This method returns the elements from the tuple with
minimum value.
Python Notes 13
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Tuples
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Example
by Training.
+91-9928016573
The following example shows the usage of min()
Live Project
method.
y
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
700, 200)
St
tuple1, tuple2 = (‘aad’, 'xyz', 'zara', 'abc'), (456,
D
print ("min value element : ", min(tuple1))
print ("min value element : ", min(tuple2))
Output:
JM
min value element : aad
min value element : 200
Python Notes 14
Assistance Python-Tuples-Function-tuples()
Classes
YoganandSharma
Description
+91-9928016573
Python tuple method tuple() converts a list of items
into tuples
y
Training by
d
JMD Study Computer
Live Project & 100%Job
Syntax
tu
tuple( seq)
S
D
Parameters
JM
seq−This is a sequence to be converted into tuple.
Return Value
This method returns the tuple.
Python Notes 15
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com YoganandSharma
+91-9928016573
Output:
method.
Example
JM
aTuple= tuple(aList)
D
Python-Tuples
St
aList= [123, 'xyz', 'zara', 'abc']
ud
print ("Tuple elements : ", aTuple)
y
Python Notes
Tuple elements : (123, 'xyz', 'zara', 'abc')
The following example shows the usage of tuple()
16