0% found this document useful (0 votes)
8 views37 pages

Day 2 Session 2

The document provides an overview of various data structures in Python, including lists, tuples, sets, strings, and dictionaries. It details the characteristics, properties, and functionalities of each data structure, emphasizing their mutability, indexing, and ability to hold multiple data types. Additionally, it explains operations and methods associated with these data structures.

Uploaded by

Manu My
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views37 pages

Day 2 Session 2

The document provides an overview of various data structures in Python, including lists, tuples, sets, strings, and dictionaries. It details the characteristics, properties, and functionalities of each data structure, emphasizing their mutability, indexing, and ability to hold multiple data types. Additionally, it explains operations and methods associated with these data structures.

Uploaded by

Manu My
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Day_2

Session 2 : Data Structures

 List
 Tuple
 Set
 String
 Dictionary
List
A list in Python is an ordered collection of items. Lists are mutable, meaning you can change their contents (add, remove, or modify items)
after creation. They can contain elements of different data types (e.g., integers, strings, floats, etc.).

• Basic Characteristics of a List:


• Ordered: The order in which elements are inserted is preserved.
• Mutable: Lists can be modified after creation.
• Indexed: Lists are indexed, meaning you can access elements by their index (starting from 0).
• Allows duplicates: A list can contain duplicate elements.
• Can store multiple data types: Lists can hold items of various data types.
Indexing
Indexing is the process of accessing an element in a sequence (such as a list, string, or tuple) by its position or
index.

General Concept:
Lists: Lists are ordered collections, so each element in a list has a specific index. The index starts at 0 for the
first element and increases by 1 for each subsequent element.
Strings: Strings are also sequences, and each character in a string can be accessed using an index.
Tuples: Like lists, tuples are ordered collections and are indexed similarly.
1. Ordered
Properties of a list

• A list maintains the order of the elements as they were inserted.


• The order is preserved and you can access the elements based on their position (index).

2. Mutable

•Lists are mutable, meaning their content can be changed after they are created.
•You can modify, add, or remove elements from a list.
3. Indexed

• Lists are indexed, meaning each element has a position starting from 0.
• You can access elements using their index.

4. Allows Duplicates

• Lists can store duplicate elements. Multiple occurrences of the same element are allowed.
5. Can Contain Multiple Data Types

• Lists can hold elements of different data types (integers,


strings, floats, etc.).

6. Dynamic in Size

• Lists in Python are dynamic. You can add or remove


elements, and the list will resize itself automatically.
• You don’t need to predefine the size of the list.

7. Can Contain Other Lists (Nested Lists)

• Lists can contain other lists as elements,


allowing you to create multidimensional or
nested lists.
8. Supports Slicing

• Lists support slicing, which allows you to get a sub list


(a portion) of the list.
• You can use slicing to access a part of the list based on start
and end indices.

9. Iterable

• Lists are iterable, which means you can loop through them using a
for loop or other iteration techniques.

10. Supports List Operations and Methods

Lists support a variety of operations, such as:


Concatenation (+): Combining two or more lists.
Repetition (*): Repeating the list elements.
Methods like append(), insert(), remove(), pop(), sort(), reverse(), etc.
Tuple
A tuple in Python is an ordered, immutable collection of elements. Tuples are similar to lists, but they have a few key
differences that make them suitable for certain use cases where data should not be modified.

Key Properties of Tuples:


1. Ordered: The elements in a tuple have a defined order, meaning the order of elements is preserved.
2. Immutable: Once a tuple is created, you cannot modify its contents (you can't add, remove, or change items).
3. Indexed: Just like lists, you can access elements in a tuple using their index.
4. Allows Duplicates: A tuple can contain duplicate elements.
5. Can Contain Multiple Data Types: Tuples can store elements of different data types (e.g., integers, strings, floats, etc.).
6. Can Store Multiple Elements: Tuples can hold a single element or many elements, including other tuples (nested tuples).
7. Iterable: Tuples are iterable, so they can be used in loops.
Syntax:
A tuple is defined by placing the elements inside parentheses () and separating them with commas.
3. Tuple with a Single Element:

For a tuple with one element, you need a trailing comma


to differentiate it from a regular parentheses grouping

Accessing Tuple Elements:

Tuples are indexed, and you can access individual


elements using their index, starting from 0.
Modifying Tuples:

Tuples are immutable, so you cannot change their content once they are created.
However, you can create a new tuple by combining or slicing existing tuples.
Set
A set in Python is an unordered, mutable, and iterable collection of unique elements. It is commonly used for operations
involving membership testing, duplicate elimination, and set operations like union, intersection, and difference.
2. Remove Elements:

• Use remove() to remove an element (raises an error if the


element doesn’t exist).
• Use discard() to remove an element without raising an error
if it doesn’t exist.
String
A string in Python is an immutable sequence of characters. Strings are used to represent text, such as words,
sentences, or even paragraphs. They are one of the most commonly used data types in Python.
Dictionary
A dictionary in Python is a collection of key-value pairs. Each key is unique, and it maps to a specific value.
Dictionaries are mutable, meaning their content can be modified after creation. They are widely used for storing data
in a structured format.
Syntax:

Dictionaries are defined using curly braces {} with key-value pairs separated by colons .
Examples of Dictionaries

You might also like