Python Notes Day 6
Python Notes Day 6
By @Curious_.programmer
Lists and Tuples
Lists and tuples in Python both store collections of items.
Lists are mutable, allowing changes like adding or removing
elements, and use square brackets (`[ ]`).
Key Features:
1. append()
Adds a single element to the end of the list.
Syntax: list.append(element)
2. extend()
Extends the list by appending elements from an iterable
(like another list).
Syntax: list.extend(iterable)
3. insert()
Inserts an element at a specified position in the list.
Syntax: list.insert(index, element)
4. remove()
Removes the first occurrence of a specified element from
the list.
Syntax: list.remove(element)
5.pop()
Removes and returns the element at a specified position
(index). If no index is specified, it removes and returns
the last element.
Syntax: list.pop(index)
Indexing
Key Points:
1.Positive Indexing:
3. Indexing in Strings:
4. Out-of-Range Index:
Slicing
List Comprehension
Syntax:
expression: The value or operation applied to each item.
item: The variable representing the current element in the
iteration.
iterable: The collection (like a list, tuple, or range) that you
are iterating over.
condition: (Optional) A filter that decides whether the
expression should be applied to the current item.
Tuple Unpacking:
Tuple unpacking is the reverse process, where the values in a
tuple are extracted and assigned to individual variables.
CodeWithCurious Store