{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4dab9245",
   "metadata": {},
   "source": [
    "while loop examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1451e01",
   "metadata": {},
   "outputs": [],
   "source": [
    "button = input(\"Time to wake up: snooze or deactivate?\")\n",
    "\n",
    "while button == 'snooze':\n",
    "    button = input(\"Time to wake up: snooze or deactivate?\")\n",
    "\n",
    "print('Have a nice day :)')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a989e9b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 5\n",
    "\n",
    "while n > 0:\n",
    "    print(n)\n",
    "    n = n-1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80042e43",
   "metadata": {},
   "source": [
    "Άσκηση 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d215ee64",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = input(\"Δώσε ακέραιο αριθμό μικρότερο του 20: \")\n",
    "n = int(n)\n",
    "\n",
    "while n >= 20:\n",
    "    n = input(\"Δώσε ακέραιο αριθμό μικρότερο του 20: \")\n",
    "    n = int(n)\n",
    "    \n",
    "print(\"Έδωσες\", n)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "764ad544",
   "metadata": {},
   "source": [
    "Άσκηση 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2e5d4b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A\n",
    "a_n = 0\n",
    "\n",
    "i = 1\n",
    "\n",
    "while i <= 100:\n",
    "    a_n += i**(-i) # ισοδ΄ύναμα: a_n = a_n + i**(-i)\n",
    "    i += 1 # ισοδ΄ύναμα: i = i + 1\n",
    "\n",
    "print(f'A_n = {a_n:.5f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d3407b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# B\n",
    "a_n = 0\n",
    "\n",
    "n = 1\n",
    "\n",
    "while n**(-n) >= 0.0001:\n",
    "    a_n += n**(-n)\n",
    "    n += 1\n",
    "\n",
    "print(f'A_n = {a_n:.5f}')\n",
    "print('Iterations = ', n-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa056a48",
   "metadata": {},
   "source": [
    "for loop examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7917f13",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "997fe54e",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(5,1,-1):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c6c0013",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(5,-1):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "182a9d30",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1,10,1):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2e9fb09",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1,8,2):\n",
    "    print(i*2)\n",
    "print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bd20827",
   "metadata": {},
   "outputs": [],
   "source": [
    "for j in range(4,0,-1):\n",
    "    print(\":)\"*j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0512a0a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "type(range(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c45ed46",
   "metadata": {},
   "source": [
    "Arithmetic series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7578dfb7",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 10\n",
    "S = 0\n",
    "\n",
    "for i in range(1, n+1):\n",
    "    S += i\n",
    "print(\"Sum =\", S)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2724e93",
   "metadata": {},
   "source": [
    "Factorial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e526862b",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 5\n",
    "i = 1\n",
    "fact_w = 1\n",
    "\n",
    "while i<=n:\n",
    "    fact_w *= i\n",
    "    i += 1\n",
    "\n",
    "print(fact_w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28e45b07",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Εναλλακτικά με for\n",
    "\n",
    "fact_f = 1\n",
    "\n",
    "for i in range(1,(n+1)):\n",
    "    fact_f *=i\n",
    "    \n",
    "print(fact_f)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3637b556",
   "metadata": {},
   "source": [
    "Άθροισμα ανά 2 όρους"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3d93113",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_sum = 0\n",
    "\n",
    "for i in range(0,10,2):\n",
    "    my_sum +=i\n",
    "\n",
    "print(my_sum)"
   ]
  }
 ],
 "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.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
