Module 1: Python Foundations#

CodeVision Python Training

Contents#

  • Part 1: Core Syntax & Types (Sections 1–4)

  • Part 2: Data Structures (Sections 5–8)

  • Part 3: Control Flow & Logic (Sections 9–11)

  • Part 4: Data Handling & Environment (Sections 12–15)


Welcome to Module 1#

This module brings everyone—regardless of background—to a strong, practical baseline in Python. Python is the foundation of the entire CodeVision stack. By the end of this module, you will be comfortable reading, writing, and running code that works with real data.

We focus on doing, not theory. Every concept here is tied to examples you will reuse later for data wrangling, machine learning, and AI pipelines.


Part 1: Core Syntax & Types#

1. Python Fundamentals#

Python is designed to be readable and expressive. Unlike many other languages, Python uses indentation (whitespace) to define code blocks rather than curly braces. This structure forces you to write clean, readable code.

# Standard print statement
print("Hello, CodeVision")

# Indentation defines the block
def start_engine():
    print("Engine started")     # Indented line
    print("Ready to go")        # Indented line

start_engine()
Hello, CodeVision
Engine started
Ready to go

2. Variables and Core Data Types#

Variables store values. Python is dynamically typed, meaning it automatically determines the type based on the value you assign.

name = "Jon"          # String (str)
age = 45              # Integer (int)
active = True         # Boolean (bool)
balance = 1023.75     # Float (decimal number)

# Checking a type
print(type(active))   # Output: <class 'bool'>
<class 'bool'>

3. Strings and Formatting#

Strings are text sequences. In modern Python, we use f-strings to inject variables directly into text. This is critical for generating dynamic messages or file paths.

role = "Engineer"
level = 3

# f-string formatting
print(f"User: {role}, Level: {level}")
# Output: User: Engineer, Level: 3

# Common string methods
filename = "  data.csv  "
print(filename.strip().upper())
# Output: DATA.CSV
User: Engineer, Level: 3
DATA.CSV

4. Type Conversion#

Sometimes data arrives in the wrong format (e.g., numbers stored as text in a CSV). You must cast them to the correct type to perform math.

value_str = "100"
# total = value_str + 50  # This would cause an Error

# Convert string to integer
total = int(value_str) + 50
print(total)
# Output: 150
150

Part 2: Data Structures#

5. Lists#

Lists are ordered, mutable collections. They are the default tool for storing sequences of items.

tools = ["Python", "Pandas", "FAISS"]

# Adding an item
tools.append("Ollama")

# Accessing by index (0-based)
print(tools[0])
# Output: Python

print(tools)
Python
['Python', 'Pandas', 'FAISS', 'Ollama']

6. Tuples#

Tuples are immutable sequences. Once created, they cannot be changed. They are faster than lists and used to protect data integrity (e.g., coordinates, configuration constants).

# Defined with parentheses
dimensions = (1920, 1080)

print(f"Width: {dimensions[0]}, Height: {dimensions[1]}")

# dimensions[0] = 1280  # This would cause a TypeError!
Width: 1920, Height: 1080

7. Sets#

Sets are unordered collections of unique elements. They are incredibly useful for deduplication (removing duplicates) and membership testing.

# Defined with curly braces but no keys
tags = {"python", "ai", "python", "data"}

print(tags)
# Output: {'python', 'ai', 'data'} (Duplicate 'python' removed)
{'data', 'python', 'ai'}

8. Dictionaries#

Dictionaries store Key-Value pairs. They are the standard way to represent structured records (similar to JSON objects).

user = {
    "name": "Jon",
    "role": "Engineer",
    "active": True
}

# Access value by key
print(user["name"])
# Output: Jon
Jon

Part 3: Control Flow & Logic#

9. Conditionals#

Logic flow is controlled using if, elif, and else statements.

score = 85

if score >= 90:
    print("Distinction")
elif score >= 80:
    print("Pass")
else:
    print("Fail")
# Output: Pass
Pass

10. Loops#

  • For Loops: Used when you know how many times to iterate (e.g., through a list).

  • While Loops: Used when you need to loop until a condition changes.

# For loop
tools = ["Python", "Pandas"]
for tool in tools:
    print(tool)

print("---")

# Range loop (prints 1, 2, 3)
for i in range(1, 4):
    print(i)
Python
Pandas
---
1
2
3

11. Functions#

Functions group reusable logic. A function accepts input (arguments) and usually returns a result using the return keyword.

def double_value(x):
    return x * 2

result = double_value(10)
print(result)
# Output: 20
20

Part 4: Data Handling & Environment#

12. Modules and Libraries#

Python’s power comes from its ecosystem. You use import to load external code (modules).

import math
import random

print(math.sqrt(16))      # Output: 4.0
print(random.random())    # Output: 0.854... (random)
4.0
0.4997274059227098

13. File Handling#

Always use the with open(...) statement to handle files. It ensures the file is automatically closed even if errors occur, preventing data corruption.

# Writing to a file
with open("log.txt", "w") as f:
    f.write("System Start")

# Reading from a file
with open("log.txt", "r") as f:
    content = f.read()
    print(content)
System Start

14. JSON Processing#

JSON is the standard format for data exchange in AI and APIs. Python’s json module translates between JSON text strings and Python dictionaries.

import json

data = {"status": "ok", "count": 42}

# Convert Dict -> JSON String
json_string = json.dumps(data, indent=2)
print(json_string)

# Convert JSON String -> Dict
parsed_data = json.loads(json_string)
print(parsed_data["status"])
{
  "status": "ok",
  "count": 42
}
ok

15. Jupyter vs. Scripts & Virtual Environments#

Jupyter Notebooks (.ipynb)

  • Purpose: Interactive experimentation, data visualization, and learning.

  • Launch Online: Click the Rocketship Icon at the top of this page to launch this content in JupyterLab, Jupyter Notebook, or Google Colab. This allows you to complete the exercises directly in your browser without installing anything locally.

  • Kernel: The “brain” running the code. If variables act strangely, use Restart Kernel to clear memory and start fresh.

Python Scripts (.py)

  • Purpose: Automation, production pipelines, and scheduled tasks.

  • Execution: Run from the terminal using python my_script.py.

Virtual Environments

A virtual environment is an isolated folder for a project’s dependencies. It prevents library conflicts between projects (e.g., Project A needs Pandas 1.0, Project B needs Pandas 2.0).

  • Creation: python -m venv venv

  • Activation:

    • Mac/Linux: source venv/bin/activate

    • Windows: venv\Scripts\activate

# Example: Running a script from command line
# python my_script.py

print("Both notebooks and scripts are essential tools!")
print("Notebooks for exploration, scripts for automation.")
Both notebooks and scripts are essential tools!
Notebooks for exploration, scripts for automation.