Lists in Python | Basics of Python - Software Development PDF Download

Introduction

Lists are one of the most commonly used data structures in Python. They allow you to store and organize multiple values in a single variable. Lists are versatile and provide several built-in methods to manipulate and access their elements. This article will introduce you to the concept of lists in Python and provide simple code examples to help you understand their usage.

Creating Lists

You can create a list in Python by enclosing comma-separated values within square brackets '[ ]'. Here's an example:

fruits = ['apple', 'banana', 'orange', 'grape']

In the example above, we created a list named 'fruits' that contains four string values. Lists can hold values of any data type, including numbers, strings, and even other lists.

Accessing List Elements

You can access individual elements in a list using their index. In Python, list indices start from '0' for the first element and increment by '1' for each subsequent element. Here's how you can access elements in a list:

fruits = ['apple', 'banana', 'orange', 'grape']

print(fruits[0])  # Output: apple

print(fruits[2])  # Output: orange

In the example above, 'fruits[0]' returns the first element of the list ('apple'), and 'fruits[2]' returns the third element ('orange').

Modifying List Elements

Lists are mutable, which means you can modify their elements by assigning new values to specific indices. Here's an example:

fruits = ['apple', 'banana', 'orange', 'grape']

fruits[1] = 'pear'

print(fruits)  # Output: ['apple', 'pear', 'orange', 'grape']

In the example above, we modified the second element of the 'fruits' list from 'banana' to 'pear'. The 'print()' function displays the updated list.

List Methods

  • Python provides several built-in methods to perform various operations on lists. Here are some commonly used list methods:
  • 'append()': Adds an element to the end of the list.
  • 'insert()': Inserts an element at a specified index.
  • 'remove()': Removes the first occurrence of a specified element.
  • 'pop()': Removes and returns the element at a specified index.
  • 'len()': Returns the number of elements in a list.

Let's see these methods in action:

fruits = ['apple', 'banana', 'orange']

fruits.append('grape')

print(fruits)  # Output: ['apple', 'banana', 'orange', 'grape']

fruits.insert(1, 'pear')

print(fruits)  # Output: ['apple', 'pear', 'banana', 'orange', 'grape']

fruits.remove('banana')

print(fruits)  # Output: ['apple', 'pear', 'orange', 'grape']

removed_fruit = fruits.pop(2)

print(removed_fruit)  # Output: orange

print(fruits)  # Output: ['apple', 'pear', 'grape']

print(len(fruits))  # Output: 3

Sample Problems

Problem 1: Create a list called 'numbers' that contains the integers from 1 to 5. Print the second element of the list.

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

print(numbers[1])  # Output: 2

Problem 2: Add the number 6 to the end of the 'numbers' list. Print the updated list.

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

numbers.append(6)

print(numbers)  # Output: [1, 2, 3, 4, 5, 6]

Problem 3: Remove the number 3 from the 'numbers' list. Print the updated list.

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

numbers.remove(3)

print(numbers)  # Output: [1, 2, 4, 5]

Problem 4: Create a list called 'fruits' that contains the strings 'apple', 'banana', and 'orange'. Replace the second element with 'grape'. Print the updated list.

fruits = ['apple', 'banana', 'orange']

fruits[1] = 'grape'

print(fruits)  # Output: ['apple', 'grape', 'orange']

Conclusion

Lists are a fundamental data structure in Python that allow you to store and manipulate collections of values. Understanding how to create, access, and modify list elements, as well as use built-in methods, is essential for writing efficient and organized code. With the knowledge gained from this article, you can now confidently work with lists in Python.

The document Lists in Python | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development
49 videos|38 docs|18 tests

Top Courses for Software Development

49 videos|38 docs|18 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Free

,

Lists in Python | Basics of Python - Software Development

,

Important questions

,

Semester Notes

,

mock tests for examination

,

Sample Paper

,

study material

,

past year papers

,

Exam

,

Extra Questions

,

practice quizzes

,

Lists in Python | Basics of Python - Software Development

,

Objective type Questions

,

video lectures

,

ppt

,

Viva Questions

,

pdf

,

MCQs

,

Previous Year Questions with Solutions

,

shortcuts and tricks

,

Summary

,

Lists in Python | Basics of Python - Software Development

;