{ "cells": [ { "cell_type": "markdown", "id": "c3e21f76", "metadata": {}, "source": [ "# Loop Operations" ] }, { "cell_type": "code", "execution_count": 78, "id": "6ccbd87a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b\n", "a\n", "n\n", "a\n", "n\n", "a\n" ] } ], "source": [ "# Character iteration example\n", "for x in \"banana\":\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 79, "id": "a2f8970f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n", "banana\n", "cherry\n" ] } ], "source": [ "# List iteration example\n", "fruits = [\"apple\", \"banana\", \"cherry\"]\n", "for x in fruits:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 80, "id": "4a666d26", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n" ] } ], "source": [ "# List iteration with break example\n", "fruits = [\"apple\", \"banana\", \"cherry\"]\n", "for x in fruits:\n", " if x == \"banana\":\n", " break\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 81, "id": "f07d007a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n", "cherry\n" ] } ], "source": [ "# List iteration with continue example\n", "fruits = [\"apple\", \"banana\", \"cherry\"]\n", "for x in fruits:\n", " if x == \"banana\":\n", " continue\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 82, "id": "1f89c7d3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n" ] } ], "source": [ "# Loop with range example\n", "for x in range(6):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 83, "id": "cbf03e9b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n", "4\n", "5\n" ] } ], "source": [ "# loop with range and start example\n", "for x in range(2, 6):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 84, "id": "811e2ffc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "5\n", "8\n", "11\n", "14\n", "17\n", "20\n", "23\n", "26\n", "29\n" ] } ], "source": [ "# Loop with range example with step\n", "for x in range(2, 30, 3):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 85, "id": "331755b3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "Finally finished!\n" ] } ], "source": [ "# Loop with else example\n", "for x in range(6):\n", " print(x)\n", "else:\n", " print(\"Finally finished!\")" ] }, { "cell_type": "code", "execution_count": 86, "id": "8adec0ef", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "red apple\n", "red banana\n", "red cherry\n", "big apple\n", "big banana\n", "big cherry\n", "tasty apple\n", "tasty banana\n", "tasty cherry\n" ] } ], "source": [ "# Nested loops example\n", "adj = [\"red\", \"big\", \"tasty\"]\n", "fruits = [\"apple\", \"banana\", \"cherry\"]\n", "\n", "for x in adj:\n", " for y in fruits:\n", " print(x, y)" ] }, { "cell_type": "code", "execution_count": 87, "id": "cdbd0d25", "metadata": {}, "outputs": [], "source": [ "# Loop with pass example\n", "for x in [0, 1, 2]:\n", " pass" ] }, { "cell_type": "markdown", "id": "df4de683", "metadata": {}, "source": [ "# Basic Loop Exercise" ] }, { "cell_type": "code", "execution_count": 88, "id": "a6718b53", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "# Print Numbers from 1 to 10\n", "for i in range(1, 11):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 89, "id": "7dcb2102", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "4\n", "6\n", "8\n", "10\n", "12\n", "14\n", "16\n", "18\n", "20\n", "22\n", "24\n", "26\n", "28\n", "30\n", "32\n", "34\n", "36\n", "38\n", "40\n", "42\n", "44\n", "46\n", "48\n", "50\n" ] } ], "source": [ "# Print Even Numbers from 1 to 50\n", "for i in range(2, 51, 2):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 90, "id": "4901d8e5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n", "16\n", "25\n", "36\n", "49\n", "64\n", "81\n", "100\n" ] } ], "source": [ "# Print Squares of Numbers 1 to 10\n", "for i in range(1, 11):\n", " print(i * i)" ] }, { "cell_type": "code", "execution_count": 91, "id": "47f05e67", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5050\n" ] } ], "source": [ "# Sum of First N Numbers\n", "n = 100\n", "total = 0\n", "for i in range(1, n + 1):\n", " total += i\n", "print(total)" ] }, { "cell_type": "code", "execution_count": 92, "id": "2305a1ef", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 x 1 = 5\n", "5 x 2 = 10\n", "5 x 3 = 15\n", "5 x 4 = 20\n", "5 x 5 = 25\n", "5 x 6 = 30\n", "5 x 7 = 35\n", "5 x 8 = 40\n", "5 x 9 = 45\n", "5 x 10 = 50\n" ] } ], "source": [ "# Multiplication Table\n", "for i in range(1, 11):\n", " print(f\"5 x {i} = {5 * i}\")" ] }, { "cell_type": "code", "execution_count": 93, "id": "7ea44b23", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* \n", "* * \n", "* * * \n", "* * * * \n", "* * * * * \n" ] } ], "source": [ "# Print a Pattern\n", "# * \n", "# * * \n", "# * * * \n", "# * * * * \n", "# * * * * * \n", "for i in range(1, 6):\n", " print(\"* \" * i)" ] }, { "cell_type": "code", "execution_count": 94, "id": "0d5cf55d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "120\n" ] } ], "source": [ "# Find the Factorial of a Number\n", "n = 5\n", "fact = 1\n", "for i in range(1, n + 1):\n", " fact *= i\n", "print(fact)" ] }, { "cell_type": "code", "execution_count": 95, "id": "cd78c599", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "o\n", "l\n", "l\n", "e\n", "h\n" ] } ], "source": [ "# Reverse a String\n", "word = \"hello\"\n", "for char in reversed(word):\n", " print(char)" ] }, { "cell_type": "code", "execution_count": 96, "id": "d45a816d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "# Count Vowels in a String\n", "word = \"programming\"\n", "vowels = \"aeiou\"\n", "count = 0\n", "for char in word:\n", " if char in vowels:\n", " count += 1\n", "print(count)" ] }, { "cell_type": "code", "execution_count": 97, "id": "dd03f6c7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "30\n" ] } ], "source": [ "# Sum of Elements in a List\n", "numbers = [2, 4, 6, 8, 10]\n", "total = 0\n", "for num in numbers:\n", " total += num\n", "print(total)" ] }, { "cell_type": "code", "execution_count": 98, "id": "8aeb306a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[11, 12, 13]\n" ] } ], "source": [ "# Given nums = [1, 2, 3], add 10 to each number using a loop.\n", "nums = [1, 2, 3]\n", "for i in range(len(nums)):\n", " nums[i] += 10\n", "print(nums)" ] } ], "metadata": { "kernelspec": { "display_name": "all", "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.9" } }, "nbformat": 4, "nbformat_minor": 5 }