[go: up one dir, main page]

0% found this document useful (0 votes)
2 views3 pages

List_Operations_in_Python

This document provides an overview of common list operations in Python, including creating, accessing, slicing, adding, removing, and iterating through lists. It includes examples for each operation to illustrate their usage. Additionally, it covers list comprehension, sorting, reversing, and checking membership in lists.

Uploaded by

gamerpurrple.1
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)
2 views3 pages

List_Operations_in_Python

This document provides an overview of common list operations in Python, including creating, accessing, slicing, adding, removing, and iterating through lists. It includes examples for each operation to illustrate their usage. Additionally, it covers list comprehension, sorting, reversing, and checking membership in lists.

Uploaded by

gamerpurrple.1
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

List Operations in Python with Examples

Lists in Python are used to store multiple items in a single variable. They are one of the most

versatile data types in Python. This document covers common list operations with examples.

1. Creating a List

Example:

my_list = [1, 2, 3, 4, 5]

print(my_list)

2. Accessing Elements

Example:

my_list = [1, 2, 3]

print(my_list[0]) # Output: 1

3. Slicing a List

Example:

my_list = [1, 2, 3, 4, 5]

print(my_list[1:4]) # Output: [2, 3, 4]

4. Adding Elements

Example:

my_list = [1, 2, 3]

my_list.append(4)

print(my_list)

5. Removing Elements

Example:

my_list = [1, 2, 3, 4]
my_list.remove(3)

print(my_list)

6. List Comprehension

Example:

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

print(squares) # Output: [0, 1, 4, 9, 16]

7. Sorting a List

Example:

my_list = [3, 1, 2]

my_list.sort()

print(my_list)

8. Reversing a List

Example:

my_list = [1, 2, 3]

my_list.reverse()

print(my_list)

9. Checking Membership

Example:

my_list = [1, 2, 3]

print(2 in my_list) # Output: True

10. Iterating Through a List

Example:

my_list = [1, 2, 3]

for item in my_list:


print(item)

You might also like