Unit 3: Dictionaries & Functions
Dictionaries in Python
A dictionary is a collection of key–value pairs, where each key is used to access its value.
Key Characteristics
- Unordered collection (in concept)
- Mutable (can be changed)
- Keys must be unique
- Values can be duplicated
- Written using curly braces
{}
Example
Dictionary Structure (Diagram)
Accessing Values in Dictionary
Using Key Name
⚠ If key does not exist, it gives error.
Using get() Method (Safe Way)
If key not found, it returns None instead of error.
Working with Dictionaries
Adding New Elements
Updating Existing Value
Deleting Elements
Looping Through Dictionary
Accessing Keys, Values, Items
Properties of Dictionary
| Property | Description |
|---|---|
| Mutable | Values can be changed |
| Unique Keys | No duplicate keys |
| Indexed by Key | Not by position |
| Dynamic | Can grow or shrink |
| Nested | Can contain dictionary inside dictionary |
Nested Dictionary Example
Dictionary Functions
| Function | Description | Example |
|---|---|---|
| len() | Number of key-value pairs | len(student) |
| type() | Type of variable | type(student) |
| str() | Convert to string | str(student) |
Important Dictionary Methods
Commonly Used Methods
| Method | Purpose |
|---|---|
| get() | Get value |
| keys() | All keys |
| values() | All values |
| items() | Key-value pairs |
| update() | Update dictionary |
| pop() | Remove key |
| clear() | Remove all |
| copy() | Copy dictionary |
Example: student.update({"city": "Lucknow"})
Dictionary vs List (Exam Favorite)
| Feature | Dictionary | List |
|---|---|---|
| Data Type | Key-Value | Index-Value |
| Access | By key | By index |
| Duplicate Keys | Not allowed | Allowed |
| Order | No index | Indexed |
Important Exam Points
- Dictionary stores data in key-value form
- Keys must be immutable (string, number, tuple)
- Accessing non-existing key using
[]gives error get()method is safer than[]- Dictionary is mutable
Frequently Asked Exam Questions
- Define dictionary with example
- Difference between list and dictionary
- Explain dictionary methods
- What is nested dictionary?
Functions in Python
A function is a block of reusable code that performs a specific task.
Instead of writing the same code again and again, we define it once and call it whenever needed.
Advantages of Functions
- Code reusability
- Better readability
- Easy debugging
- Modular programming
Defining & Calling a Function
Syntax to Define a Function
Example
Calling a Function
Passing Arguments to Functions
Arguments are values passed to a function during function call.
Example
Mutable & Immutable Data Types
Meaning
- Mutable: Can be changed
- Immutable: Cannot be changed
Common Examples
| Mutable | Immutable |
|---|---|
| list | int |
| dictionary | float |
| set | string |
| bytearray | tuple |
Effect on Function (Very Important for Exam)
Immutable Example
Mutable Example
Conclusion: Mutable objects are modified inside function, immutable are not.
Different Types of Arguments
1. Positional Arguments
Arguments passed in order.
2. Keyword Arguments
Arguments passed using parameter name.
3. Default Arguments
Default value assigned to parameter.
4. Variable Length Arguments
*a) args (Non-keyword)
**b) kwargs (Keyword arguments)
Recursion
A function calling itself to solve a problem.
Two Parts of Recursion
- Base condition
- Recursive call
Example: Factorial
Recursion Flow Diagram (Text)
Scope of Variables
Scope defines where a variable can be accessed.
Types of Scope
| Scope | Meaning |
|---|---|
| Local | Inside function |
| Global | Outside function |
| Built-in | Python predefined |
Local Variable Example
Global Variable Example
Using global Keyword
Exam-Oriented Important Points
- Functions are defined using
defkeyword - Mutable arguments can be modified inside function
- Immutable arguments remain unchanged
*argsand**kwargsallow flexible arguments- Recursion must have base condition
- Local variables have limited scope
Frequently Asked Exam Questions
- Define function with example
- Explain types of arguments
- Difference between mutable and immutable
- Explain recursion with example
- Scope of variables in Python
Tags:
Python Programming