Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Tuple - Python Program, Assignments of Computer Fundamentals

An overview of working with tuples in python. It covers various operations and methods that can be performed on tuples, such as accessing elements, checking for membership, modifying tuples, converting between tuples and lists, and unpacking tuples. The document also demonstrates the use of tuple assignments and the differences between tuples and lists. Additionally, it explores the concept of regions and how they can be extracted from a tuple using tuple unpacking. Overall, this document serves as a comprehensive guide for understanding and working with tuples in python programming.

Typology: Assignments

2023/2024

Uploaded on 10/24/2024

hugger
hugger 🇺🇸

4.7

(11)

923 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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') ```