AI/ML Internship – Day 12 Notes & Tasks
Module 3: Python for AI
Part 1: List Comprehension (Very Important)
What is List Comprehension?
A short and efficient way to create lists.
Basic Syntax
[expression for item in iterable]
Example
nums = [1, 2, 3, 4]
squares = [x*x for x in nums]
print(squares)
With Condition
nums = [1, 2, 3, 4, 5]
even = [x for x in nums if x % 2 == 0]
print(even)
Real Use
names = ["ai", "ml", "python"]
upper = [[Link]() for n in names]
print(upper)
Part 2: Dictionary Comprehension
What is it?
Create dictionary in one line.
Example
nums = [1, 2, 3]
squares = {x: x*x for x in nums}
print(squares)
With Condition
nums = [1, 2, 3, 4]
even_dict = {x: x*x for x in nums if x % 2 == 0}
print(even_dict)
Part 3: Sets (Advanced Usage)
What is Set?
• Unordered collection
• No duplicates
Example
nums = {1, 2, 2, 3}
print(nums)
Operations
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # Union
print(a & b) # Intersection
print(a - b) # Difference
Part 4: Iterators & Generators
Iterator
Object that can be iterated.
Generator (Important)
Uses yield instead of return
def count(n):
for i in range(n):
yield i
for num in count(5):
print(num)
Why Generators?
• Memory efficient
• Used in large data processing
Part 5: Zip Function
Combine Multiple Lists
names = ["A", "B"]
marks = [80, 90]
combined = list(zip(names, marks))
print(combined)
Part 6: Enumerate Function
names = ["AI", "ML"]
for i, name in enumerate(names):
print(i, name)
Part 7: Real-World Thinking
These concepts are used in:
• Data transformation
• Feature engineering
• Data pipelines
Especially:
• List comprehension → fast processing
• Generators → big data handling
Tasks for Day 12
Task 1: Theory
1. What is list comprehension?
2. Difference between list and set
3. What is generator?
4. What is zip function?
5. What is enumerate?
Task 2: List Comprehension
1. Square numbers
2. Find even numbers
3. Convert strings to uppercase
4. Filter numbers > 50
Task 3: Dictionary Comprehension
1. Create number-square dictionary
2. Filter even numbers
3. Map names to lengths
Task 4: Set Practice
1. Remove duplicates
2. Find common elements
3. Find union and difference
Task 5: Generator Practice
1. Create generator for numbers
2. Generate even numbers
3. Fibonacci using generator
Task 6: Zip & Enumerate
1. Combine 2 lists
2. Print index + value
3. Convert zip result to dictionary