๐ฆ Day 4: Copilot with Loops, Arrays, and Objects
๐ฏ Goal:
Use Copilot to help write code involving:
- Loops (
for,while, etc.) - Arrays/lists
- Objects/dictionaries
This helps in data processing, CRUD operations, and logic building.
๐ง Step 1: Loop Structures
Open a .py, .js, or .java file and try these prompts:
๐ Python:
# Print numbers from 1 to 10
Copilot suggests:
for i in range(1, 11):
print(i)
๐ป JavaScript:
// Print even numbers from 1 to 20
Copilot suggests:
for (let i = 1; i <= 20; i++) {
if (i % 2 === 0) console.log(i);
}
๐ง Step 2: Array/List Operations
๐ Python:
# Filter even numbers from a list
# Find the sum of all numbers in a list
๐ป JavaScript:
// Find the largest number in an array
// Reverse an array
// Count occurrences of each element in an array
Copilot will usually suggest map, filter, reduce, or loops automatically.
๐ง Step 3: Objects / Dictionaries
๐ Python:
# Count word frequency in a sentence using dictionary
Copilot likely gives:
def word_frequency(sentence):
words = sentence.split()
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
return freq
๐ป JavaScript:
// Create a student object with name, age, and marks
// Update the marks property
let student = {
name: "Ravi",
age: 18,
marks: 85
};
student.marks = 90;
๐งช Practice Challenges
Try one of these prompts and let Copilot complete the logic:
# Find all unique elements in a list// Count how many times each letter appears in a string# Remove duplicates from a list// Convert an array of objects to a single object grouped by category# Sort a list of dictionaries by key 'age'
๐ Bonus: Combine Concepts
Prompt Copilot like this:
# Given a list of student dictionaries, return the names of those who scored above 80
Copilot might give you:
def top_students(students):
return [s['name'] for s in students if s['marks'] > 80]
✅ Summary of Day 4
| Focus Area | Example Prompt |
|---|---|
| Loops | # Print numbers from 1 to 10 |
| Arrays/Lists | # Find sum of elements in a list |
| Objects/Dictionaries | # Group names by department in a dictionary |
Tags:
GitHub Copilot