100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

Python objects TUPLES

Rating
-
Sold
-
Pages
3
Uploaded on
04-11-2022
Written in
2021/2022

Lecture notes of 3 pages for the course python at python (Python tuples)

Institution
Course








Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Course

Document information

Uploaded on
November 4, 2022
Number of pages
3
Written in
2021/2022
Type
Class notes
Professor(s)
Shah
Contains
All classes

Subjects

Content preview

Python objects II: TUPLES


TUPLES

The tuple object

A tuple may be thought of as an immutable list. Tuples are constructed by
placing the items inside parentheses:

>>> t = (1, ’two’, 3.)
>>> t
(1, ’two’, 3.0)

Tuples can be indexed and sliced in the same way as lists but, being immutable,
they cannot be appended to, extended, or have elements removed from them:

>>> t = (1, ’two’, 3.)
>>> t[1]
’two’
>>> t[2] = 4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ’tuple’ object does not support item assignment

Although a tuple itself is immutable, it may contain references to mutable
objects such as lists. Hence,

>>> t = (1, [’a’, ’b’, ’d’], 0)
>>> t[1][2] = ’c’ # OK to change the list within the tuple
>>> t
(1, [’a’, ’b’, ’c’], 0)

An empty tuple is created with empty parentheses: t0 = (). To create a tuple
containing only one item (a singleton), however, it is not sufficient to enclose
the item in parentheses (which could be confused with other syntactical use of
parentheses); instead, the lone item is given a trailing comma:t = (’one’,).

Uses of tuples

In some circumstances, particularly for simple assignments such as those in the
previous section, the parentheses around a tuple’s items are not required:

>>> t = 1, 2, 3
$8.49
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
kgazfazb

Get to know the seller

Seller avatar
kgazfazb Annamalai University
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
3 year
Number of followers
0
Documents
11
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions