{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# IF STATEMENT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "write an integer\n",
      "3\n",
      " \n",
      "The number you wrote is smaller than 5\n"
     ]
    }
   ],
   "source": [
    "# if statement\n",
    "\n",
    "print(\"write an integer\")\n",
    "n = input()   # n is passed as a string\n",
    "x = int(float(n))\n",
    "if (x != float(n)):\n",
    "    # Note the indentation!\n",
    "    print(\"This is not an integer, sorry\")\n",
    "else:\n",
    "    if (x == 5):\n",
    "        print('The number you wrote is 5')\n",
    "    elif (x > 5):\n",
    "        print(' ')\n",
    "        print(\"The number you wrote is greater than 5\")\n",
    "    elif (x < 5):\n",
    "        print(' ')\n",
    "        print(\"The number you wrote is smaller than 5\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# FOR STATEMENT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(0, 10)\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "a = range(10)\n",
    "print(a)\n",
    "b = list(range(10))\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in a:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in b:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = ['Hi!', 'How', 'are', 'you', 'today?']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi!\n",
      "How\n",
      "are\n",
      "you\n",
      "today?\n"
     ]
    }
   ],
   "source": [
    "for word in a:\n",
    "    print(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "[0, 1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "n = len(a)\n",
    "print(n)\n",
    "print(list(range(len(a))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi!\n",
      "How\n",
      "are\n",
      "you\n",
      "today?\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(a)):\n",
    "    print(a[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "[12, 20, 30, 42, 56, 72, 90]\n"
     ]
    }
   ],
   "source": [
    "z = [ ]\n",
    "for i in range(3,10):\n",
    "    print(i)\n",
    "    x = i**2\n",
    "    y = i\n",
    "    z.append(x+y)\n",
    "\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array( [[3., 4., 5.], [-1., -2., -3.]] )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3. 4. 5.]\n",
      "[-1. -2. -3.]\n"
     ]
    }
   ],
   "source": [
    "for element in a:\n",
    "    print(element)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rows = 2 cols = 3\n"
     ]
    }
   ],
   "source": [
    "# Gets number of rows and columns in a\n",
    "rows = len(a)\n",
    "cols = len(a[0])\n",
    "print('rows =',rows,'cols =',cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rows = 2 cols = 3\n"
     ]
    }
   ],
   "source": [
    "# Another way to get rows and cols\n",
    "rows, cols = np.shape(a)\n",
    "print('rows =',rows,'cols =',cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n",
      "4.0\n",
      "5.0\n",
      "-1.0\n",
      "-2.0\n",
      "-3.0\n"
     ]
    }
   ],
   "source": [
    "for i in range(rows):\n",
    "    for j in range(cols):\n",
    "        print(a[i,j])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3.0, 4.0, 5.0, -2.0, -3.0]\n"
     ]
    }
   ],
   "source": [
    "x = [ ]\n",
    "for i in range(rows):\n",
    "    for j in range(i,cols):\n",
    "        x.append(a[i,j])\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'float' object cannot be interpreted as an integer",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [18]\u001b[0m, in \u001b[0;36m<cell line: 6>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      4\u001b[0m q \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m2\u001b[39m\n\u001b[1;32m      5\u001b[0m \u001b[38;5;66;03m# -> The following does not work because (p/q) is a float (even if p and q are integers) and range accepts only integers\u001b[39;00m\n\u001b[0;32m----> 6\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28;43mrange\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mp\u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43mq\u001b[49m\u001b[43m)\u001b[49m:\n\u001b[1;32m      7\u001b[0m     n \u001b[38;5;241m=\u001b[39m sqrt(i)\n\u001b[1;32m      8\u001b[0m     \u001b[38;5;28mprint\u001b[39m(i, n)\n",
      "\u001b[0;31mTypeError\u001b[0m: 'float' object cannot be interpreted as an integer"
     ]
    }
   ],
   "source": [
    "from math import sqrt\n",
    "\n",
    "p = 10\n",
    "q = 2\n",
    "# -> The following does not work because (p/q) is a float (even if p and q are integers) and range accepts only integers\n",
    "for i in range(p/q):\n",
    "    n = sqrt(i)\n",
    "    print(i, n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import sqrt\n",
    "\n",
    "p = 10\n",
    "q = 2\n",
    "# The following instead works because (p//q) is integer division and returns an integer\n",
    "for i in range(p//q):\n",
    "    n = sqrt(i)\n",
    "    print(i, n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1,11,2):\n",
    "    print(i, i**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# WHILE STATEMENT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Below is a clumsy way to build a list with all integers from 0 to 10\n",
    "i = 0\n",
    "x = [ ]\n",
    "while i < 10:\n",
    "    x.append(i)\n",
    "    i += 1\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The better way is the following, btw, but it does not allow us to practice with the while statement\n",
    "x = list(range(10))\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Use of the break statement inside while\n",
    "i = 10\n",
    "print(\"Input increment\")\n",
    "step = input()\n",
    "step = int(float(step))\n",
    "print(' ')\n",
    "print(\"Starting from \" + str(i) + \" I will increment by a step = \" + str(step) + \" until I reach 30\")\n",
    "print(\"However I will stop if hit 24\")\n",
    "print( ' ' )\n",
    "while i < 30:\n",
    "    i = i + step\n",
    "    print(i)\n",
    "    if (i==24):\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Use of the break statement inside while\n",
    "i = 10\n",
    "print(\"Input increment\")\n",
    "step = input()\n",
    "step = int(float(step))\n",
    "print(' ')\n",
    "print(\"Starting from \" + str(i) + \" I will increment by a step = \" + str(step) + \" until I reach 30\")\n",
    "print(\"However I will skip printing if/when I hit 24\")\n",
    "print( ' ' )\n",
    "while i < 30:\n",
    "    i = i +step\n",
    "    if (i==24):\n",
    "        continue\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Fibonacci's numbers (series in which each number is the sum of the previous two)\n",
    "f1 = 1\n",
    "f2 = 1\n",
    "while (f1 < 1000):\n",
    "    print(f1)\n",
    "    fnext = f1 + f2\n",
    "    f1 = f2\n",
    "    f2 = fnext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Fibonacci's numbers, more elegant\n",
    "f1 = 1\n",
    "f2 = 1\n",
    "while f1 < 1000:\n",
    "    print(f1)\n",
    "    f1, f2 = f2, f1 + f2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
