{ "cells": [ { "cell_type": "markdown", "id": "670f024c", "metadata": {}, "source": [ "# Day 1 โ€” Python Foundations & the Security Mindset\n", "### Summer Coding & Cybersecurity Camp\n", "\n", "Welcome! Today you'll write your first Python programs **and** start thinking like\n", "a security-minded programmer from the very first line of code.\n", "\n", "**By the end of today you can:**\n", "- Run code in Google Colab\n", "- Use variables, data types, and `print()`\n", "- Get input from a user and convert it safely\n", "- Use `if / elif / else` to make decisions\n", "- Explain the golden rule of security: **never blindly trust input**\n", "\n", "> ๐ŸŸข **How Colab works:** Click a code cell and press **Shift + Enter** to run it.\n", "> The output appears right below the cell. Try it on the next cell!" ] }, { "cell_type": "code", "execution_count": null, "id": "552aaaba", "metadata": {}, "outputs": [], "source": [ "print(\"Hello, future security engineer! ๐Ÿ”\")" ] }, { "cell_type": "markdown", "id": "8a205f7a", "metadata": {}, "source": [ "## 1. Variables and data types\n", "\n", "A **variable** is a labeled box that stores a value. Python has a few core types:\n", "\n", "| Type | Example | Meaning |\n", "|------|---------|---------|\n", "| `int` | `42` | whole number |\n", "| `float` | `3.14` | decimal number |\n", "| `str` | `\"hello\"` | text (a *string*) |\n", "| `bool` | `True` / `False` | yes / no |\n", "\n", "Run the cell below and read the output carefully." ] }, { "cell_type": "code", "execution_count": null, "id": "9b7c451d", "metadata": {}, "outputs": [], "source": [ "age = 16 # int\n", "height = 1.72 # float\n", "name = \"Sam\" # str\n", "is_student = True # bool\n", "\n", "print(name, \"is\", age, \"years old.\")\n", "print(\"Type of age:\", type(age))\n", "print(\"Type of name:\", type(name))" ] }, { "cell_type": "markdown", "id": "7ebda56b", "metadata": {}, "source": [ "## 2. Talking to the user with `input()`\n", "\n", "`input()` pauses the program and waits for the person to type something.\n", "**Everything `input()` returns is a string** โ€” even if they type a number." ] }, { "cell_type": "code", "execution_count": null, "id": "26ee738c", "metadata": {}, "outputs": [], "source": [ "favorite = input(\"What's your favorite food? \")\n", "print(\"Yum! I like \" + favorite + \" too.\")" ] }, { "cell_type": "markdown", "id": "bcc61891", "metadata": {}, "source": [ "### f-strings: the clean way to build messages\n", "Putting a value inside `{ }` in an f-string drops it right into the text." ] }, { "cell_type": "code", "execution_count": null, "id": "46bb7707", "metadata": {}, "outputs": [], "source": [ "name = input(\"Your name: \")\n", "print(f\"Welcome to camp, {name}! Let's write some secure code.\")" ] }, { "cell_type": "markdown", "id": "1ce33566", "metadata": {}, "source": [ "## 3. ๐Ÿ” Security moment #1: input can break your program\n", "\n", "Computers do **exactly** what we tell them. If we *assume* the user types a number\n", "but they type letters, the program can crash.\n", "\n", "Run the cell and type a **word** (like `banana`) instead of a number. Watch it crash." ] }, { "cell_type": "code", "execution_count": null, "id": "3dd8eb66", "metadata": {}, "outputs": [], "source": [ "number = input(\"Enter a number to double: \")\n", "doubled = int(number) * 2 # int() expects something number-like!\n", "print(\"Doubled:\", doubled)" ] }, { "cell_type": "markdown", "id": "a820c550", "metadata": {}, "source": [ "You probably got a `ValueError`. That red error is Python saying\n", "*\"you promised me a number and gave me text.\"*\n", "\n", "A real attacker (or just a confused user) will type the *unexpected* thing.\n", "**Rule #1 of secure programming: assume input will be wrong, weird, or hostile โ€”\n", "and handle it.** We'll learn the tools to do that tomorrow. For now, just notice\n", "how easily it breaks." ] }, { "cell_type": "markdown", "id": "2e0bb4c2", "metadata": {}, "source": [ "## 4. Making decisions with `if`\n", "\n", "Programs choose what to do based on conditions." ] }, { "cell_type": "code", "execution_count": null, "id": "e2318c32", "metadata": {}, "outputs": [], "source": [ "score = int(input(\"Enter your quiz score (0-100): \"))\n", "\n", "if score >= 90:\n", " print(\"Grade: A ๐ŸŽ‰\")\n", "elif score >= 80:\n", " print(\"Grade: B\")\n", "elif score >= 70:\n", " print(\"Grade: C\")\n", "else:\n", " print(\"Keep practicing!\")" ] }, { "cell_type": "markdown", "id": "f9c52ebf", "metadata": {}, "source": [ "### Comparison & logic operators\n", "- `==` equal, `!=` not equal, `< > <= >=`\n", "- combine conditions with `and`, `or`, `not`" ] }, { "cell_type": "code", "execution_count": null, "id": "c045cf94", "metadata": {}, "outputs": [], "source": [ "password_len = 6\n", "has_symbol = False\n", "\n", "if password_len >= 8 and has_symbol:\n", " print(\"Strong-ish password\")\n", "else:\n", " print(\"โš ๏ธ Weak password โ€” too short or missing a symbol\")" ] }, { "cell_type": "markdown", "id": "b1a1545e", "metadata": {}, "source": [ "---\n", "## ๐Ÿงช LAB 1A โ€” Personalized Greeting\n", "Write a program that:\n", "1. Asks for the user's **name**\n", "2. Asks for their **age**\n", "3. Prints a friendly message using an **f-string**, e.g.\n", " `Hi Sam, in 10 years you'll be 26!`\n", "\n", "Fill in the `TODO`s below." ] }, { "cell_type": "code", "execution_count": null, "id": "041cd036", "metadata": {}, "outputs": [], "source": [ "# LAB 1A โ€” your code here\n", "name = input(\"Name: \")\n", "# TODO: ask for age and convert it to an int\n", "# age = ...\n", "\n", "# TODO: print a message that includes their age in 10 years\n", "# print(f\"...\")" ] }, { "cell_type": "markdown", "id": "ce0228e1", "metadata": {}, "source": [ "## ๐Ÿงช LAB 1B โ€” A Calculator That Doesn't Crash (yet)\n", "Build a simple calculator that:\n", "1. Asks for two numbers\n", "2. Asks for an operation: `+`, `-`, `*`, or `/`\n", "3. Prints the result\n", "\n", "For now, assume the user behaves. After you get it working, **try to break it**:\n", "- What happens if you type letters?\n", "- What happens if you divide by zero?\n", "\n", "Write down what breaks โ€” tomorrow we'll fix it properly." ] }, { "cell_type": "code", "execution_count": null, "id": "36b288b5", "metadata": {}, "outputs": [], "source": [ "# LAB 1B โ€” your code here\n", "a = float(input(\"First number: \"))\n", "b = float(input(\"Second number: \"))\n", "op = input(\"Operation (+ - * /): \")\n", "\n", "# TODO: use if/elif/else to compute and print the result\n" ] }, { "cell_type": "markdown", "id": "b938e70f", "metadata": {}, "source": [ "## ๐Ÿ CHALLENGE โ€” \"Break It, Then Note It\"\n", "Trade calculators with a partner (or test your own). Your mission: **make it crash**\n", "or produce nonsense. For each way you break it, write one sentence describing the bad\n", "input. You're now thinking like a security tester! ๐Ÿ•ต๏ธ\n", "\n", "---\n", "### โœ… Solutions (try the labs first!)" ] }, { "cell_type": "code", "execution_count": null, "id": "f1fbbb57", "metadata": {}, "outputs": [], "source": [ "# --- LAB 1A solution ---\n", "name = input(\"Name: \")\n", "age = int(input(\"Age: \"))\n", "print(f\"Hi {name}, in 10 years you'll be {age + 10}!\")" ] }, { "cell_type": "code", "execution_count": null, "id": "55efb7f6", "metadata": {}, "outputs": [], "source": [ "# --- LAB 1B solution ---\n", "a = float(input(\"First number: \"))\n", "b = float(input(\"Second number: \"))\n", "op = input(\"Operation (+ - * /): \")\n", "\n", "if op == \"+\":\n", " print(\"Result:\", a + b)\n", "elif op == \"-\":\n", " print(\"Result:\", a - b)\n", "elif op == \"*\":\n", " print(\"Result:\", a * b)\n", "elif op == \"/\":\n", " print(\"Result:\", a / b) # still crashes on b == 0 โ€” we fix this tomorrow!\n", "else:\n", " print(\"Unknown operation\")" ] }, { "cell_type": "markdown", "id": "7394f4e7", "metadata": {}, "source": [ "### ๐Ÿ“ Day 1 recap\n", "- Variables hold `int`, `float`, `str`, `bool` values\n", "- `input()` always returns a **string** โ€” convert with `int()` / `float()`\n", "- `if / elif / else` lets programs make decisions\n", "- ๐Ÿ” **Never trust input** โ€” it crashed our calculator, and that's exactly the kind\n", " of weakness attackers look for. Tomorrow: how to handle it safely." ] } ], "metadata": { "colab": { "provenance": [], "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }