2 Learn Python to A.I Programming – Lesson

Lesson 2 – Loops and Arrays (With A.I YOLO Example)

The goal of this lesson is to run real AI code using YOLOv8 and see how an AI model detects objects in an image.
In this example, the YOLO model analyzes a picture stored on your computer in the folder C:\AI\PICS, detects objects such as people, cows, and balls, and prints how many of each it finds.
You can save your test images into C:\AI\PICS and replace the file name in the code (for example, pic1.png) to analyze different images with artificial intelligence.


1. COCO Dataset Classes (default 80 classes used by YOLOv5/YOLOv8/YOLOv7 etc.)

Here is the full list of 80 YOLO COCO classes in order (index 0→79):

0 person
1 bicycle
2 car
3 motorcycle
4 airplane
5 bus
6 train
7 truck
8 boat
9 traffic light
10 fire hydrant
11 stop sign
12 parking meter
13 bench
14 bird
15 cat
16 dog
17 horse
18 sheep
19 cow
20 elephant
21 bear
22 zebra
23 giraffe
24 backpack
25 umbrella
26 handbag
27 tie
28 suitcase
29 frisbee
30 skis
31 snowboard
32 sports ball
33 kite
34 baseball bat
35 baseball glove
36 skateboard
37 surfboard
38 tennis racket
39 bottle
40 wine glass
41 cup
42 fork
43 knife
44 spoon
45 bowl
46 banana
47 apple
48 sandwich
49 orange
50 broccoli
51 carrot
52 hot dog
53 pizza
54 donut
55 cake
56 chair
57 couch
58 potted plant
59 bed
60 dining table
61 toilet
62 tv
63 laptop
64 mouse
65 remote
66 keyboard
67 cell phone
68 microwave
69 oven
70 toaster
71 sink
72 refrigerator
73 book
74 clock
75 vase
76 scissors
77 teddy bear
78 hair drier
79 toothbrush


0 person
1 bicycle
2 car
3 motorcycle
4 airplane
5 bus
6 train
7 truck

79 toothbrush


formatted output (aligned columns):

0 person
1 bicycle
2 car
3 motorcycle

79 toothbrush


 

 remark Jupiter notebook – run this code to know where you locate 

we will use this code and learn how it works 

Lesson 2 to teach loops and arrays using a real YOLO-style example

Before we use YOLO-style data, let’s understand dictionaries and arrays with simple

Lesson – Dictionaries and Loops in Python

A dictionary in Python stores data as key → value pairs.

Output:
CARTi
AI Robot
1.0


2. Adding and Changing Values in a Dictionary

Output:
{'name': 'CARTi', 'type': 'AI Robot', 'version': 2.0, 'battery': '95%'}


3. Looping Through a Dictionary

Output:
name → CARTi
type → AI Robot
version → 2.0

4. Looping Through a 1-D Array (List)

Output:
Animal: cow
Animal: dog
Animal: cat
Animal: horse
Animal: bird

Output:
Loop index: 0
Loop index: 1
Loop index: 2
Loop index: 3
Loop index: 4


5. Loop with Index (using range and len)

Output:
Index: 0 | Animal: cow
Index: 1 | Animal: dog
Index: 2 | Animal: cat


Output

Row: [1, 2, 3]
Matrix value: 1
Matrix value: 2
Matrix value: 3
Row: [4, 5, 6]
Matrix value: 4
Matrix value: 5
Matrix value: 6
Row: [7, 8, 9]
Matrix value: 7
Matrix value: 8
Matrix value: 9

Explanation

The outer loop prints each full row of the matrix.
The inner loop then prints each element inside that row, one by one.
This helps visualize the 2-D structure — a key concept in A.I. for handling image pixels or data grids


esson 6 – Output (Text Only)

Example 6.1 – Output
Animal: cow
Animal: dog
Animal: cat

Example 6.2 – Output
Animal index: 0 | Name: cow
Animal index: 1 | Name: dog
Animal index: 2 | Name: cat

Example 6.3 – Output
Detected cow with confidence 0.92
Detected dog with confidence 0.85
Detected cat with confidence 0.88

Example 6.4 – Output
High confidence detection: cow (0.92)

Example 6.5 – Output
Detected: cow | Confidence: 0.92
Detected: dog | Confidence: 0.85
Detected: cat | Confidence: 0.88

Example 6.6 – Output
Matrix value: 1
Matrix value: 2
Matrix value: 3
Matrix value: 4
Matrix value: 5
Matrix value: 6
Matrix value: 7
Matrix value: 8
Matrix value: 9

Example 6.7 – Output
—- New Frame —-
Detected: cow | Confidence: 0.91
Detected: dog | Confidence: 0.87
—- New Frame —-
Detected: car | Confidence: 0.94
Detected: person | Confidence: 0.96

Example 6.8 – Output
Detection count: {'cow': 2, 'dog': 1, 'cat': 1}

Example 6.9 – Output
Filtered results: [{'cls': 'person', 'conf': 0.95}, {'cls': 'car', 'conf': 0.88}]

Example 6.10 – Output
Detected person | Confidence: 0.93
Location: (120, 200) → (260, 480)
Detected car | Confidence: 0.89
Location: (300, 150) → (600, 400)


example 7 : let see and understand A.I  yolo real  code

Program start…
image 1/1 C:\AI\PICS\pic1.png: 448×640 1 person, 3 cows, 1 sports ball, 427.0 ms
Speed: 20.1 ms preprocess, 427.0 ms inference, 22.9 ms postprocess per image at shape (1, 3, 448, 640)
Number of cows detected: 3

YOLO processed one image (C:\AI\PICS\pic1.png) resized to 448×640 px and detected 1 person, 3 cows, and 1 sports ball in 427 ms.
It spent 20.1 ms loading/resizing (preprocess), 427 ms detecting (inference), and 22.9 ms filtering results (postprocess).
The model input shape (1, 3, 448, 640) means 1 image, 3 color channels, and the resized dimensions.

Finally, your loop found and printed “Number of cows detected: 3.”


Program start…
image 1/1 C:\AI\PICS\pic1.png: 448×640 1 person, 3 cows, 1 sports ball, 128.1 ms
Speed: 2.3 ms preprocess, 128.1 ms inference, 1.5 ms postprocess per image at shape (1, 3, 448, 640)
Number of cows detected: 3

כתיבת תגובה