From 7d0f08f6ffa9cbdf08c62437f8a1cb9261c9576a Mon Sep 17 00:00:00 2001 From: Dhrubaraj Pati Date: Sun, 29 Jun 2025 19:13:15 +0530 Subject: [PATCH] docs: add comprehensive Python Lists chapter - Introduced detailed explanation of Python Lists - Included examples of creation, indexing, slicing, and modification - Documented all common list methods with code samples - Added sections on list comprehension, nested lists, and copying - Prepared markdown ready for integration into documentation site Close: #133 Co-Authored-By: Sanjay Viswanathan --- docs/python/python-list.md | 260 +++++++++++++++++++++++++++++++ docs/python/setup-environment.md | 2 +- 2 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 docs/python/python-list.md diff --git a/docs/python/python-list.md b/docs/python/python-list.md new file mode 100644 index 00000000..55b8ece6 --- /dev/null +++ b/docs/python/python-list.md @@ -0,0 +1,260 @@ +--- +id: python-list +title: List in Python +sidebar_label: List in Python #displays in sidebar +sidebar_position: 8 +tags: + [ + Python, + List in Python, + Introduction of python, + Python Syntax, + Variables, + Operators, + Type Casting, + String + ] + +--- + + +# Python Lists + +A **List** in Python is a data structure that allows you to store multiple items in a single variable. Lists are **ordered**, **mutable**, and **can contain elements of different data types**. + + +## Creating a List + +You create a list using square brackets `[]`: + +```python +# Empty List +empty_list = [] + +# List of Integers +numbers = [1, 2, 3, 4, 5] + +# List of Strings +fruits = ["apple", "banana", "cherry"] + +# Mixed Data Types +mixed = [1, "hello", 3.14, True] +```` + + + +## Indexing + +**Indexing** means accessing elements by their position. + +* Index starts from **0** in Python: + +```python +fruits = ["apple", "banana", "cherry"] + +print(fruits[0]) # apple +print(fruits[1]) # banana +print(fruits[2]) # cherry +``` + +* Negative indexing starts from the end: + +```python +print(fruits[-1]) # cherry +print(fruits[-2]) # banana +print(fruits[-3]) # apple +``` + + + +## Slicing + +**Slicing** lets you extract a sublist: + +```python +numbers = [10, 20, 30, 40, 50] + +print(numbers[1:4]) # [20, 30, 40] +print(numbers[:3]) # [10, 20, 30] +print(numbers[2:]) # [30, 40, 50] +print(numbers[-3:-1]) # [30, 40] +``` + +**Syntax:** + +``` +list[start:stop:step] +``` + +**Example with step:** + +```python +print(numbers[::2]) # [10, 30, 50] +``` + + +## Modifying Elements + +Lists are **mutable**, which means you can change their contents: + +```python +fruits = ["apple", "banana", "cherry"] +fruits[1] = "mango" +print(fruits) # ['apple', 'mango', 'cherry'] +``` + + +## List Methods + +Python provides many built-in methods for lists: + +| Method | Description | +| -------------- | ----------------------------------------------------- | +| `append(x)` | Adds an item to the end of the list | +| `insert(i, x)` | Inserts an item at a specific index | +| `extend(iter)` | Adds all elements from another iterable | +| `remove(x)` | Removes the first occurrence of the item | +| `pop([i])` | Removes and returns the item at the given index | +| `clear()` | Removes all elements | +| `index(x)` | Returns the index of the first occurrence of the item | +| `count(x)` | Counts how many times the item appears | +| `sort()` | Sorts the list in ascending order | +| `reverse()` | Reverses the list | +| `copy()` | Returns a shallow copy of the list | + +--- + +### Examples + +#### append() + +```python +nums = [1, 2, 3] +nums.append(4) +print(nums) # [1, 2, 3, 4] +``` + +#### insert() + +```python +nums.insert(1, 100) +print(nums) # [1, 100, 2, 3, 4] +``` + +#### extend() + +```python +nums.extend([5, 6]) +print(nums) # [1, 100, 2, 3, 4, 5, 6] +``` + +#### remove() and pop() + +```python +nums.remove(100) +print(nums) # [1, 2, 3, 4, 5, 6] + +nums.pop() # Removes the last element +print(nums) # [1, 2, 3, 4, 5] + +nums.pop(2) # Removes index 2 +print(nums) # [1, 2, 4, 5] +``` + + +## Iterating Through a List + +**Using a for loop:** + +```python +fruits = ["apple", "banana", "cherry"] + +for item in fruits: + print(item) +``` + +**Output:** + +``` +apple +banana +cherry +``` + +**Using indices:** + +```python +for i in range(len(fruits)): + print(i, fruits[i]) +``` + + +## Membership Test + +Check whether an item exists in the list: + +```python +print("apple" in fruits) # True +print("mango" not in fruits) # True +``` + + +## Nested Lists + +Lists can contain other lists: + +```python +matrix = [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9] +] + +print(matrix[0]) # [1, 2, 3] +print(matrix[1][2]) # 6 +``` + + +## List Comprehensions + +A **concise way** to create new lists: + +```python +squares = [x**2 for x in range(1, 6)] +print(squares) # [1, 4, 9, 16, 25] +``` + +**With a condition:** + +```python +even = [x for x in range(10) if x % 2 == 0] +print(even) # [0, 2, 4, 6, 8] +``` + + +## Copying Lists + +Be careful! Assigning directly creates a reference: + +```python +a = [1, 2, 3] +b = a +b.append(4) + +print(a) # [1, 2, 3, 4] +``` + +To create an **independent copy:** + +```python +c = a.copy() +c.append(5) + +print(a) # [1, 2, 3, 4] +print(c) # [1, 2, 3, 4, 5] +``` + + +## Conclusion + +Python Lists are a **powerful and flexible** data structure used everywhere—from collecting and processing data to building complex programs. Practice using list methods and experiment to become confident. diff --git a/docs/python/setup-environment.md b/docs/python/setup-environment.md index fec9fcea..66391a0a 100644 --- a/docs/python/setup-environment.md +++ b/docs/python/setup-environment.md @@ -2,7 +2,7 @@ id: setup-environment title: Setting up your development environment sidebar_label: Setting up environment -sidebar_position: 8 +sidebar_position: 9 tags: [ html,