Python Programming for AI
1. Python Introduction & Installation
Python is an easy-to-learn, high-level programming language used in AI due to its simple syntax and vast
libraries. You can download Python from [Link] and install it on Windows, macOS, or Linux.
Recommended tools: IDLE, VS Code, or Jupyter Notebook.
2. First Program: print() Function
The print() function is used to display output on the screen.
Example: print("Hello, AI World!")
Its the first step to interact with the user.
3. Variables and Data Types
Variables store data. Python supports types like int, float, str, bool.
Example:
age = 25
name = "AI Bot"
4. Operators and Expressions
Operators are symbols that perform operations.
Types: Arithmetic (+, -, *, /), Comparison (==, !=), Logical (and, or, not).
Example: if age > 18 and age < 60:
5. Control Structures: if, else, elif
Control flow allows decisions.
Example:
if age < 18:
print("Minor")
elif age < 60:
print("Adult")
else:
print("Senior")
6. Loops: for and while
Loops repeat code.
- for loop: for known repetitions
- while loop: for unknown repetitions
Example:
for i in range(5):
print(i)
7. Functions and Parameters
Functions are reusable code blocks.
Example:
def greet(name):
print("Hello", name)
8. Lists, Tuples, and Dictionaries
- List: ordered, changeable my_list = [1, 2, 3]
- Tuple: ordered, unchangeable my_tuple = (1, 2, 3)
- Dictionary: key-value pairs my_dict = {"name": "AI", "age": 5}
9. Modules and Packages
Modules are files with Python code. Packages are folders with multiple modules.
Example:
import math
print([Link](16))
10. File Handling
Used to read/write files.
Example:
with open("[Link]", "r") as file:
content = [Link]()
11. Error Handling
Used to manage errors using try, except.
Example:
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
12. Object-Oriented Programming (OOP)
OOP allows you to create classes and objects.
Example:
class AI:
def __init__(self, name):
[Link] = name
bot = AI("ChatGPT")