0% found this document useful (0 votes)
12 views2 pages

Class 11 Python Notes List Tuple Dictionary String

The document provides notes on four fundamental Python data structures: List, Tuple, Dictionary, and String, detailing their characteristics, creation, access methods, and common functions. Lists and Dictionaries are mutable, while Tuples and Strings are immutable. It also includes a quick comparison table and exam tips for effective revision.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Class 11 Python Notes List Tuple Dictionary String

The document provides notes on four fundamental Python data structures: List, Tuple, Dictionary, and String, detailing their characteristics, creation, access methods, and common functions. Lists and Dictionaries are mutable, while Tuples and Strings are immutable. It also includes a quick comparison table and exam tips for effective revision.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

■ Class 11 Python Notes

List, Tuple, Dictionary, and String

These notes are designed for quick learning and revision with examples, tables, and clear explanations.

■ 1. LIST (Mutable Sequence)

What is a List? A list is a collection of items stored in a single variable. It is ordered, mutable (changeable), allows
duplicate values, and can store different data types.

Creating a List: Example: marks = [90, 85, 78, 92], names = ["Aman", "Riya", "Sita"], mixed = [1, "Hello", 3.5, True]

Accessing Elements: Use indexing like marks[0] (first) and marks[-1] (last).

Slicing: marks[1:3] gives elements from index 1 to 2.

Common Methods: append(), insert(), remove(), pop(), sort(), reverse(), len().

Changing Elements: marks[0] = 100 (Allowed because list is mutable).

■ 2. TUPLE (Immutable Sequence)

What is a Tuple? A tuple is like a list but immutable (cannot be changed). It is ordered and allows duplicates.

Creating a Tuple: t = (10, 20, 30), t2 = ("A", "B", "C"), single = (5,)

Accessing Elements: Use indexing like t[0] or t[-1].

Immutability: t[0] = 100 is not allowed (Error).

Common Methods: len(), max(), min(), count(), index().

Use: When data should not change (e.g., days of week, coordinates).

■ 3. DICTIONARY (Key–Value Pairs)

What is a Dictionary? A dictionary stores data in key : value pairs. It is mutable. Keys are unique, values can
repeat.

Creating: student = {"name": "Rahul", "age": 16, "marks": 85}

Accessing: student["name"] or [Link]("age").

Adding/Changing: student["grade"] = "A", student["age"] = 17.

Deleting: del student["marks"], [Link]("age").

Important Methods: keys(), values(), items(), update().


■ 4. STRING (Sequence of Characters)

What is a String? A string is a sequence of characters written in single or double quotes. It is immutable and
indexed.

Creating: s = "Hello", name = 'Python'.

Accessing: s[0] gives first character, s[-1] gives last.

Slicing: s[1:4] gives 'ell'.

Immutability: s[0] = 'h' is not allowed (Error).

Common Functions: len(), lower(), upper(), title(), strip(), replace(), find().

Concatenation: "Hello" + " " + "World" = "Hello World".

■ Quick Difference Table

Feature List Tuple String Dictionary

Mutable Yes No No Yes


Ordered Yes Yes Yes Yes (Insertion Order)
Indexing Yes Yes Yes No (Uses Keys)
Stores Values Values Characters Key–Value Pairs

■ Exam Tips

Remember: List and Dictionary are mutable. Tuple and String are immutable.

Dictionary uses keys, not index numbers.

Be prepared for: definitions, differences, methods, and small programs.

Common questions

Powered by AI

Understanding the concept of immutability is crucial when working with strings in Python because it directly impacts how strings can be manipulated and stored. Since strings are immutable, any operation that modifies a string results in the creation of a new string object rather than changing the original one. This behavior has important implications for memory management and performance, as frequent string modifications may lead to increased memory usage and slower performance due to the overhead of creating multiple string objects . Moreover, immutability ensures that strings are thread-safe and can be reliably used in concurrent applications without the risk of data inconsistency . Recognized understanding of immutability helps developers write more efficient, predictable, and bug-free code by avoiding unintended side effects that can occur with mutable objects.

Tuples are preferred over lists in scenarios where data integrity is crucial because they are immutable. This immutability ensures that data remains constant throughout the program's execution, making tuples ideal for storing constants such as days of the week or fixed coordinates . Moreover, because tuples are immutable, they can be used as keys in dictionaries, whereas lists cannot. The use of tuples also implies that the data within will not inadvertently change, which can be beneficial for debugging and reliability .

