Python Functions Presentation (1)
Python Functions Presentation (1)
Submitted by-
Shreyansh Mishra
Introduction
• Overview of data structures and functions in
Python
• Importance and usage in programming
Lists - Introduction
• Ordered, mutable collection of elements
• Defined using square brackets: my_list = [1, 2,
3]
• Common use cases: storing data, iterating
over elements
Lists - Basic Operations
• Adding elements: append(), extend(), insert()
my_list.append(4) # [1, 2, 3, 4]
my_list.extend([5, 6]) # [1, 2, 3, 4, 5, 6]
my_list.insert(1, 'banana') # [1, 'banana', 2, 3, 4, 5, 6]
• Removing elements: remove(), pop(), del
my_list.remove('banana') # [1, 2, 3, 4, 5, 6]
my_list.pop(2) # [1, 2, 4, 5, 6]
del my_list[0] # [2, 4, 5, 6]
• Updating elements: my_list[0] = 'new_value‘
• Checking existence: 'value' in my_list
my_list[0] = 'new_value'
Checking existence:
'value' in my_list
Lists - Indexing and Slicing
• Access elements using indices: list[0]
• Slicing: list[start:stop:step]
my_list[1:4] # Extracts elements from index 1 to 3
• Negative indexing: list[-1] for last element
• Example: my_list[1:4] extracts elements from
index 1 to 3
Lists - Built-in Functions
• len(): Returns the number of elements.
• max(): Returns the largest element.
• min(): Returns the smallest element.
• sum(): Returns the sum of elements.
• sorted(): Returns a sorted list
Tuples - Introduction
• Ordered, immutable collection of elements
• Defined using parentheses:
• Example: my_tuple = (1, 2, 3, 'apple')
Tuples - Operations & Indexing
• Accessing elements like lists
my_tuple[0] # First element