Python: Lists, Tuples, Dictionaries,
Sets & Functions
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
• Cannot modify elements
Tuples are immutable.
Tuples - Built-in Functions
• len()
• max()
• min()
• count(): Returns the number of occurrences of
a value.
• index(): Returns the index of the first
occurrence of a value.
Dictionaries - Introduction
• Key-value pairs
• Defined using curly braces:
my_dict = {'a': 1, 'b': 2}
Dictionaries - Basic Operations
• Adding/updating elements: dict[key] = value
my_dict['c'] = 3 # {'a': 1, 'b': 2, 'c': 3}
• Removing elements: pop(), del
• pop(): Removes and returns an element.
my_dict.pop('a') # {'b': 2, 'c': 3}
• del: Deletes an element.
del my_dict['b'] # {'c': 3}
Dictionaries - Built-in Functions
• keys(): Returns a view object of keys.
• values(): Returns a view object of values.
• items(): Returns a view object of key-value
pairs.
• get(): Returns the value for a key.
• pop()
• update(): Updates the dictionary with
elements from another dictionary.
Sets - Introduction
• Unordered, unique elements
• Defined using curly braces: my_set = {1, 2, 3}
Sets - Basic Operations
• Adding elements: add(), update()
• add(): Adds a single element.
my_set.add(4) # {1, 2, 3, 4}
• update(): Adds multiple elements.
my_set.update([5, 6]) # {1, 2, 3, 4, 5, 6}
• Removing elements: remove(), discard(), pop()
• remove(): Removes a specific
element.my_set.remove(1) # {2, 3, 4, 5, 6}
• discard(): Removes a specific element without
raising an error if the element is not found.
• pop(): Removes and returns an arbitrary
element.
Sets - Built-in Functions
• union(): Returns a set containing all elements
from both sets.
• intersection(): Returns a set containing
common elements.
• difference(): Returns a set containing elements
in the first set but not in the second.
• issubset(): Checks if one set is a subset of
another.
The zip() Function
• Combines multiple iterables into tuples
Example:list(zip([1, 2, 3], ['a', 'b', 'c']))
# [(1, 'a'), (2, 'b'), (3, 'c')]
Functions - Introduction
• Block of reusable code
• Defined using def keyword
def my_function():
print("Hello")
my_function()
# Output: Hello
Function Definition and Calling
• Syntax:
• def my_function():
• print("Hello")
• my_function()
The return Statement & Void Functions
• return sends a value back to the caller
def add(a, b):
return a + b
result = add(2, 3)
# result is 5
• Void functions do not return a value
def greet():
print("Hello")
greet()
# Output: Hello
Parameters & Arguments
• Positional Arguments: Order matters
Example:
def subtract(a, b):
return a - b
subtract(5, 2)
# Output: 3
• Keyword Arguments: Named parameters
Example:
def greet(name, message):
print(f"{message}, {name}")
greet(name="Alice", message="Hello")
# Output: Hello, Alice
• Default Arguments: Predefined values.
Example:
def greet(name, message="Hello"):
print(f"{message}, {name}")
greet("Alice")
# Output: Hello, Alice
Scope and Lifetime of Variables
• Local variables: Exist inside a function
def my_function():
local_var = 10
print(local_var)
my_function()
# Output: 10
• Global variables: Accessible everywhere
global_var = 20
def my_function():
print(global_var)
my_function()
# Output: 20
Returning Multiple Values
• Example: return a, b, c returns a tuple
def get_coordinates():
return 10, 20
x, y = get_coordinates()
# x is 10, y is 20
Recursive Functions
• A function that calls itself
• Example: Factorial function using recursion
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
factorial(5)
# Output: 120
Lambda Functions
• Anonymous, one-line functions
• Example: square = lambda x: x * x
square(5) # Output: 25
Conclusion
• Lists, Tuples, Dictionaries, Sets, and Functions
in Python.
• Importance of these concepts in writing
efficient and clean code.
Thank You!
• Questions?