Wednesday, October 15, 2025

Python Programming Foundations – Pre-Read

Python Programming Foundations – Pre-Read

Estimated Reading Time: 15–20 minutes Level: Beginner Friendly Prerequisites: None

🎯 Learning Outcomes

  • By the end of this pre-read, you’ll be able to:
  • Understand what Python is and why it’s so popular
  • Create and use variables to store data
  • Work with basic data types in Python
  • Take input from users and display output
  • Write and use simple functions
  • Read from and write to files
  • Set up and use virtual environments for clean project setups

🧭 Why This Topic Matters

Think about applications like Instagram, Netflix, or ChatGPT — all rely on Python for handling data, automation, and logic.

Python is simple to read and write, yet powerful enough for real-world use in:

Learning these foundations gives you the core tools every Python developer uses daily.


🧱 What This Module Covers

We’ll explore:

Variables – storing and reusing data

Virtual Environments (virtualenv) – isolating project libraries

Data Types – understanding numbers, strings, lists, and more

Input and Output – taking user input and printing output

Functions – grouping related code together

File Reading & Writing – interacting with files in Python

These are your building blocks for everything that follows in AI/ML and software development.


Variables

Variables are like containers that hold data. They make your programs flexible and reusable.

Example:

name = "Amit"

age = 21

print(name, age)

 

Here, name and age are variables storing a string and an integer.

You can change or reuse them anytime:

💡 Tip: Variable names should be meaningful, like student_name or total_marks, not x or data1.


2️⃣ Virtual Environments (virtualenv)

When working on multiple Python projects, each might need different library versions. To avoid conflicts, we use virtual environments.

Creating a virtual environment:

python -m venv myenv

 

Activating it (Windows):

myenv\Scripts\activate

 

Activating it (Mac/Linux):

source myenv/bin/activate

 

Once active, any packages you install (like numpy or pandas) stay inside that environment only.

💡 Think of it like: Your personal workspace for each project — neat, separate, and safe!


3️⃣ Data Types

Every value in Python has a data type that tells Python what kind of data it is and how it should behave.

Data Type

Example

Description

int

10

Whole numbers

float

3.14

Decimal numbers

str

"Hello"

Text data

bool

True,False

Logical values

list

[1, 2, 3]

Ordered collection, changeable

tuple

(1, 2, 3)

Ordered, not changeable

dict

{"name": "Amit", "age": 21}

Key-value pairs

Example:

student = {"name": "Amit", "age": 21, "marks": [85, 90, 88]}

print(student["marks"])

 

💡 Real-world analogy: Think of data types as different boxes – each holds a different kind of item (numbers, text, lists).


4️⃣ Input and Output

Input allows you to take user data. Output displays results to the user.

Example:

name = input("Enter your name: ")

print("Welcome,", name)

 

When you run this program, it waits for you to type your name, then greets you.

💡 Why it matters: Input and output make your programs interactive — like taking user details or showing results in an AI model.


5️⃣ Functions

Functions help you group related code into reusable blocks.

Syntax:

 

def greet(name):

    print("Hello,", name)

 

greet("Amit")

 

Here, def defines a new function. You can reuse it multiple times without rewriting the same code.

💡 Think of it as: Writing your own mini-tools that perform a task whenever called.

Example:

def add_numbers(a, b):

    return a + b

 

result = add_numbers(5, 3)

print("Sum:", result)

 

💡Tip: Start with small functions that do one clear job. It keeps code clean and easy to debug.


6️⃣ File Reading & Writing

Python lets you read data from or write data into files — a skill used heavily in data science and automation.

Example: Writing to a file

 

file = open("data.txt", "w")

file.write("Hello Python!\n")

file.close()

 

Example: Reading from a file

 

file = open("data.txt", "r")

content = file.read()

print(content)

file.close()

 

💡 Tip: Use with open() to automatically close files after use:

with open("data.txt", "a") as f:

    f.write("Appending new data\n")

 


🧩 Putting It All Together

Let’s combine everything you’ve learned into one small example.

Goal: Take student data and store it in a file.

Step 1: Take input

name = input("Enter student name: ")

marks = input("Enter marks: ")

 

Step 2: Define a function

def save_data(student_name, student_marks):

    with open("students.txt", "a") as file:

        file.write(student_name + " - " + student_marks + "\n")

    print("Data saved successfully!")

 

Step 3: Call the function

save_data(name, marks)

 

Output: When you run this, a file students.txt will be created or updated with the new record.

This small project uses: Variables Input/Output Functions File Handling


🧠 Quick Recap

Concept

What It Does

Variables

Store data in memory

Data Types

Define kind of data (text, number, etc.)

Input/Output

Interact with users

Functions

Reusable code blocks

File Handling

Read and write files

Virtualenv

Keeps each project environment isolated


🚀 Why You Should Care

These concepts are the foundation of every AI, ML, and Data Science project you’ll build later.

  • Variables and data types form the data layer of your programs
  • Functions organize logic for model building and analysis
  • File handling helps manage datasets and outputs
  • Virtual environments ensure clean project management

Even complex systems like AI chatbots or ML pipelines are just combinations of these basics — done smartly.


🔚 Summary The Key Takeaways

  • Python is a beginner-friendly, powerful language used in many industries.
  • Variables are used to store and reuse data.
  • Data types define how data is stored and processed.
  • input() and print() handle user interaction.
  • Functions make your code organized and reusable.
  • File handling helps you save and read data.
  • Virtual environments keep your projects clean and conflict-free.

With these basics, you’re ready to step into the world of real Python development — where these simple concepts combine to build powerful applications.

 


Lecture Notes: Optimising Numerical Code

Lecture Notes: Optimising Numerical Code Prerequisites: Basic Python programming Understanding of NumPy arrays and vector ...