No history yet

Python and MySQL Basics

Python's Building Blocks

Python is a popular programming language known for its clear and readable syntax. It's often described as being close to plain English, which makes it a great starting point. At its core, programming involves telling a computer how to handle information. We start by giving names to pieces of data using variables.

Lesson image

A variable is like a labeled box where you can store information. You can put different types of data in these boxes.

variable

noun

A symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name.

The most common data types are:

  • Integers: Whole numbers, like 10 or -5.
  • Floats: Numbers with a decimal point, like 3.14.
  • Strings: Text, enclosed in quotes, like "Hello, world!".
  • Booleans: Represent truth values, either True or False.

Here’s how you assign values to variables in Python:

# This is a comment. Python ignores it.

user_name = "Alice"  # A string
user_age = 30         # An integer
account_balance = 105.50 # A float
is_active = True      # A boolean

Beyond single pieces of data, Python has structures for storing collections. A list is an ordered collection of items, which can be of different types. A dictionary is a collection of key-value pairs, which is useful for storing related information.

# A list of user ages
ages = [25, 30, 22, 45]

# A dictionary for a single user
user_profile = {
    "name": "Bob",
    "age": 42,
    "is_verified": False
}

Making Decisions in Python

Writing a program is more than just storing data. You need to control its flow, making decisions and repeating actions. This is called control flow. It's like a recipe that sometimes says, "If you have lemons, add juice. Otherwise, use vinegar."

In Python, we use if, elif (else if), and else statements to make decisions.

temperature = 25

if temperature > 30:
    print("It's a hot day!")
elif temperature > 20:
    print("It's a pleasant day.")
else:
    print("It's a bit chilly.")

# This will print: It's a pleasant day.

To repeat actions, we use loops. A for loop iterates over a sequence (like a list), while a while loop continues as long as a condition is true.

# A for loop to print each name
names = ["Alice", "Bob", "Charlie"]
for name in names:
    print(f"Hello, {name}!")

# A while loop that counts down
count = 3
while count > 0:
    print(count)
    count = count - 1 # or count -= 1
print("Blast off!")

Control flow structures like if-statements and loops give your program a brain, allowing it to perform complex tasks and react to different situations.

Storing Data with MySQL

Variables are great, but they only exist while your program is running. To store data permanently, we need a database. MySQL is a popular relational database management system.

A relational database organizes data into tables, which are like spreadsheets. Each table has columns (attributes) and rows (records).

relational database

noun

A type of database that stores and provides access to data points that are related to one another.

Imagine a table to store user information. It might have columns for an ID, name, and email address. Each row would represent a unique user.

Talking to Your Database

To interact with a MySQL database, we use a language called SQL (Structured Query Language). SQL lets us create databases and tables, add data, and ask questions about the data stored inside.

First, you might create a new database to hold your project's tables.

CREATE DATABASE my_app;

Once created, you tell MySQL you want to work inside that database. Then you can create a table, specifying the name and data type for each column.

USE my_app;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100)
);

INT is for integers, VARCHAR(100) is for strings up to 100 characters, and PRIMARY KEY marks a column as a unique identifier for each row.

With the table set up, you can add data using INSERT INTO and retrieve it using SELECT.

-- Insert a new row into the users table
INSERT INTO users (name, email) 
VALUES ('Alice', 'alice@example.com');

-- Select all columns (*) from the users table
SELECT * FROM users;

-- Select only the name of the user with id 1
SELECT name FROM users WHERE id = 1;

These basic Python and SQL commands are the foundation for building backend systems. Python handles the logic, while MySQL provides reliable, long-term storage for the application's data.

Ready to check your understanding?

Quiz Questions 1/6

What is the primary purpose of a for loop in Python?

Quiz Questions 2/6

In SQL, a table is organized into columns and rows. What do the columns represent?

With these fundamentals, you're ready to explore how Python applications can connect to and interact with a MySQL database to create powerful, data-driven backends.