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

Python List Exp

The document provides a comprehensive guide on list operations in Python, including creating, accessing, modifying, adding, and removing elements. It also covers advanced topics such as slicing, iterating, list comprehension, sorting, and common list methods. Each section includes code examples demonstrating the respective functionalities.

Uploaded by

aceinfotechthane
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)
7 views

Python List Exp

The document provides a comprehensive guide on list operations in Python, including creating, accessing, modifying, adding, and removing elements. It also covers advanced topics such as slicing, iterating, list comprehension, sorting, and common list methods. Each section includes code examples demonstrating the respective functionalities.

Uploaded by

aceinfotechthane
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
You are on page 1/ 3

1.

Creating a List
# Simple list
fruits = ["apple", "banana", "cherry"]
print(fruits)

# Mixed data types


mixed_list = [1, "hello", 3.14, True]
print(mixed_list)

2. Accessing Elements
fruits = ["apple", "banana", "cherry"]

# Accessing by index
print(fruits[0]) # First element: "apple"
print(fruits[-1]) # Last element: "cherry"

3. Modifying a List
fruits = ["apple", "banana", "cherry"]
fruits[1] = "blueberry" # Replace "banana" with "blueberry"
print(fruits) # ["apple", "blueberry", "cherry"]

4. Adding Elements
# Using append()
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits) # ["apple", "banana", "cherry"]

# Using insert()
fruits.insert(1, "blueberry")
print(fruits) # ["apple", "blueberry", "banana", "cherry"]

5. Removing Elements
fruits = ["apple", "banana", "cherry"]

# Using remove()
fruits.remove("banana")
print(fruits) # ["apple", "cherry"]

# Using pop()
last_item = fruits.pop()
print(last_item) # "cherry"
print(fruits) # ["apple"]

# Using del
del fruits[0]
print(fruits) # []
6. Slicing a List
fruits = ["apple", "banana", "cherry", "date", "elderberry"]

# Get a sublist
print(fruits[1:4]) # ["banana", "cherry", "date"]

# Reverse a list
print(fruits[::-1]) # ["elderberry", "date", "cherry", "banana", "apple"]

7. Iterating Through a List


fruits = ["apple", "banana", "cherry"]

for fruit in fruits:


print(fruit)

8. List Comprehension
# Create a list of squares
squares = [x**2 for x in range(1, 6)]
print(squares) # [1, 4, 9, 16, 25]

# Filter even numbers


even_numbers = [x for x in range(10) if x % 2 == 0]
print(even_numbers) # [0, 2, 4, 6, 8]

9. Sorting a List
numbers = [5, 2, 9, 1]

# Ascending
numbers.sort()
print(numbers) # [1, 2, 5, 9]

# Descending
numbers.sort(reverse=True)
print(numbers) # [9, 5, 2, 1]

# Using sorted()
print(sorted(numbers)) # [1, 2, 5, 9]

10. Other Common Methods


fruits = ["apple", "banana", "cherry"]

# Length of the list


print(len(fruits)) # 3

# Check if an item exists


print("banana" in fruits) # True
# Count occurrences
numbers = [1, 2, 2, 3]
print(numbers.count(2)) # 2

# Index of an item
print(fruits.index("cherry")) # 2

# Clear the list


fruits.clear()
print(fruits) # []

You might also like