Ad Code

Responsive Advertisement

List in Python Class 11 Notes | Python List Manipulation

  

What is List in Python?

  • List is built in sequence data type in python.
  • Stores multiple values
  • List item can be of different data types
  • All the items are comma separated and enclosed in square bracket.
  • Individual item in a list can be accessed using index which begins from 0.

Examples

Accessing elements in a List

  • Individual item in a list can be accessed using indexes.
  • Indexes are unique numbers assigned to each item in a list to identify them
  • Index always begins from 0 and written in square brackets “[]”.

Example:

List is Mutable

  • Yes list is mutable.
  • Content of the list can be changed after it has been created.

List operations

List supports following operations:

  • Concatenation
  • Repetition
  • Membership
  • Slicing

Concatenation

  • Concatenation refers to joining two List.
  • Plus (‘+’) is used as concatenation operator.
  • Concatenating List with other data type produces TypeErrors.

Example:

Repetition

  • Repetition as it name implies used to replicate a list at specified no of times.
  • Asterisk (‘*’) is used as repetition operator.

Membership

  • Membership operation refers to checking an item is exists in the list or not.
  • Python uses ‘in’ and ‘not in’ as membership operator.
  • ‘in’ returns true if the item specified present in the list.
  • ‘not in’ returns true if the item specified present in the list.

Example:

Slicing

  • Extracting a subset of items from given list is called slicing
  • Subset occurred after slicing contains contiguous items.
  • Slicing is done using index range like List[start_index : end_index : step_value]
  • End index is always excluded in resultant subset of list.
  • Negative index can also be used for slicing.

Examples:

Traversing a List

  • Traversing a List refers to accessing each item a given list sequentially.
  • for or while loop can be used for traversing a list.

Examples:

List methods | List Functions



Nested List

One list appears as an element inside another list.

Example:

Post a Comment

0 Comments