Python Essentials for AI: Practice Exercises
📝 Practice Exercises
Test your Python skills with these exercises. Try them yourself first, then jump to the solution using the links provided.
-
Variables & Data Types: Create variables for your name, age, height, and student status. Print their types.
Jump to Solution
-
Conditionals: Write a program that prints "Adult" if age ≥ 18, otherwise "Minor".
Jump to Solution
-
Loops: Use a for loop to print all even numbers from 1 to 10.
Jump to Solution
-
Functions: Write a function that takes two numbers and returns their product.
Jump to Solution
-
Lists & Dictionaries: Create a list of your favorite fruits and a dictionary of a student's name and score. Print specific elements.
Jump to Solution
-
NumPy Array Operations: Create a NumPy array of [1,2,3,4] and multiply it by 3.
Jump to Solution
-
Pandas DataFrame: Create a DataFrame with Names ["Alice","Bob"] and Scores [85,90]. Print the DataFrame.
Jump to Solution
-
Advanced Challenge: Write a function using list comprehension that returns squares of numbers from a given list.
Jump to Solution
✅ Solutions
1. Variables & Data Types
name = "Alice"
age = 25
height = 5.6
is_student = True
print(type(name), type(age), type(height), type(is_student))
2. Conditionals
age = 20
if age >= 18:
print("Adult")
else:
print("Minor")
3. Loops
for i in range(1, 11):
if i % 2 == 0:
print(i)
4. Functions
def multiply(a, b):
return a * b
print(multiply(3, 4))
5. Lists & Dictionaries
fruits = ["apple", "banana", "cherry"]
print(fruits[1])
student = {"name": "John", "score": 90}
print(student["name"])
6. NumPy Array Operations
import numpy as np
array = np.array([1, 2, 3, 4])
print(array * 3)
7. Pandas DataFrame
import pandas as pd
data = {"Name": ["Alice", "Bob"], "Score": [85, 90]}
df = pd.DataFrame(data)
print(df)
8. Advanced Challenge
numbers = [1, 2, 3, 4, 5]
squares = [x**2 for x in numbers]
print(squares)
💡 Back to Part 2: Python Essentials for AI