Python Training-Week3

Data Structures

What are Lists in Python

Lists in Python are ordered collections of items that can be of mixed types. They are mutable, meaning their content can be changed.

Features of Lists in Python

Dynamic and Flexible

Lists in Python are dynamic, meaning they can grow or shrink in size as needed. This flexibility allows programmers to add or remove elements without worrying about the size of the list, making lists an incredibly flexible data structure for managing collections of items.

Heterogeneous Elements

Python lists can contain elements of different data types within the same list. This means a single list can hold integers, floats, strings, and even other lists or complex objects. This heterogeneity makes lists extremely versatile for various programming tasks.

Mutable

Lists are mutable, which means that their content can be changed after they are created. You can modify elements, add new ones, or remove existing ones. This ability to change lists in place is powerful but requires careful management to avoid unintended side effects in your code.

Ordered

The elements in a list are ordered, meaning that each element has a specific position or index. This order is maintained, and when you access the elements of a list, they will be returned in the order they were added. The index of the first element is 0, not 1, which is a common source of errors for beginners.

Support for Nested Lists

Lists can contain other lists as elements, allowing for the creation of nested or multidimensional lists. This capability is particularly useful for representing matrices, grids, or any hierarchical data

Basic creation of Lists in Python

Basic Examples of Lists in Python
example_list_1 = [1, 2, 3, 4, 5]
example_list_2 = [1, "Python", True, 3.14]
example_list_3 = [x * 2 for x in range(1, 6)]
Example: Accessing elements in a List

Accessing List Elements:

example_list_1 = [1, 2, 3, 4, 5]
print(example_list_1[0])  # Output: 1
print(example_list_1[-1]) # Output: 5 (last element)
Example: Modifying elements in a List

Modifying Lists:

example_list_1[1] = "two"
print(example_list_1)  # Output: [1, "two", 3, 4, 5]
Example: List Comprehension

What is List Comprehensions? Why use List comprehension?:List comprehensions offer several advantages:

List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition

Readability: They can make your code more concise and readable.

Efficiency: They are often faster than traditional loops for creating lists.

They allow for complex operations and transformations with minimal code.

Creating a list of square numbers:

squares = [x**2 for x in range(10)]
print(squares)  # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Filtering a list to exclude negative numbers:

this is the code: squares = [x**2 for x in range(10)]
print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Example : Basic List Operations
numbers = [1, 2, 3, 4 5]
print(numbers[2]) # Outputs: 3
numbers.append(6) # Adds 6 to the list
print(numbers) # Outputs: [1, 2, 3, 4, 5, 6]

Applying a function to all items in the list

fruits = ["apple", "banana", "cherry"]
upper_fruits = [fruit.upper() for fruit in fruits]
print(upper_fruits) # Output: ['APPLE', 'BANANA', 'CHERRY']

Try these Examples out-The more you Code the Better you Become


Adding Elements to a List:

fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
more_fruits = ["grape", "pineapple"]
fruits.extend(more_fruits)
fruits.insert(1, "orange")

append(): Adds an item to the end of the list

extend(): Adds all elements of a list to another list.

insert(): Inserts an item at a given position

Removing Elements from a List:

fruits.remove("banana")
last_fruit = fruits.pop()
fruits.clear()

remove(): Removes the first item from the list that has a specific value

pop(): Removes the item at the given position in the list, and if no index is specified, it removes and returns the last item.

clear(): Removes all items from the list

Other Operations in List:

index_of_apple = fruits.index("apple")
apple_count = fruits.count("apple")
fruits.sort()
fruits.reverse()
fruits_copy = fruits.copy()

index(): Returns the index of the first item with a specified value

count(): Returns the number of times a specified value occurs in a list

sort(): Sorts the items of the list in place (the list itself is changed)

reverse(): Reverses the items of the list in place

copy(): Returns a shallow copy of the list

List comprehension-Try these out

Squares of Numbers

squares = [x**2 for x in range(10)]

Getting Even numbers

even_numbers = [x for x in original_numbers if x % 2 == 0]

Uppercase conversion

upper_fruits = [fruit.upper() for fruit in fruits]

Flatten a Matrix

flattened = [num for row in matrix for num in row]