Python for AI & Data Science
Explore the fundamental concepts and tools that make Python the leading language in Artificial Intelligence. This lab is your starting point for building intelligent systems.
Interactive Demo: Law of Contradiction
A core principle in logic is that a proposition cannot be both true and false at the same time. This demo illustrates that `P` and `not P` is always false.
Foundational Libraries for AI/ML
NumPy: The Foundation of Scientific Computing
NumPy (Numerical Python) is the core library for scientific computing. Its most important object is the `ndarray`, a multi-dimensional array that is significantly faster and more memory-efficient than Python's built-in lists for numerical operations. In AI/ML, these arrays are used to store and manipulate data, from images to the weights of a neural network.
For more information, see the official NumPy documentation: docs.numpy.org.
Key Concepts:
- N-dimensional Arrays (`ndarray`): The primary data structure for efficient, vectorized operations.
- Vectorized Operations: Performing element-wise operations without explicit loops, which is a major performance benefit.
- Array Creation: Functions like `np.empty()`, `np.full()`, `np.zeros()`, and `np.array()` for creating arrays.
import numpy as np
# Create an array of zeros
zeros_array = np.zeros((2, 3), dtype=int)
print("Array with zeros:\n", zeros_array)
# Perform a vectorized operation
arr = np.array([1, 2, 3])
squared_arr = arr ** 2
print("\nSquared array:", squared_arr)
Pandas: The Data Analyst's Best Friend
Built on top of NumPy, Pandas is a data manipulation and analysis library. It introduces two powerful data structures: `Series` (a one-dimensional labeled array) and `DataFrame` (a two-dimensional labeled data table, like a spreadsheet). In AI/ML, Pandas is crucial for data preprocessing, cleaning, and exploratory data analysis (EDA).
For more information, see the official Pandas documentation: docs.pandas.pydata.org.
Key Concepts:
- Series: A single column of data with an index.
- DataFrame: The primary data structure for structured data, consisting of columns and rows.
- Data Manipulation: Functions for filtering, sorting, merging, and grouping data.
import pandas as pd
import numpy as np
exam_data = {
'name': ['A', 'B', 'C'],
'score': [12.5, np.nan, 16.5],
'qualify': ['yes', 'no', 'yes']
}
df = pd.DataFrame(exam_data, index=['a', 'b', 'c'])
print("Original DataFrame:\n", df)
# Select rows where score is greater than 10
qualified_students = df[df['score'] > 10]
print("\nStudents with score > 10:\n", qualified_students)
Foundational Programming Skills
Conditional Logic: `if`, `elif`, `else`
Essential for building algorithms, conditionals allow your program to make decisions. In AI, this is used in everything from simple rules-based systems to complex control flow in machine learning models.
if score > 90:
action = "Excellent"
elif score > 70:
action = "Good"
else:
action = "Needs Improvement"
Loops & Iteration: `for`, `while`, `range()`
Loops are used to perform repetitive tasks, such as processing a large dataset, training a model over multiple epochs, or iterating through a list of students. The `range()` function is a powerful tool for controlling the number of iterations.
for student in student_list:
process_data(student)
for i in range(1, 101): # Loop for 100 students
print(f"Processing student {i}")