

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 2
This page cannot be seen from the preview
Don't miss anything!
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.
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 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.
## 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') ```