Day 2 Python Data Structure and Functions
Day 2 Python Data Structure and Functions
Lists, Tuples, Dictionaries, Sets: Python provides several built-in data structures for
organizing and storing data:
Lists: Ordered collection of items. Mutable, meaning they can be modified after
creation.
Tuples: Similar to lists but immutable, meaning they cannot be modified after
creation.
Dictionaries: Collection of key-value pairs. Keys are unique and immutable, values
can be of any data type.
Sets: Unordered collection of unique items. Useful for mathematical operations like
union, intersection, etc.
List Comprehensions: List comprehensions provide a concise way to create lists. They
consist of an expression followed by a for clause, then zero or more for or if clauses.
Functions: Functions are blocks of reusable code that perform a specific task. They improve
code modularity and reusability.
Lambda Functions: Lambda functions, also known as anonymous functions, are small,
single-expression functions without a name.
Basic Input/Output Operations: Python provides built-in functions for taking input from
the user and displaying output.
Output:
Common methods:
o append(): Adds an element at the end
o remove(): Removes the first occurrence of an element
o pop(): Removes an element at a given index
Tuples:
Dictionaries:
Common methods:
o keys(): Returns a view object of all keys
o values(): Returns a view object of all values
o items(): Returns a view object of all key-value pairs
Sets:
List Comprehensions
Concise way to create lists
Syntax: [expression for item in iterable if condition]
Example:
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
Lambda Functions:
add = lambda x, y: x + y
print(add(2, 3))
This concludes the note for Day 2: Python Data Structures and Functions.