Introduction to Arrays and Lists in Programming, Study notes of Computer Science

This document introduces the concepts of arrays and lists in programming. Arrays and lists are data structures used to store collections of data. They allow you to group multiple values of the same or different data types into a single container. Lists are a common data structure in many programming languages, including Python. the key characteristics of lists, such as being homogeneous or heterogeneous, ordered, mutable, and dynamic in size. It also provides examples of creating and accessing elements in a list, as well as common list operations like adding and removing elements.

Typology: Study notes

2022/2023

Available from 10/15/2023

usman-ahmed-14
usman-ahmed-14 🇵🇰

11 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Arrays and Lists
Introduction
- Arrays and lists are data structures used to store collections of data in programming.
- They allow you to group multiple values of the same or different data types into a single
container.
- Lists are a common data structure in many programming languages, including Python.
Key Characteristics
1. Homogeneous or Heterogeneous
- Homogeneous Lists : All elements in the list are of the same data type.
- Heterogeneous Lists : Elements in the list can be of different data types.
2. Ordered
- Lists maintain the order of elements, which means you can access elements by their
position (index) in the list.
3. Mutable
- Lists are typically mutable, meaning you can change their contents (add, remove, or modify
elements) after they are created.
4. Dynamic Size
- Lists can grow or shrink in size as elements are added or removed.
pf3
pf4

Partial preview of the text

Download Introduction to Arrays and Lists in Programming and more Study notes Computer Science in PDF only on Docsity!

Arrays and Lists

Introduction

  • Arrays and lists are data structures used to store collections of data in programming.
  • They allow you to group multiple values of the same or different data types into a single container.
  • Lists are a common data structure in many programming languages, including Python. Key Characteristics 1. Homogeneous or Heterogeneous
  • Homogeneous Lists : All elements in the list are of the same data type.
  • Heterogeneous Lists : Elements in the list can be of different data types.
    1. Ordered
  • Lists maintain the order of elements, which means you can access elements by their position (index) in the list.
    1. Mutable
  • Lists are typically mutable, meaning you can change their contents (add, remove, or modify elements) after they are created.
    1. Dynamic Size
  • Lists can grow or shrink in size as elements are added or removed.

Creating Lists In Python, lists are created by enclosing elements in square brackets []. Example:

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

Accessing Elements

  • Elements in a list are accessed by their index.
  • Indexing starts at 0 for the first element. Example:
first_fruit = fruits[0] Accesses the first element ("apple") 
Common List Operations Adding Elements 
  • Append : Adds an element to the end of the list.
  • Insert : Adds an element at a specific position in the list. Removing Elements

fruits.remove("banana") Removes the first "banana" found. fruits.sort() Sorts the list in alphabetical order.

List Slicing - You can extract a portion (slice) of a list using slicing notation. Example: ```python subset = fruits[1:3] Extracts elements at index 1 and 2 ("banana" and "cherry") 
Nested Lists 
  • Lists can contain other lists as elements, creating nested lists. Example:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 
Summary: 
  • Lists are data structures used to store collections of data.
  • They are ordered, mutable, and can hold elements of different data types.
  • Lists offer a wide range of operations for adding, removing, and modifying elements. ---- Understanding lists is essential for working with structured data in programming.