Python Programming

Simple, powerful, and incredibly versatile

What is Python?

Python is a high-level, interpreted programming language emphasizing code readability and simplicity. Created by Guido van Rossum in 1991, Python has become one of the most popular languages in the world.

Its clean syntax, extensive standard library, and massive ecosystem make Python ideal for everything from simple scripts to complex machine learning applications.

Key Features

  • Clean, readable syntax that resembles plain English
  • Extensive standard library covering common tasks
  • Rich ecosystem with PyPI hosting 500,000+ packages
  • Cross-platform compatibility and portability
  • Strong support for multiple programming paradigms

Common Use Cases

Data Science & AI

NumPy, Pandas, TensorFlow, and PyTorch for data analysis and machine learning.

Web Development

Django, Flask, and FastAPI for building robust web applications and APIs.

Automation

Scripts for file management, web scraping, testing, and DevOps workflows.


Refer to Python Documentation

Getting Started

Download Python from python.org or use your system's package manager. Verify installation with:

python --version

Start with basic syntax and data structures, then explore libraries that match your interests. The Python community offers excellent resources at docs.python.org and countless tutorials online.

Installing Python

Python 3 is the current version. You can manage multiple Python versions using pyenv:

Using pyenv

# Install pyenv
curl https://pyenv.run | bash

# Install Python 3.12
pyenv install 3.12.0
pyenv global 3.12.0

Virtual Environments

Virtual environments isolate project dependencies. Use venv (built-in) to create them:

# Create a virtual environment
python -m venv myenv

# Activate it
source myenv/bin/activate  # On Windows: myenv\Scripts\activate

# Install packages
pip install package-name

# Save dependencies
pip freeze > requirements.txt

# Deactivate
deactivate

Installing mysql-connector-python

A self-contained Python driver for communicating with MySQL

Using pip:
pip install mysql-connector-python

Standard installation for Python 2.x and 3.x systems where pip is the default.

Using pip3 (Python 3 specific):
pip3 install mysql-connector-python

Explicitly install for Python 3 when both Python 2 and 3 are installed.

Using virtual environment (recommended):

Create virtual environment:

python -m venv myenv

Activate (Linux/Mac):

source myenv/bin/activate

Activate (Windows):

myenv\Scripts\activate

Install package:

pip install mysql-connector-python

Virtual environments isolate dependencies and prevent conflicts between projects.

Using requirements.txt:

Add to requirements.txt:

mysql-connector-python

Install from file:

pip install -r requirements.txt

Use requirements.txt to manage all project dependencies in one file.

💡 Tips:

  • • Verify installation: pip show mysql-connector-python
  • • Upgrade package: pip install --upgrade mysql-connector-python
  • • Uninstall: pip uninstall mysql-connector-python

Hello World Example

Create your first Python script:

#!/usr/bin/env python3

def main():
    print("Hello, World!")

if __name__ == "__main__":
    main()

Save this as hello.py and run it with python hello.py

Popular Python Packages

Requests

Elegant HTTP library for making API calls

Pandas

Data manipulation and analysis library

Flask

Lightweight web framework for building APIs and web apps

NumPy

Fundamental package for scientific computing

Django

Full-featured web framework with batteries included

Pytest

Testing framework that makes writing tests simple

Python Language Basics

Variables and Data Types

# Variables (dynamic typing)
name = "Alice"
age = 30
height = 5.6
is_student = False

# Multiple assignment
x, y, z = 1, 2, 3

String Formatting

# F-strings (Python 3.6+)
name = "Bob"
age = 25
print(f"My name is {name} and I am {age} years old")

# Format method
print("{} is {} years old".format(name, age))

Lists and Dictionaries

# Lists
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")

# Dictionaries
person = {
    "name": "Alice",
    "age": 30,
    "city": "New York"
}