Day 4: GitHub Copilot Training – Loops, Arrays, and Objects

๐ŸŸฆ 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:

  1. # Find all unique elements in a list
  2. // Count how many times each letter appears in a string
  3. # Remove duplicates from a list
  4. // Convert an array of objects to a single object grouped by category
  5. # 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


Previous Post Next Post