Part 2: Python Essentials for AI
๐ฐ Why Learn Python Before AI?
Before diving into AI models, you need a solid foundation in Python. Python’s syntax is beginner-friendly, and its libraries are essential for handling data and building AI systems.
๐งฑ Python Fundamentals
Let’s explore the core building blocks you'll need for AI development:
1️⃣ Variables and Data Types
name = "Alice" # String
age = 25 # Integer
height = 5.6 # Float
is_student = True # Boolean
You can check types using type()
:
print(type(height)) # Output: <class 'float'>
2️⃣ Conditionals (if/else statements)
score = 85
if score >= 90:
print("Excellent")
elif score >= 70:
print("Good")
else:
print("Needs improvement")
3️⃣ Loops (for and while)
# For loop
for i in range(5):
print("AI is powerful!")
# While loop
counter = 0
while counter < 3:
print("Learning...")
counter += 1
4️⃣ Functions
Functions let you reuse code efficiently:
def multiply(a, b):
return a * b
result = multiply(3, 4)
print(result) # Output: 12
5️⃣ Data Structures
Lists
fruits = ["apple", "banana", "cherry"]
print(fruits[1]) # Output: banana
Dictionaries
student = {"name": "John", "age": 21}
print(student["name"]) # Output: John
Tuples
coordinates = (10, 20)
๐ Working with NumPy and Pandas (Essential for AI)
These libraries are core tools for data handling in AI:
๐น NumPy (Numerical Python)
import numpy as np
array = np.array([1, 2, 3, 4])
print(array * 2) # Output: [2 4 6 8]
๐น Pandas (Data Analysis Library)
import pandas as pd
data = {
"Name": ["Alice", "Bob"],
"Score": [85, 90]
}
df = pd.DataFrame(data)
print(df)
Output:
Name Score
0 Alice 85
1 Bob 90
๐งช Practice Challenge
Try writing a function that takes a list of numbers and returns the average:
def average(numbers):
return sum(numbers) / len(numbers)
print(average([10, 20, 30])) # Output: 20.0
๐ What You’ve Learned:
-
Python basics: variables, functions, loops
-
Working with lists, dictionaries, and tuples
-
Intro to NumPy and Pandas
๐งญ What’s Next?
In Part 3, we’ll explore core AI concepts like problem-solving, rule-based systems, and simple AI logic—all implemented with Python.
No comments:
Post a Comment