0% found this document useful (0 votes)
49 views15 pages

Tuples and Dictionary

It's for class 11th CS students.

Uploaded by

Harshita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
49 views15 pages

Tuples and Dictionary

It's for class 11th CS students.

Uploaded by

Harshita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 15
>See, \ by pt TUPLES & , {gPICTIONARY, pe \ resente d by Mahi Yadav X OC oe Ba “Ns Welcome to "Mastering Tuples and Dictionaries in Python"! rey In the vast landscape of Python's capabilities, the proficiency to handle data efficiently is paramount. Today, we embark on a journey to explore two fundamental data structures - Tuples and Dictionaries. As cornerstones of Python programming, understanding these structures opens doors to more elegant and effective code. In this presentation, we'll delve into the intricacies of Tuples and Dictionaries, unraveling their unique features, use cases, and advanced techniques. Let's navigate through the power and versatility these data structures bring to your Python toolkit. Join us on this exploration, and by the end, you'll be equipped to leverage Tuples and Dictionaries with finesse in your Python projects. Let's dive in! TUPLE my-_tuple = (element1, elementz, A tuple isan ordered collection of elements, and it's similar to a list in python. The key difference is that tuples eral are immutable, meaning their elements can't be changed. my_tuple = (1, "apple", 3.14) + Tuples are immutable, but members of tuple may be mutable \Y . EWN TRAVERSING A TUPLE peNN\ \ ze a my_tuple = (1, 2, 3, 4) my_tuple = ("apple", "banana", "cherry") Fle ia aral amet lio for jin range(len(my_tuple)): print(element) oda seo) CE Maa CS es aa) lao OANA Le Tel aN) PU Ctra (sed nC) TUPLE OPERATIONS Tuples support various operations in python: OLS ea 4 (2) REPETITION YN CONCATENATION my_tuple = (4, 5) repeated_tuple = my_tuple *3 (4,544,554, 5) result = tupll + tuple? #, ) INDEXING ® colors = red’, 'green’,'blue’y 20,30, 40,50) first_color = colors[0] # ed" my_tuple{l:4] TUPLE FUNCTIONS Returns the Length ( number of Counts the occurrences of a elements) in a tuple. specified element in a tuple. Example : Example: smy_suple = (,2,3,4,5) numbers = (1,2, 3,2,4,2) ws length = len(my_tuple) #5 count_of_2 = numbers.count(2) #3 + Creates a new shorted tuple from the elements of original tuple . Example: Feredieretee Genet] sorted_tuple = tuple(sorted(unsorted_tuple)) # (42,3,5,8) Returns the minimum or maximum value in a tuple , respectively. Example + numbers = (10, 5, 8, 3,15) max_value = max(numbers) #15 ‘min_value = min(numbers) #3 @ DICTIONARY aes COOL CE eC rence Cea ee Adictionary in Python is an Paar Aree ree es etre ae Elements in dictionaries have no specific order value pairs. It allows efficient [3 mutable data retrieval based on keys, nee eee Rae eek eee entitle Mad Nauta 2 i Piemonte) eee 4 Elements are indexed by keys., not their position or dynamically sized and support} incicies ROUEN Me RCE oom ere einen een eet aero Yee rr Yj Fe aed Oe es ay T >PVO® METHODS TO ee CREATE ve DICTIONARY METHOD 2 Using dict() function : METHOD | key_value_pairs = [(‘name', 'Alice'), (‘age', 30), Using curly braces : my_dict = {'keyl': 'valuel', (‘city', 'Wonderland')] 'value2'} @ my_dict = dict(key_value_pairs) “METHOD 3, a METHOD 4 Using string data type : By passing nested list as an string_di argument to dict( ) function dict(name="Alice', age="30', string dict = dict(name="Alice’, age="30', city="Wonderland") city="Wonderland’) ACCESSING ELEMENTS | Retrive a value using the get () method , which is safer if the key OTT ar la eee tao access a value using the key inside Tocris Eels) my_dict = {'name': ‘john’, my_dict = ('name': John’, ‘age!: 25, 'city': 'New York'} # Accessing values using + square brackets name_value = my_dict['name'] age_value = my_dict['age'] print(f"Name: {name_value}, Age: {age_value}") ‘age': 5, ‘city's 'New York'} name_value=my_dict.get('nam ) age_value = my_dict.get('age') print(f"Name: {name_value}, Age: {age_value}" if name_value is not None and age_value is not None else + "Key not found.") REMOVING ELEMENTS Manigetsn) . + Removes an item based on the Deletes a specific key or the specified key and returns its entire dictionary using the del value. Raises a KeyError if the statement. Pefencaurns $ imy-dict= (name's John, ‘age’: 25, ‘city's'New _ mwy_dict={'name's ohn’, 'age': 25, city':'New Yerk} York’ Frame ine jars Removing ‘city’ hey ete ete eter) del my- dict] print("Dictionar after pop(:", my_dic) print(?Dictionary after del", my_dict) print("Removed Age:", removed_age) oro) + copy () cela 0) Prete} *len() + keys () * update () + pop () Peeitceae) + popitems () clear () Ce oh Ro oOo ieee) Eee PTW ee aa aoe elt cn) copy () original_dict = {‘name': ohn’, ‘age’: 25, ‘city’ ‘New Sta) copied dict = original_dict.copy() aN Reread RT) age_value copied_dict.get(‘age') print("Age Value:", age_value) -opied_dict.items() print("Items List", items_list) len () copied_dic = len(copied_dict) print("Length:", length) {iname': 'John', update () copied_dict = {'name': john’, ‘age': 25, ‘city's 'New York'} updated _dict = (‘country's 'USA’, ‘gender’: 'Male'} copied_dict.update(updated_d ict) print("Updated Dictionary:", copied_dict) pop () copied_dict = {'name': john’, ‘age': 25, ‘city's 'New York'} name_value = copied_dict-pop(‘name') print("Name Value (after pop()):", name_value) values () iny_dict= (name's Alice’ ‘age': 30, ‘city’: Wonderland} 4# Using values() to get alist of values values_list = Uist(my_dict-values()) print(*Original Dictionary:", my dict) print("Values Lists", values list) Popitems () copied_dict = {'name': john’, ‘age': 25, ‘city's 'New York'} last_item = copied_dict.popitem() print("Last Item (after popitem()):", last_item) CONCLUSION Tuples: Tuples provide an immutable sequence of elements, ensuring data integrity. They are particularly useful for heterogeneous collections and scenarios where the order and values should remain constant. dictionaries in Python offer a versatile key-value mapping with dynamic and mutable characteristics. They provide efficient data retrieval based on keys, making them valuable for various applications where association and manipulation of data are key requirements. THANK YOU

You might also like