Functions and Collections in
Python
What are Functions in Python?
• Functions are blocks of code that perform a
specific task.
• They help to reduce repetition and improve
modularity.
• Defined using the 'def' keyword.
• Can take inputs (parameters) and return
outputs.
Function Syntax and Example
• Syntax: def function_name(parameters):
• Example:
def greet(name):
print('Hello', name)
greet(‘kevin')
Types of Functions
• Built-in Functions: print(), len(), type()
• User-defined Functions: Functions you define
• Functions with Arguments and Return Values
• Lambda (Anonymous) Functions
• Default and Keyword Arguments
What are Collections in Python?
• Collections are used to store multiple items in
a single variable.
• Python has four main collection types: List,
Tuple, Set, Dictionary.
• Each collection type has different features and
use-cases.
List
• Ordered, mutable, allows duplicates.
• Defined using square brackets [].
• Example: fruits = ['apple', 'banana']
• Supports methods like append(), remove(),
sort().
Tuple
• Ordered, immutable, allows duplicates.
• Defined using parentheses ().
• Example: colors = ('red', 'blue')
• Used when data should not change.
Set
• Unordered, mutable, does not allow
duplicates.
• Defined using curly braces {}.
• Example: nums = {1, 2, 2, 3} -> {1, 2, 3}
• Supports operations like union, intersection.
Dictionary
• Unordered collection of key-value pairs.
• Defined using curly braces {} with colons.
• Example: student = {'name': 'Tom', 'age': 20}
• Access values using keys, e.g., student['name']