The 'pop' method is significant in managing lists and dictionaries in Python due to its dual role in removing elements while returning the removed value, which is useful for further processing. In lists, pop() removes and returns the last element by default or an element at a specified index, facilitating dynamic list modifications without needing to manually manage indices . This capability is crucial in algorithms that require the extraction of the last inserted or a specific element while maintaining list integrity. Similarly, in dictionaries, pop(key) removes and returns the value associated with the specified key, which is particularly beneficial for situations where key-value pairs must be deleted for memory management or as part of data processing routines . The ability to both remove and retrieve elements simultaneously enhances code efficiency and clarity, making 'pop' an essential tool for managing these data structures in Python.

Indexing plays a vital role in accessing elements stored within lists, tuples, and strings in Python. Each of these data structures is ordered, meaning that they maintain a consistent sequence of elements, which can be accessed using numerical indices. In lists, indexing starts from 0, allowing access to each element based on its position; for instance, marks[0] retrieves the first element, and marks[-1] retrieves the last. This same principle applies to tuples and strings, where each numeral corresponds to an element or character . Indexing enables precise retrieval and manipulation of data at any position within these structures, facilitating tasks like iterating over elements, slicing segments, and performing various operations that involve specific parts of the dataset. Consequently, understanding indexing is key to effectively harnessing the full capabilities of these fundamental data structures in Python .

Lists and strings differ primarily in their purposes and functionality. A list can store elements of multiple data types and is designed for storing collections of items that can be manipulated. Lists are mutable, allowing for item modification, addition, and deletion using methods such as append(), remove(), and pop(). Strings, however, are sequences of characters primarily used for text processing. They are immutable and any modification operations result in a new string being created. Unlike lists, strings have specific character manipulation functions like lower(), upper(), and strip() to assist in text formatting .

Using the dictionary's update() method in Python provides several advantages over manually adding each key-value pair. The update() method allows for the efficient addition or modification of multiple key-value pairs simultaneously. This is not only concise but also minimizes repetitive code and reduces potential errors associated with writing multiple individual assignment operations. Additionally, using update() can lead to performance benefits, as the method is optimized for setting multiple entries at once, reducing the overhead of repeated dictionary lookups and insertions. This method is particularly beneficial when importing data from another dictionary or when implementing batch updates, thereby improving the maintainability and readability of the code .

A dictionary in Python is significantly different from lists, tuples, and strings primarily because it stores data in key-value pairs rather than sequences. Unlike these sequence-based structures, dictionaries are not accessed via indices but through unique keys, offering more efficient data retrieval when the key is known. This key-based access allows for faster lookups and is especially useful in cases where data needs to be quickly retrieved or updated based on meaningful identifiers, such as in databases or configurations . Additionally, dictionaries are mutable, meaning that their contents can be changed without creating a new object, which is a marked contrast to the immutability of strings and tuples. This flexibility, combined with its unique key-value pair structure, makes dictionaries particularly powerful for handling large data sets and complex data structures .

Slicing a list or a string in Python can be highly beneficial in scenarios where only a portion of the data is relevant to the task at hand. For lists, slicing allows for easy extraction of sublists from large datasets, which is useful in data analysis and manipulation tasks. Slicing can help in selectively processing only a segment of the data, which can reduce computation time and improve the efficiency of the program . For strings, slicing enables substring extraction, which is a common requirement in text processing tasks such as parsing, data cleaning, or generating summaries. Whether working with lists or strings, slicing provides a clean and efficient way to access and manipulate specific sections of data without needing extensive loops or complex conditions .

Lists and dictionaries are mutable, meaning their elements can be changed after creation. You can add new items, or modify or remove existing ones. For example, in a list, you can do operations such as append() and pop() to alter the elements. Similarly, in a dictionary, you can add or change key-value pairs and use methods like update(). On the other hand, tuples and strings are immutable. Once created, their contents cannot be changed. For instance, attempting to change an element in a tuple or alter a character in a string will result in an error .

In a Python list, you can directly modify any element due to its mutable nature. For example, in a list marks = [90, 85, 78, 92], you can change the first element by executing marks[0] = 100, which will update the list to [100, 85, 78, 92]. Conversely, attempting to perform a similar operation on a tuple will result in an error, as tuples are immutable. For example, if you have a tuple t = (10, 20, 30) and write t[0] = 100, Python will raise a TypeError, indicating that tuple objects do not support item assignment . This illustrates the fundamental difference in mutability between lists and tuples.

You might also like