Python Chapter8 Tuple.pdf
Python Chapter8 Tuple.pdf
Classes
YoganandSharma
y
Training by
d
Creating a tuple is as simple as putting different comma-separated values.
JMD Study Computer
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
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
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
d
JMD Study Computer
Live Project & 100%Job
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
y
with the undesired elements discarded.
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
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
y
Training by
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
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
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
−
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
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
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
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:
Description
Python tuple method max() returns the elements from the
+91-9928016573
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
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
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