Language Notes

Python

Data structures, OOP, file handling, automation, popular libraries, and Pythonic best practices.

Back to Resources

Basics

Python is a high-level, interpreted language known for its readability and versatility from web development to AI and data science.

Hello World & Variables
print("Hello, World!")

# Variables (no type declaration needed)
name = "Aditya"
age = 17
is_student = True
gpa = 9.5

# f-strings
print(f"I'm {name}, age {age}")

Data Structures

Lists, Dicts, Tuples, Sets
# List  ordered, mutable
fruits = ["apple", "banana", "cherry"]
fruits.append("mango")

# Dictionary  key-value pairs
student = {"name": "Aditya", "grade": 11, "school": "Aspee Nutan"}
print(student["name"])

# Tuple  ordered, immutable
coords = (19.076, 72.877)

# Set  unordered, unique values
skills = {"Python", "AI", "Web Dev"}

# List comprehension
squares = [x**2 for x in range(10)]

# Dict comprehension
word_len = {w: len(w) for w in ["hello", "world"]}

Functions & Lambdas

Defining Functions
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

# *args and **kwargs
def flex(*args, **kwargs):
    print(args)    # tuple of positional args
    print(kwargs)  # dict of keyword args

# Lambda
double = lambda x: x * 2
nums = [1, 2, 3, 4]
doubled = list(map(lambda x: x * 2, nums))

Object-Oriented Programming

Classes & Inheritance
class Developer:
    def __init__(self, name, lang):
        self.name = name
        self.lang = lang

    def introduce(self):
        return f"I'm {self.name}, I code in {self.lang}"

class AIStudent(Developer):
    def __init__(self, name, lang, school):
        super().__init__(name, lang)
        self.school = school

    def introduce(self):
        return f"{super().introduce()} at {self.school}"

me = AIStudent("Aditya", "Python", "Aspee Nutan")
print(me.introduce())

File Handling

Reading & Writing Files
# Reading
with open("data.txt", "r") as f:
    content = f.read()

# Writing
with open("output.txt", "w") as f:
    f.write("Hello from Python!")

# CSV
import csv
with open("data.csv", "r") as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row)

# JSON
import json
with open("config.json", "r") as f:
    data = json.load(f)

Popular Libraries

  • pandas Data manipulation and analysis
  • numpy Numerical computing
  • matplotlib / plotly Data visualisation
  • streamlit Build data apps fast
  • requests HTTP requests
  • flask / fastapi Web frameworks
  • google-generativeai Gemini API SDK
  • beautifulsoup4 Web scraping
  • scikit-learn Machine learning

Best Practices

  • Follow PEP 8 style guide
  • Use virtual environments (venv) for every project
  • Write docstrings for functions and classes
  • Use type hints for clarity: def greet(name: str) -> str:
  • Handle exceptions with specific except clauses
  • Prefer list comprehensions over manual loops
  • Use with statements for file/resource handling
  • Keep functions short and focused
React Next '