Docsity

Python List Operations: A Beginner's Guide, Slides of Computer science

Partial preview of the text

Download Python List Operations: A Beginner's Guide and more Slides Computer science in PDF only on Docsity!

Python Program: List

Operations

Create, Append and Remove Elements in Lists

Introduction to Lists

  • A list is a collection of items in Python.
  • Lists are ordered, mutable, and allow duplicate values.
  • (^) • Syntax: my_list = [1, 2, 3, 'apple', 'banana']

Appending to a List

We use append() method to add an element.

  • Example:
  • (^) my_list = [1, 2, 3]
  • (^) my_list.append(4)
  • print(my_list)
  • Output: [1, 2, 3, 4]

Removing from a List

We use remove() method to delete a specific element.

  • Example:
  • (^) my_list = [1, 2, 3, 4]
  • (^) my_list.remove(3)
  • print(my_list)
  • Output: [1, 2, 4]