Docsity

Tuple - Python Program, Assignments of Computer Fundamentals

Toggle sidebar
bg1
Exploring Tuples in Python: A
Comprehensive Guide
Tuples in Python
Tuple Basics
Tuples are immutable data structures in Python, meaning their
elements cannot be modified after creation.
Tuples are defined using parentheses () and can contain elements of
different data types.
Unlike lists, tuples do not support item assignment, as they are
immutable.
Tuple Operations
Accessing elements in a tuple is done using indexing, similar to lists.
Tuples do not have methods like append() or insert() to modify their
contents.
Tuples can be concatenated using the + operator to create a new tuple.
Tuples can be repeated using the * operator.
Tuple Unpacking
Tuple unpacking allows you to assign multiple variables at once from
the elements of a tuple.
This is done using the * operator to capture the remaining elements in
a list.
Examples
```python
Creating a tuple
T = ('Amsterdam', 'Paris', 'Karachi')
Attempting to modify a tuple
element
T[1] = 'Zurich' # TypeError: 'tuple' object does not support item assignment
•
•
•
•
•
•
•
•
•
pf2

Partial preview of the text

Download Tuple - Python Program and more Assignments Computer Fundamentals in PDF only on Docsity!

Exploring Tuples in Python: A

Comprehensive Guide

Tuples in Python

Tuple Basics

Tuples are immutable data structures in Python, meaning their elements cannot be modified after creation. Tuples are defined using parentheses () and can contain elements of different data types. Unlike lists, tuples do not support item assignment, as they are immutable.

Tuple Operations

Accessing elements in a tuple is done using indexing, similar to lists. Tuples do not have methods like append() or insert() to modify their contents. Tuples can be concatenated using the + operator to create a new tuple. Tuples can be repeated using the * operator.

Tuple Unpacking

Tuple unpacking allows you to assign multiple variables at once from the elements of a tuple. This is done using the * operator to capture the remaining elements in a list.

Examples

 ## Creating a tuple T = ('Amsterdam', 'Paris', 'Karachi') ## Attempting to modify a tuple ## element T[1] = 'Zurich' # TypeError: 'tuple' object does not support item assignment ## Creating a list and converting it to ## a tuple cities = ['Berlin', 'Lahore', 'Paris', 'Karachi', 'Mumbai'] R = tuple(cities) print(R) # Output: ('Berlin', 'Lahore', 'Paris', 'Karachi', 'Mumbai') ## Concatenating and repeating ## tuples W = ('Berlin', 'Amsterdam', 'Zurich', 'Karachi', 'Mumbai') T = W + ('Dhaka',) print(T) # Output: ('Berlin', 'Amsterdam', 'Zurich', 'Karachi', 'Mumbai', 'Dhaka') T = T * 5 print(T) # Output: ('Berlin', 'Amsterdam', 'Zurich', 'Karachi', 'Mumbai', 'Dhaka', 'Berlin', 'Amsterdam', 'Zurich', 'Karachi', 'Mumbai', 'Dhaka', 'Berlin', 'Amsterdam', 'Zurich', 'Karachi', 'Mumbai', 'Dhaka', 'Berlin', 'Amsterdam', 'Zurich', 'Karachi', 'Mumbai', 'Dhaka', 'Berlin', 'Amsterdam', 'Zurich', 'Karachi', 'Mumbai', 'Dhaka') ## Tuple unpacking Regions = ('South Asia', 'Central Asia', 'Middle East', 'South East Asia') SA, CA, *MS = Regions print(SA) # Output: South Asia print(CA) # Output: Central Asia print(MS) # Output: ['Middle East', 'South East Asia'] ``` ## Data Types in Tuples Tuples can contain elements of different data types, such as strings, integers, and floats. python a = 'String' b = 18 c = 18. ## Membership Testing You can use the in operator to check if an element is present in a tuple. ```python if 'Karachi' in T: print('Yes') else: print('No') if 'Kabul' in T: print('Yes') else: print('No') ```