Python for AI/ML - Day 3
Welcome to the 3rd day of Python session. The first two sessions can be reviewed in the link outlined below.
In this session we talk and discuss about dictionary and functions in general with some advanced examples.
Part 1: Python Dictionaries
A dictionary in Python is a collection of key-value pairs, where each key is unique and is used to access its corresponding value. Dictionaries are mutable and highly optimized for fast lookups due to their hash table implementation.
Key Features of Python Dictionaries:
Keys must be immutable (e.g., strings, numbers, or tuples containing immutable items).
Values can be of any data type (mutable or immutable).
Operations like lookups, insertions, and deletions are efficient.
Creating a Dictionary
Explanation:
creates a dictionary with three key-value pairs.
The constructor is an alternative way to create dictionaries.
creates an empty dictionary.
Accessing Values: [] vs .get()
Explanation:
raises a if the key doesn’t exist.
returns (or a specified default value) if the key doesn’t exist.
Adding or Updating Items
Explanation:
Adding a new key-value pair or updating an existing key’s value is done using the assignment operator .
Deleting Items: del, .pop(), .popitem()
Explanation:
removes a key-value pair.
removes and returns the value for the specified key.
removes and returns the last key-value pair.
Dictionary Methods
Here are some additional methods with examples:
Keys, Values, and Items
Merging Dictionaries:
Copying a Dictionary:
Clearing a Dictionary:
Setting Default Values:
Dictionary Comprehension
Explanation:
Creates a dictionary where keys are numbers, and values are their squares.
Part 2: Python Functions
A function is a block of reusable code that performs a specific task. Functions improve code modularity and reusability.
Defining and Calling Functions
Explanation:
defines a function. The function is called using its name followed by parentheses.
Functions with Parameters
Explanation:
takes two arguments, and , and returns their sum.
Functions with Default Parameters
Default parameters allow you to specify default values for one or more parameters in a function. These values are used if no corresponding argument is provided during the function call.
Rules for Default Parameters:
Default parameters must come after all required parameters in the function definition.
If a default parameter is specified, it can be overridden by passing a value during the function call.
Examples:
Explanation:
In , the parameter has a default value of "Stranger".
In , defaults to unless overridden.
In , the and parameters both have default values, allowing for flexible function calls.
Lambda Functions
Importance of Lambda Functions
Lambda functions are particularly useful for creating short, single-use functions without the need to define a full function using . They are often used in functional programming and data processing pipelines.
Custom Sorting:
Higher-Order Functions
A higher-order function is a function that accepts another function as an argument or returns a function as a result. This makes them useful for abstracting logic or reusing code efficiently.
Explanation:
takes a function and a value . It applies to and returns the result.
In this example, doubles the value of .
Recursive Functions
Explanation:
A function that calls itself is called recursive. This example calculates the factorial of a number.
For , the function computes and so on, until it reaches .
This summary encapsulates the fundamental concepts of Python dictionaries, functions, lambda expressions, and higher-order functions. Understanding these elements is crucial for effective programming in Python, enabling developers to write cleaner, more efficient, and more functional code.
NOTE: These articles are notes from the sessions. More in-depth technical explanations will be incrementally added.
Bench Sales Recruiter
5morakesh@srimatech.com