Unit 3 - Python Programming
Unit 3 - Python Programming
Lists
Definition:
A list is a mutable, ordered collection of items in Python, enclosed in square brackets []. Lists
can hold elements of different data types (int, float, string, etc.).
Creating a List:
Accessing Elements:
Slicing a List:
Negative Indices:
Example:
List Comprehensions:
Definition:
Tuples are immutable, ordered collections of items, enclosed in parentheses ().
Creating a Tuple:
Access elements using positive or negative indexing. Slicing works similarly to lists.
Operations on Tuples:
Immutability:
● Once created, tuple elements cannot be changed. This provides data integrity.
Usage:
● Used when data should not change, e.g., storing days of the week.
Dictionaries
Definition:
Dictionaries are unordered collections of key-value pairs, enclosed in {}.
Creating a Dictionary:
Dictionary Operations:
Sets
Definition:
A set is an unordered collection of unique elements, defined using curly braces {} or the set()
function.
Creating a Set:
Operations on Sets:
Set Operations:
Use Case:
● Used when duplicate values are not allowed, such as collecting unique elements from a
list.