[go: up one dir, main page]

0% found this document useful (0 votes)
5 views14 pages

Data Containers

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

Data Containers

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

Python Programming

Session 1: (Data Structures and Control Flow)


Agenda:

1 Data Containers

2 Control Flow
Data Containers
Data Containers
Python provides several built-in data structures (or containers) to organize and manage data efficiently.
These include lists, tuples, sets, and dictionaries.
Lists
A list is an ordered, mutable collection of items. Lists can contain
items of different types, including other lists.
Syntax
Lists are defined using square brackets [ ]

Key Characteristics of list

• Ordered: Lists allow access to items using an index.


• Mutable: You can add, delete, or modify items in a list.
• Non-Unique Items: Lists can contain duplicate elements.
• Mixed Data Types: Lists can hold items of different data types.
Data Containers
Lists Operations
Indexing
Access elements using their index (starting at 0)

Slicing
Access a range of elements
Data Containers
Lists Operations
Adding Elements
Use append() to add an item, insert() to add at a specific index.

Removing Elements
Use remove() to delete an item, pop() to remove by index.
Tuples
A tuple is an ordered, immutable collection of items. Once
defined, its contents cannot be changed.
Syntax
Tuples are defined using parentheses ( )

Operations
Indexing and Slicing: Similar to lists.

Immutability
Cannot change elements, which makes tuples faster for
read-only data.
Tuples
Key Characteristics of Tuples
• Ordered: Tuples allow access to items using an index.
• Immutable: You cannot add, delete, or modify items in a tuple.
• Non-Unique Items: Tuples can contain duplicate elements.
• Mixed Data Types: Tuples can hold items of different data types.
Sets
A set is an unordered collection of unique items. Sets are used for
membership testing and eliminating duplicate entries.
Syntax
Sets are defined using curly braces {} or the set() function

Operations
Adding Elements: Use add()
Sets
Key Characteristics of Sets
• Unordered: Sets do not maintain a specific order of items.
• No Indexing/Slicing: Sets do not support indexing or slicing operations.
• Immutable Data Types: Sets can only contain immutable data types
(e.g., numbers, strings, tuples).
• Unique Items: Sets automatically remove duplicate items, ensuring all
elements are unique.
Sets
Operations
Removing Elements: Use remove() or discard()

Set Operations: Union, intersection, difference.


Dictionaries
A dictionary is an unordered collection of key-value pairs. Keys must be
unique and immutable (e.g., strings, numbers).
Syntax
Defined using curly braces { } with key-value pairs separated by colons.

Operations
Accessing Values: Use keys.

Adding/Updating: Assign a value to a key.


Dictionaries
Operations
Removing Items: Use pop() to remove a key-value pair.

Key Characteristics of Dictionaries

• Key-Value Pairs: Dictionaries store data as key-value pairs.


• Immutable Keys: Dictionary keys must be immutable (e.g., numbers, strings, tuples).
• Flexible Values: Dictionary values can be of any data type.
• Unique Keys: Each key in a dictionary must be unique.
• Unordered: Dictionaries are not ordered; elements are accessed using keys.
Comparing Lists, Tuples, Sets, Dictionaries

List Tuple Set Dictionary

[] or list() () or tuple() {} or set() {} or dict()

Ordered Ordered Unordered Unordered

Changeable Unchangeable Unchangeable Changeable

Indexed Indexed Unindexed Key Value pair

Allows Duplicates Allows Duplicates No Duplicates No Duplicates

Allows Slicing Allows Slicing No Slicing No Slicing

You might also like