{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "847ab51a",
   "metadata": {},
   "source": [
    "# Introduction to Python 'for' Statements"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23b6fbf4",
   "metadata": {},
   "source": [
    "## Python 'for' statements implement \"looping\" in Python.  In \"looping\" one uses 'for' statements to execute a section of their code repeatedly (or iteratively) usually advancing some control variable until some expression becomes false."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e9a8883a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Value = 1, Value squared = 1\n",
      "Value = 2, Value squared = 4\n",
      "Value = 3, Value squared = 9\n",
      "Value = 4, Value squared = 16\n",
      "Value = 5, Value squared = 25\n"
     ]
    }
   ],
   "source": [
    "for dummy in [1, 2, 3, 4, 5]:\n",
    "    print(f\"Value = {dummy}, Value squared = {dummy**2}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ee7f5f12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The third color in the list is:  Yellow\n"
     ]
    }
   ],
   "source": [
    "myLstVar1 = [\"Red\", \"Orange\", \"Yellow\", \"Green\", \"Blue\", \"Indigo\", \"Violet\"]\n",
    "print(\"The third color in the list is: \", myLstVar1[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2667d8d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Red\n",
      "Orange\n",
      "Yellow\n",
      "Green\n",
      "Blue\n",
      "Indigo\n",
      "Violet\n"
     ]
    }
   ],
   "source": [
    "for eachColor in myLstVar1:\n",
    "    print(eachColor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dd6ef6a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "12\n",
      "14\n",
      "16\n",
      "18\n",
      "20\n"
     ]
    }
   ],
   "source": [
    "for j in range(10,21,2):   # range(start, stop, step) returns start, start+step, start+2*step, ... while start + j*step < stop\n",
    "    print(j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "155e8b01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "myLstVar1 contains  7  items.\n",
      "0 Red\n",
      "1 Orange\n",
      "2 Yellow\n",
      "3 Green\n",
      "4 Blue\n",
      "5 Indigo\n",
      "6 Violet\n"
     ]
    }
   ],
   "source": [
    "print(\"myLstVar1 contains \", len(myLstVar1), \" items.\")\n",
    "for i in range(len(myLstVar1)):     # \"range(n)\" gives you series of numbers from 0 ... n-1\n",
    "    print(i, myLstVar1[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "52d35884-9507-485e-9637-25d7201ee6ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "unsorted myLstVar1 contains  7  items. ['Blue', 'Green', 'Indigo', 'Orange', 'Red', 'Violet', 'Yellow']\n",
      "sorted myLstVar1 contains    7  items. ['Blue', 'Green', 'Indigo', 'Orange', 'Red', 'Violet', 'Yellow']\n",
      "0 Blue\n",
      "1 Green\n",
      "2 Indigo\n",
      "3 Orange\n",
      "4 Red\n",
      "5 Violet\n",
      "6 Yellow\n"
     ]
    }
   ],
   "source": [
    "print(\"unsorted myLstVar1 contains \", len(myLstVar1), \" items.\",myLstVar1)\n",
    "myLstVar1.sort()\n",
    "print(\"sorted myLstVar1 contains   \", len(myLstVar1), \" items.\",myLstVar1)\n",
    "for i in range(len(myLstVar1)):     # \"range(n)\" gives you series of numbers from 0 ... n-1\n",
    "    print(i, myLstVar1[i])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
