0% found this document useful (0 votes)
12 views

Python Lec 04

Uploaded by

Hardik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python Lec 04

Uploaded by

Hardik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

List Definition and Use Cases

• Definition: A list is a mutable, ordered collection of items in Python. It allows you


to store a sequence of elements under a single variable name.
• Use Cases: Lists are incredibly versatile and can be used for a wide range of
purposes, including:
o Storing shopping lists, to-do items, or any group of related data.
o Representing sequences of numbers, characters, or any other data type.
o Creating multi-dimensional data structures (like matrices) by storing lists
within lists.
o Working with data from external sources like files or APIs.
Why Use Lists?
• Flexibility: Lists can hold elements of different data types within the same list,
making them adaptable to various data scenarios.
• Mutability: Unlike strings and tuples, lists are mutable, meaning you can modify
their contents after creation. You can add, remove, or change elements
dynamically.
• Ordering: Lists preserve the order in which elements are inserted. This is crucial
when you need to maintain the sequence of data.
Why Lists Can Be Better Than Arrays (in some cases):
While arrays in other languages offer similar functionality, Python's lists provide some
advantages:
• Heterogeneity: Lists can store elements of different data types, whereas arrays
in some languages may be restricted to a single type.
• Slicing: Python lists support powerful slicing operations for extracting or
modifying sub-sequences, making them more convenient for data manipulation.
• Built-in Methods: Lists come with a rich set of built-in methods for common
operations like sorting, searching, and modifying elements, reducing the need to
write custom logic.
List Slicing
Slicing is a powerful technique in Python for extracting or modifying sub-sequences of a
list. Here's the syntax:
Python


start: Index of the first element to include (inclusive). Defaults to 0 (beginning).
• end: Index of the first element to exclude (exclusive). Defaults to the list's length.
• step: Optional step size for iterating through the list. Defaults to 1 (every

element).
Examples:
Python

Common List Methods


Python provides numerous built-in methods for working with lists. Here are some
commonly used ones:
• append(item): Adds an item to the end of the list.
• insert(index, item): Inserts an item at a specific index.
• remove(item): Removes the first occurrence of an item from the list.
• pop(index): Removes and returns the item at a specific index (or the last item if

no index is provided).
• index(item): Returns the index of the first occurrence of an item.
• sort(): Sorts the list in ascending order (by default).
• count(item): Counts the number of occurrences of an item in the list.
• extend(iterable): Extends the list by appending all items from an iterable (like

another list or a string).


• clear(): Removes all items from the list.

Code Examples
Here are some code examples demonstrating list operations:
Python

Lists vs. Strings: Mutability and Immutability


In Python, data types can be classified into mutable and immutable. This refers to
whether the content of the variable can be changed after its creation.
• Mutable Lists: Lists are mutable, meaning you can modify their elements after
they are created. You can add, remove, or change the order of items within a list.
• Immutable Strings: Strings in Python are immutable. Once a string is created,
its content cannot be directly modified. If you try to change a string, Python will
create a new string object with the modifications and assign it to a new variable.
Code: - List being mutable

Code: - Strings being immutable

Error: ------

Happy Birthday Naisha!

You might also like