diff --git a/for_loop_&_While_loop.ipynb b/for_loop_&_While_loop.ipynb
new file mode 100644
index 0000000..ab17934
--- /dev/null
+++ b/for_loop_&_While_loop.ipynb
@@ -0,0 +1,2673 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyO9E08Z4uVX1R76aC9jpSgV",
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ ""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "#Introduction to Loops"
+ ],
+ "metadata": {
+ "id": "3WqykKfqmvbi"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "In our every day life we follow routines for many repetitive tasks.\n",
+ "\n",
+ "In the same way we also do a lots of repetitive tasks.In order to handel repetitive task programming languages use loops.\n",
+ "\n",
+ "Python programming language also provides two types of loop:\n",
+ "\n",
+ "1) While loop\n",
+ "\n",
+ "2) For loop\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "QEyfSQqPm69N"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# While Loop\n",
+ "\n",
+ "We use the word ***while*** to make a while loop.It is used to execute a block of statements repeatedly until a given condition is satisfied.\n",
+ "\n",
+ "\n",
+ "When the condition becomes false, the lines of code after the loop will be continued to be executed."
+ ],
+ "metadata": {
+ "id": "0OJ_RKaAoT-v"
+ }
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "D7LAZsP5ms-t"
+ },
+ "outputs": [],
+ "source": [
+ "# syntax\n",
+ "while condition:\n",
+ " code goes here"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Example:\n",
+ "\n",
+ "count = 0\n",
+ "while count < 5:\n",
+ " print(count)\n",
+ " count = count + 1"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "kTtbiou4mt05",
+ "outputId": "ed263f97-49ce-4447-cc5d-27316a6ebf27"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "In above while loop, the condition becomes false when count is 5.\n",
+ "\n",
+ "That is when the loop stops.If we are interested to run block of code once the condition is no longer true, we can use ***else***."
+ ],
+ "metadata": {
+ "id": "f-WkgfYMq2hL"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# syntax\n",
+ "while condition:\n",
+ " code goes here\n",
+ "else:\n",
+ " code goes here"
+ ],
+ "metadata": {
+ "id": "6WX2tt8ymt16"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Example\n",
+ "\n",
+ "count = 0\n",
+ "while count <= 5:\n",
+ " print(count)\n",
+ " count += 1\n",
+ "\n",
+ "else:\n",
+ " print(count)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "USmwGQ9Zmt6M",
+ "outputId": "83c13788-ba0e-4da1-e51e-86b1ce6c8b64"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The above loop condition will be false when count is 5 and the loop stops, and execution starts the else statement.\n",
+ "\n",
+ "As a result 5 will be printed."
+ ],
+ "metadata": {
+ "id": "RP6yoyzttWrk"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "So after getting to know how while loop working , let's understand Basic Structure.\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "zgwOqSQgt5Kd"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "#Basic Structure\n",
+ "\n",
+ "A while loop consists of three main components:\n",
+ "\n",
+ "### 1) Initialization:\n",
+ "Initialize a control variable or variable before entering the loop.This variable is used to control the loop's execution.\n",
+ "\n",
+ "### 2) Condition:\n",
+ "Define a condition that is evaluated before each iteration of the loop.If the condition is true , the loop body is executed ; otherwise , the loop terminates.\n",
+ "\n",
+ "### 3) Loop Body:\n",
+ "The code block that contains the the instructions to be executed as long as the condition is true.\n",
+ "\n",
+ "### pseudocode:"
+ ],
+ "metadata": {
+ "id": "iZ4e4oN6uSRi"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "initialize control_variable\n",
+ "while condition:\n",
+ " # Loop body\n",
+ " # Code to be executed repeatedly\n",
+ " update control_variable\n"
+ ],
+ "metadata": {
+ "id": "ZBXXpTtLmt9Q"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Loop Execution\n",
+ "\n",
+ "The execution of while loop can be summarized as follows:\n",
+ "\n",
+ "1) The control variable is initialized.\n",
+ "\n",
+ "2) The condition is checked.If it's true , the body is executed; otherwise, the loop terminates.\n",
+ "\n",
+ "3) After the loop body is executed,the control variable is updated . This is a crucial step to ensure that the loop eventually terminates.If you forget to update the control variable, you may create an infinite loop.\n",
+ "\n",
+ "4) The condition is checked again. If it's still true, the loop body is executed again. This process continues untill the condition becomes false."
+ ],
+ "metadata": {
+ "id": "BJeS_poVwXgj"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# What is Infinite Loops\n",
+ "\n",
+ "Crae should be taken to avoid inifinite loops, where the conditon never become false.\n",
+ "\n",
+ "This can lead to program crashes or freezes."
+ ],
+ "metadata": {
+ "id": "f9FNWTlXygvK"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# What is Exit Condition ?\n",
+ "\n",
+ "Define a clear exit condition to ensure the loop terminates when it should."
+ ],
+ "metadata": {
+ "id": "dQhidzV4zChX"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "#Use Cases\n",
+ "\n",
+ "While loops are suitable for scenarios where you don't the exact number of iterations beforehand want to repeat a task untill a specific condition is met.\n"
+ ],
+ "metadata": {
+ "id": "vnGQ7zhWzUTt"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Precautions\n",
+ "\n",
+ "Be cautious with the control variable and condition to avoid logic errors and unexpected behavior."
+ ],
+ "metadata": {
+ "id": "l8U3bAOyz6YI"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "#Conclusion\n",
+ "\n",
+ "While loops are essential programming constructs for automating repetitive tasks. By carefully designing the initialization, condition, and loop body, you can control the flow of your program and efficiently solve a wide range of problems. However, it's crucial to pay attention to detail to avoid common pitfalls, such as infinite loops, and ensure your code behaves as expected."
+ ],
+ "metadata": {
+ "id": "4VD-4C_J0PGI"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "#what kind of coding proble we can solve using while loop?"
+ ],
+ "metadata": {
+ "id": "8srF0B4J5COt"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "While loops are versatile control structures that can be used to solve a wide range of coding problems.\n",
+ "\n",
+ "Their ability to repeatedly execute a block of code as long as a certain condition is met makes them suitable for various tasks.\n",
+ "\n",
+ "Here are some common types of coding problems that can be solved using while loops:\n",
+ "\n",
+ "#1)Counting and Summation:\n",
+ "\n",
+ "a) Counting from a starting number to an ending number.\n",
+ "\n",
+ "b) Calculating the sum of numbers in a sequence.\n",
+ "\n",
+ "c) Counting occurrences of specific elements in a list.\n",
+ "\n",
+ "#2)User Input and Validation:\n",
+ "\n",
+ "a) Repeatedly prompting the user for input until valid input is provided.\n",
+ "\n",
+ "b) Creating interactive menu-driven programs.\n",
+ "\n",
+ "#3) Searching and Filtering:\n",
+ "\n",
+ "a)Searching for an element in an array or list.\n",
+ "\n",
+ "b)Filtering elements based on a certain condition.\n",
+ "\n",
+ "#4)Data Processing and Transformation:\n",
+ "\n",
+ "a)Parsing and processing data from a file line by line.\n",
+ "\n",
+ "b)Transforming data, such as converting units or formatting text.\n",
+ "\n",
+ "#5)Simulation and Modeling:\n",
+ "\n",
+ "a)Simulating real-world scenarios, like a game loop.\n",
+ "\n",
+ "b)Modeling physical processes with discrete time steps.\n",
+ "\n",
+ "#6)Handling Unknown Iterations:\n",
+ "\n",
+ "a)Performing a task until a specific condition is met (e.g., finding a target in a game).\n",
+ "\n",
+ "b)Repeating an action until a desired level of accuracy is achieved in numerical calculations.\n",
+ "\n",
+ "#7)Dynamic Problem Solving:\n",
+ "\n",
+ "a)Solving problems where the number of iterations is determined during runtime.\n",
+ "\n",
+ "b)Dynamic programming problems, such as calculating Fibonacci numbers or solving the knapsack problem.\n",
+ "\n",
+ "#8)Dequeuing and Processing Queues:\n",
+ "\n",
+ "a)Implementing algorithms that use a queue data structure.\n",
+ "\n",
+ "b)Processing items in a queue until it's empty.\n",
+ "\n",
+ "#9)Network and I/O Operations:\n",
+ "\n",
+ "a)Receiving and processing data from network sockets.\n",
+ "\n",
+ "b)Reading and writing data from/to files or streams.\n",
+ "\n",
+ "#10)Control Flow:\n",
+ "\n",
+ "a)Implementing custom control flow mechanisms, such as custom state machines.\n",
+ "\n",
+ "b)Creating loops that react to changing conditions or events.\n",
+ "\n",
+ "#11)Error Handling and Recovery:\n",
+ "\n",
+ "a)Handling errors and retries based on error conditions.\n",
+ "\n",
+ "b)Recovering from exceptional situations.\n",
+ "\n",
+ "#12)Recursive Algorithm Replacement:\n",
+ "\n",
+ "a)Some problems that can be solved using recursion can also be solved using iterative approaches with while loops.\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "It's important to note that while loops should be used when the number of iterations is not known in advance or when you want to repeat a task until a specific condition is met. Care should be taken to ensure that the loop eventually terminates to avoid infinite loops. Additionally, while loops can often be replaced with for loops in cases where you know the exact number of iterations in advance."
+ ],
+ "metadata": {
+ "id": "XkWkP6Qs5F8e"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Q1) Write a program that uses a while loop to print numbers from 1 to 100\n",
+ "\n",
+ "num =1\n",
+ "while num <= 100:\n",
+ " print(num)\n",
+ " num += 1"
+ ],
+ "metadata": {
+ "id": "lkLMv3VvmuBO",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "f8507a9c-6bee-4e65-8c2f-b1508755540a"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "9\n",
+ "10\n",
+ "11\n",
+ "12\n",
+ "13\n",
+ "14\n",
+ "15\n",
+ "16\n",
+ "17\n",
+ "18\n",
+ "19\n",
+ "20\n",
+ "21\n",
+ "22\n",
+ "23\n",
+ "24\n",
+ "25\n",
+ "26\n",
+ "27\n",
+ "28\n",
+ "29\n",
+ "30\n",
+ "31\n",
+ "32\n",
+ "33\n",
+ "34\n",
+ "35\n",
+ "36\n",
+ "37\n",
+ "38\n",
+ "39\n",
+ "40\n",
+ "41\n",
+ "42\n",
+ "43\n",
+ "44\n",
+ "45\n",
+ "46\n",
+ "47\n",
+ "48\n",
+ "49\n",
+ "50\n",
+ "51\n",
+ "52\n",
+ "53\n",
+ "54\n",
+ "55\n",
+ "56\n",
+ "57\n",
+ "58\n",
+ "59\n",
+ "60\n",
+ "61\n",
+ "62\n",
+ "63\n",
+ "64\n",
+ "65\n",
+ "66\n",
+ "67\n",
+ "68\n",
+ "69\n",
+ "70\n",
+ "71\n",
+ "72\n",
+ "73\n",
+ "74\n",
+ "75\n",
+ "76\n",
+ "77\n",
+ "78\n",
+ "79\n",
+ "80\n",
+ "81\n",
+ "82\n",
+ "83\n",
+ "84\n",
+ "85\n",
+ "86\n",
+ "87\n",
+ "88\n",
+ "89\n",
+ "90\n",
+ "91\n",
+ "92\n",
+ "93\n",
+ "94\n",
+ "95\n",
+ "96\n",
+ "97\n",
+ "98\n",
+ "99\n",
+ "100\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Sum of Even Numbers:\n",
+ "#Calculate the sum of all even numbers from 1 to 20 using a while loop.\n",
+ "\n",
+ "num = 2\n",
+ "total = 0\n",
+ "while num <= 20:\n",
+ " total += num\n",
+ " num += 2\n",
+ "print(total)"
+ ],
+ "metadata": {
+ "id": "r9MJ13HD0j1y",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "68dfd4a9-050e-4792-fb8d-045af7af20d8"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "110\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#User Input Validation:\n",
+ "#Ask the user to enter a positive integer, and keep asking until they do.\n",
+ "\n",
+ "while True:\n",
+ " user_input = int(input(\"Enter a positive integer:\"))\n",
+ " if user_input > 0:\n",
+ " print(\"Task is completed\")\n",
+ " break"
+ ],
+ "metadata": {
+ "id": "c2tXn0JJ0kH0",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "daa49774-ce68-4f32-d93f-b7fc5dc60155"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Enter a positive integer:23\n",
+ "Task is completed\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Reverse a String:\n",
+ "#Take a string as input and print it in reverse using a while loop.\n",
+ "\n",
+ "text = input(\"Enter a string: \")\n",
+ "index = len(text) - 1\n",
+ "\n",
+ "while index >= 0:\n",
+ " print(text[index], end=\"\")\n",
+ " index -= 1"
+ ],
+ "metadata": {
+ "id": "OgEpLiHJ0kKz",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "0f2b1c60-8836-4807-c613-e8bcc6948d9e"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Enter a string: MY NAME IS SOUMYA\n",
+ "AYMUOS SI EMAN YM"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Factorial Calculation:\n",
+ "#Calculate the factorial of a number entered by the user using a while loop.\n",
+ "\n",
+ "num = int(input(\"Enter a number: \"))\n",
+ "factorial = 1\n",
+ "while num > 0:\n",
+ " factorial *= num\n",
+ " num -= 1\n",
+ "\n",
+ "print(\"Factorial:\", factorial)\n"
+ ],
+ "metadata": {
+ "id": "Imy2jSJ80kO2",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "d36f274f-64b1-4fea-f7c3-b58d31d414de"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Enter a number: 6\n",
+ "Factorial: 720\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Here the code has a logic error. It will not calculate the correct factorial for the input number due to the order of operations inside the loop.\n",
+ "\n",
+ "# num = int(input(\"Enter a number:\"))\n",
+ "This line prompts the user to enter a number and stores it in the \"num\" variable as an integer.\n",
+ "\n",
+ "# factorial = 1\n",
+ "Here , you initialize the \"factorial\" variable to 1, which is correct for calculating the factorial.\n",
+ "\n",
+ "# while num > 0\n",
+ "This line starts a while loop that should countinue as long as 'num' is greater than 0 , just like in the previous example.\n",
+ "\n",
+ "# num -= 1\n",
+ "Inside the loop, you decrement 'num' by 1.This is correct:it's a step toward reaching the condition where 'num' becomes 0 and the loop terminates.\n",
+ "\n",
+ "#factorial *= num\n",
+ "The problem lies here. You are multiplying 'factorial' by 'num' inside the loop before decrementing 'num'.This means you are multiplying 'factorial' by the current value of 'num' before decreasing it.\n",
+ "\n",
+ "This leads to incorrect results because you should multiply by the original value of 'num' before decrementing it."
+ ],
+ "metadata": {
+ "id": "1xrlnzJLISKx"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "num = int(input(\"Enter a number: \"))\n",
+ "factorial = 1\n",
+ "while num > 0:\n",
+ " num -= 1\n",
+ " factorial *= num\n",
+ "print(\"Factorial:\", factorial)"
+ ],
+ "metadata": {
+ "id": "mi1TWwrQ0kRZ",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "9f6a15ab-1982-4fd0-d239-3d56a35756e4"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Enter a number: 6\n",
+ "Factorial: 0\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Prime Number Checker:\n",
+ "#Ask the user to enter a number and determine whether it's prime or not using a while loop.\n",
+ "\n",
+ "num = int(input(\"Enter a number: \"))\n",
+ "is_prime = True\n",
+ "divisor = 2\n",
+ "while divisor < num :\n",
+ " if num % divisor == 0:\n",
+ " is_prime = False\n",
+ " break\n",
+ "\n",
+ " divisor += 1\n",
+ "\n",
+ "if is_prime:\n",
+ " print(num, \"is prime.\")\n",
+ "else:\n",
+ " print(num, \"is not prime.\")"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "wWKHWe3hMN2n",
+ "outputId": "7587c8a4-db71-4387-f831-5fb21f11d2cb"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Enter a number: 21\n",
+ "21 is not prime.\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Number Guessing Game:\n",
+ "#Create a number guessing game where the user has to guess a randomly generated number using a while loop.\n",
+ "\n",
+ "import random\n",
+ "\n",
+ "target = random.randint(1, 100)\n",
+ "attempts = 0\n",
+ "\n",
+ "while True:\n",
+ " guess = int(input(\"Guess the number (1-100):\"))\n",
+ " attempts += 1\n",
+ "\n",
+ " if guess == target:\n",
+ " print(\"Congratulations! You guessed it in\", attempts,\"attempts.\")\n",
+ " break\n",
+ " elif guess < target:\n",
+ " print(\"Try higher.\")\n",
+ " else:\n",
+ " print(\"Try lower.\")"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "uFGM0ZgGMN50",
+ "outputId": "49b3eb82-55e5-4ad1-f9ea-a6e783224c65"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Guess the number (1-100):34\n",
+ "Try higher.\n",
+ "Guess the number (1-100):45\n",
+ "Try higher.\n",
+ "Guess the number (1-100):46\n",
+ "Try higher.\n",
+ "Guess the number (1-100):76\n",
+ "Try lower.\n",
+ "Guess the number (1-100):56\n",
+ "Try higher.\n",
+ "Guess the number (1-100):66\n",
+ "Try higher.\n",
+ "Guess the number (1-100):68\n",
+ "Congratulations! You guessed it in 7 attempts.\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Power of a Number:\n",
+ "#Calculate the result of raising a base number to an exponent using a while loop.\n",
+ "\n",
+ "base = int(input(\"Enter the base: \"))\n",
+ "exponent = int(input(\"Enter the exponent: \"))\n",
+ "result = 1\n",
+ "\n",
+ "while exponent > 0:\n",
+ " result *= base\n",
+ " exponent -= 1\n",
+ "\n",
+ "print(\"Result:\", result)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "crT-ElkOMN9d",
+ "outputId": "7c67b1b7-88ca-459b-a1c2-5c4a8a6cafc9"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Enter the base: 3456\n",
+ "Enter the exponent: 43\n",
+ "Result: 144102180914865092036679373711749081923950047318462324467720521022303464592605145423453304022555895261782838683394451379740789708755825831710670261846016\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Fibonacci Sequence:\n",
+ "#Generate the first N Fibonacci numbers using a while loop.\n",
+ "\n",
+ "n = int(input(\"Enter the number of Fibonacci numbers to generate: \"))\n",
+ "a, b = 0, 1\n",
+ "count = 0\n",
+ "\n",
+ "while count < n:\n",
+ " print(a, end=\" \")\n",
+ " a, b = b, a + b\n",
+ " count += 1\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "q0Ocg2X1MOBX",
+ "outputId": "fe42bca6-33e1-47e7-ae30-237d62dca2dd"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Enter the number of Fibonacci numbers to generate: 34\n",
+ "0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 "
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Table of a Number:\n",
+ "#Display the multiplication table of a number entered by the user using a while loop.\n",
+ "\n",
+ "num = int(input(\"Enter a number: \"))\n",
+ "multiplier = 1\n",
+ "\n",
+ "while multiplier <= 10:\n",
+ " product = num * multiplier\n",
+ " print(num, \"x\", multiplier, \"=\", product)\n",
+ " multiplier += 1\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "qjzLt-B0MOHf",
+ "outputId": "b7261634-36aa-408d-9f72-c83c2d471f54"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Enter a number: 23\n",
+ "23 x 1 = 23\n",
+ "23 x 2 = 46\n",
+ "23 x 3 = 69\n",
+ "23 x 4 = 92\n",
+ "23 x 5 = 115\n",
+ "23 x 6 = 138\n",
+ "23 x 7 = 161\n",
+ "23 x 8 = 184\n",
+ "23 x 9 = 207\n",
+ "23 x 10 = 230\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Password Verification:\n",
+ "#Implement a simple password verification system where the user enters a password until it matches a predefined password.\n",
+ "\n",
+ "correct_password = 'secret'\n",
+ "\n",
+ "while True:\n",
+ " user_password = input(\"Enter the password: \")\n",
+ " if user_password == correct_password:\n",
+ " print('Access granted!')\n",
+ " break\n",
+ "\n",
+ " else:\n",
+ " print(\"Incorrect password. Try again.\")"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "vOIpzOY1Qfmj",
+ "outputId": "9085bb48-f257-4834-fb02-59c7db2b4b32"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Enter the password: 23456\n",
+ "Incorrect password. Try again.\n",
+ "Enter the password: secret\n",
+ "Access granted!\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Calculate Average:\n",
+ "#Compute the average of a list of numbers entered by the user using a while loop.\n",
+ "\n",
+ "total = 0\n",
+ "count = 0\n",
+ "\n",
+ "while True:\n",
+ " num = float(input(\"Enter a number (or 0 to calculate the average): \"))\n",
+ " if num == 0:\n",
+ " break\n",
+ " total += num\n",
+ " count += 1\n",
+ "\n",
+ "if count > 0:\n",
+ " average = total / count\n",
+ " print(\"Average:\", average)\n",
+ "else:\n",
+ " print(\"No numbers entered.\")"
+ ],
+ "metadata": {
+ "id": "PwXGNECoMOJG"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "#Break and Continue -Part 1\n",
+ "\n",
+ "Break : We use break when we like to get out of or stop the loop.\n",
+ "\n",
+ "\n",
+ " "
+ ],
+ "metadata": {
+ "id": "JNdqzBNAAwCj"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# syntax\n",
+ "while condition:\n",
+ " code goes here\n",
+ " if another_condition:\n",
+ " break"
+ ],
+ "metadata": {
+ "id": "t0I_LvW7MOK4"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "count = 0\n",
+ "while count < 5:\n",
+ " print(count)\n",
+ " count = count + 1\n",
+ " if count == 3:\n",
+ " break"
+ ],
+ "metadata": {
+ "id": "LUvXReaSMOOV",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "4b78a07e-5005-4a5e-adc6-4ffc40b99f35"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Continue:\n",
+ "with the continue statement we can skip the current iteration, and continue with the next:"
+ ],
+ "metadata": {
+ "id": "rD5VGVWWBwXZ"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ " # syntax\n",
+ "while condition:\n",
+ " code goes here\n",
+ " if another_condition:\n",
+ " continue"
+ ],
+ "metadata": {
+ "id": "714RTm0jMOW6"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "count = 0\n",
+ "while count < 5:\n",
+ " if count == 3:\n",
+ " count = count + 1\n",
+ " continue\n",
+ " print(count)\n",
+ " count = count + 1 # while loop only prints 0,1,2 and 4 .It skip 3"
+ ],
+ "metadata": {
+ "id": "w3eiqizNMOY2",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "325eeff9-55ab-4a40-f14d-2ac346667fcd"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "4\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "#For Loop\n",
+ "\n",
+ "A for loop is a control flow structure in programming that allows you to repeatedly execute a block of code a specific number of times. It's typically used when you know beforehand how many times you want to repeat a certain task.\n",
+ "\n",
+ "\n",
+ "Loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary , a set or a string)."
+ ],
+ "metadata": {
+ "id": "fkYT--OzDOTX"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "#examples:\n"
+ ],
+ "metadata": {
+ "id": "3mYeK7g5EBGx"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "for i in range(5):\n",
+ " print(f\"Current value of 'i' is {i} \")"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "2WPSkhoSEEqQ",
+ "outputId": "810a544f-24a5-439a-fd3e-0d77f82abf3f"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Current value of 'i' is 0 \n",
+ "Current value of 'i' is 1 \n",
+ "Current value of 'i' is 2 \n",
+ "Current value of 'i' is 3 \n",
+ "Current value of 'i' is 4 \n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "#Pseudo-code Explanation:"
+ ],
+ "metadata": {
+ "id": "paFVnc7kES9L"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "FOR each value 'i' in the range from 0 to 4 (inclusive):\n",
+ " Print \"Current value of 'i' is 'i'\"\n",
+ " # Code to be executed within the loop\n",
+ "END FOR\n"
+ ],
+ "metadata": {
+ "id": "rRnJK_4eEEmb"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Use case of for loop:\n",
+ "\n",
+ "for loops are versatile and can be used to solve a wide range of coding problems across various domains. Here are some common types of problems that can be solved using for loops:\n",
+ "\n",
+ "##Iteration Over Collections:\n",
+ "\n",
+ "Loop through elements in an array, list, or other data structures.\n",
+ "Process and manipulate each item in the collection.\n",
+ "\n",
+ "##Counting and Accumulation:\n",
+ "\n",
+ "1)Count the number of occurrences of a specific value in a list.\n",
+ "Calculate the sum, average, or other statistics of a list of numbers.\n",
+ "Pattern Generation:\n",
+ "\n",
+ "2)Generate and print patterns like triangles, squares, or any custom shapes.\n",
+ "\n",
+ "3)Create sequences, such as Fibonacci numbers, using loops.\n",
+ "File Processing:\n",
+ "\n",
+ "4)Read lines from a file and process them.\n",
+ "Write data to a file in a structured format.\n",
+ "\n",
+ "##Nested Loops:\n",
+ "\n",
+ "Solve problems that require multiple levels of iteration.\n",
+ "For example, a nested for loop can be used to traverse a 2D array or matrix.\n",
+ "\n",
+ "##Searching and Filtering:\n",
+ "\n",
+ "Search for specific values or elements in a collection.\n",
+ "Filter elements that meet certain conditions and create a new list.\n",
+ "\n",
+ "\n",
+ "##Validation and Input Handling:\n",
+ "\n",
+ "Validate user input by repeatedly asking for input until valid data is provided.\n",
+ "Ensure data meets specific criteria before proceeding.\n",
+ "\n",
+ "##Simulation and Modeling:\n",
+ "\n",
+ "Simulate complex systems or processes by iterating through time steps or events.\n",
+ "Implement numerical simulations using iterations.\n",
+ "\n",
+ "##Generating Sequences:\n",
+ "\n",
+ "Generate arithmetic or geometric sequences based on specific rules.\n",
+ "\n",
+ "##Game Development:\n",
+ "\n",
+ "Implement game loops to update game state and render frames.\n",
+ "Handle user input and game logic within loops.\n",
+ "\n",
+ "##Statistical Analysis:\n",
+ "\n",
+ "Perform statistical analysis on datasets using loops to calculate means, medians, and variances.\n",
+ "\n",
+ "##Iterative Algorithms:\n",
+ "\n",
+ "Implement algorithms that require iterations, such as searching, sorting, or graph traversal algorithms.\n",
+ "\n",
+ "##Automating Repetitive Tasks:\n",
+ "\n",
+ "Automate tasks that need to be performed repeatedly, such as batch processing of files or data.\n",
+ "\n",
+ "##Generating Reports and Output:\n",
+ "\n",
+ "Create reports, invoices, or data summaries by iterating through data and formatting it.\n",
+ "\n",
+ "##Calendar and Date Calculations:\n",
+ "\n",
+ "Calculate dates, days of the week, or holidays for a given year or range of years.\n"
+ ],
+ "metadata": {
+ "id": "VY0alTu1FadX"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Iterative over collection:\n",
+ "\n",
+ "my_list = [1,2,3,4,5]\n",
+ "for item in my_list:\n",
+ " print(item)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "oEnX7hYTGNte",
+ "outputId": "d279bc2d-2176-4d08-8e17-1dfc49a3779f"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Counting and Accumulation:\n",
+ "\n",
+ "numbers = [1,2,3,4,1,2,1]\n",
+ "count = 0\n",
+ "for num in numbers:\n",
+ " if num == 1:\n",
+ " count += 1\n",
+ "print(\"Count of 1s:\",count)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "N2DmM_BjGj0u",
+ "outputId": "3c8f5922-4ad1-4fe2-eb4b-5f6f63e623c1"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Count of 1s: 3\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Pattern Generation (Triangle):\n",
+ "\n",
+ "n=5\n",
+ "for i in range(1, n + 1):\n",
+ " print(\"*\" * i)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "Wwk_ogZoGj32",
+ "outputId": "9ec2fd4e-07a0-477e-dbb6-f9f9f6123d0a"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "*\n",
+ "**\n",
+ "***\n",
+ "****\n",
+ "*****\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# File Processing(Reading):\n",
+ "\n",
+ "with open(\"sample.txt\", \"r\") as file:\n",
+ " for line in file:\n",
+ " print(line.strip())\n"
+ ],
+ "metadata": {
+ "id": "AiPQNXfzGj_b"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Nested Loops:\n",
+ "\n",
+ "for i in range(3):\n",
+ " for j in range(3):\n",
+ " print(f\"{i},{j}\")"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "eW8qHK6EGPFg",
+ "outputId": "4f30eb96-086a-467b-de7f-0a2bb917b987"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "0,0\n",
+ "0,1\n",
+ "0,2\n",
+ "1,0\n",
+ "1,1\n",
+ "1,2\n",
+ "2,0\n",
+ "2,1\n",
+ "2,2\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Searching and Filtering:\n",
+ "\n",
+ "numbers = [10,20,30,40,50,60]\n",
+ "target = 30\n",
+ "found = False\n",
+ "for num in numbers:\n",
+ " if num == target:\n",
+ " found = True\n",
+ " break\n",
+ "print(\"Found:\", found)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "gRy0G70AGO5-",
+ "outputId": "7b6d2f75-1597-41cb-e20e-2a3f48b4e675"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Found: True\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Validation and Input Handling:\n",
+ "\n",
+ "while True:\n",
+ " user_input = input(\"Enter a positive number: \")\n",
+ " if user_input.isdigit() and int(user_input) > 0:\n",
+ " break\n",
+ "print(\"Valid input:\", user_input)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "x9u4EIpeK6og",
+ "outputId": "95d5a060-f2f2-4e78-8b7a-3361905eeaa8"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Enter a positive number: 23\n",
+ "Valid input: 23\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Simulation and Modeling:\n",
+ "\n",
+ "total = 1000\n",
+ "interest_rate = 0.05\n",
+ "years = 5\n",
+ "for year in range(1, years + 1):\n",
+ " total = total * (1 + interest_rate)\n",
+ " print(f\"Year {year}: ${total:.2f}\")"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "iIwRaadLK6lC",
+ "outputId": "9d7cf178-a49b-48e3-89e6-3a8f47421ece"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Year 1: $1050.00\n",
+ "Year 2: $1102.50\n",
+ "Year 3: $1157.62\n",
+ "Year 4: $1215.51\n",
+ "Year 5: $1276.28\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Generating Sequence (Fibonacci):\n",
+ "\n",
+ "n = 10\n",
+ "a,b = 0,1\n",
+ "for _ in range(n):\n",
+ " print(a, end=\" \")\n",
+ " a, b = b, a+b"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "lepIp6vGGOrT",
+ "outputId": "1ce49412-c4b5-4161-d91a-6ce87eaea64c"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "0 1 1 2 3 5 8 13 21 34 "
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Automating Repetitive Tasks:\n",
+ "import os\n",
+ "\n",
+ "for filename in os.listdir(\"data/\"):\n",
+ " if filename.endswith(\".txt\"):\n",
+ " process_file(os.path.join(\"data/\", filename))\n"
+ ],
+ "metadata": {
+ "id": "27qLqxtsEEXI"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# How for loop is used in data analysis ?. write 10 demo code."
+ ],
+ "metadata": {
+ "id": "yP_adzV6NnhR"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "for loops are commonly used in data analysis to iterate over datasets, perform calculations, filter data, and generate insights.\n",
+ "\n",
+ "Here are 10 demo code snippets that illustrate how for loops can be used in data analysis tasks using Python. These examples assume you have a list of data or a dataset represented as a list of dictionaries."
+ ],
+ "metadata": {
+ "id": "v1GPBwSvNx-k"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Calculating the Mean of a Numeric Column:\n",
+ "\n",
+ "data = [10, 15, 20, 25, 30]\n",
+ "total = 0\n",
+ "for value in data:\n",
+ " total += value\n",
+ "mean = total / len(data)\n",
+ "print(\"Mean:\", mean)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "NVp9LIGQNmQT",
+ "outputId": "4d1ab98e-3fcd-43c6-ef91-31c0e0b2e424"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Mean: 20.0\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Filtering Data Based on a Condition:\n",
+ "\n",
+ "data = [\n",
+ " {\"name\": \"Alice\", \"age\": 25},\n",
+ " {\"name\": \"Bob\", \"age\": 30},\n",
+ " {\"name\": \"Charlie\", \"age\": 22},\n",
+ "]\n",
+ "young_people = []\n",
+ "for person in data:\n",
+ " if person[\"age\"] < 30:\n",
+ " young_people.append(person)\n",
+ "print(\"Young people:\", young_people)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "AgjfGw5cNmTX",
+ "outputId": "da4fc37f-f2b2-4f65-f9c9-83be52fcee75"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Young people: [{'name': 'Alice', 'age': 25}, {'name': 'Charlie', 'age': 22}]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Counting Occurrences of a Specific Value:\n",
+ "\n",
+ "data = [1, 2, 3, 2, 4, 2, 5]\n",
+ "count = 0\n",
+ "target = 2\n",
+ "for value in data:\n",
+ " if value == target:\n",
+ " count += 1\n",
+ "print(f\"{target} appears {count} times.\")\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "6G2A_NvbNmW5",
+ "outputId": "179359b3-d47e-45b7-8e47-df436ec5b0d7"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "2 appears 3 times.\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Aggregating Data by Category:\n",
+ "\n",
+ "data = [\n",
+ " {\"category\": \"A\", \"value\": 10},\n",
+ " {\"category\": \"B\", \"value\": 15},\n",
+ " {\"category\": \"A\", \"value\": 20},\n",
+ "]\n",
+ "category_totals = {}\n",
+ "for entry in data:\n",
+ " category = entry[\"category\"]\n",
+ " value = entry[\"value\"]\n",
+ " if category in category_totals:\n",
+ " category_totals[category] += value\n",
+ " else:\n",
+ " category_totals[category] = value\n",
+ "print(\"Category Totals:\", category_totals)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "kziDTQDNNmk1",
+ "outputId": "a80fa644-e4f1-42ba-85e1-bcd4ca448801"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Category Totals: {'A': 30, 'B': 15}\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Finding Maximum and Minimum Values:\n",
+ "\n",
+ "data = [15, 30, 10, 45, 20]\n",
+ "max_value = float(\"-inf\")\n",
+ "min_value = float(\"inf\")\n",
+ "for value in data:\n",
+ " if value > max_value:\n",
+ " max_value = value\n",
+ " if value < min_value:\n",
+ " min_value = value\n",
+ "print(\"Max Value:\", max_value)\n",
+ "print(\"Min Value:\", min_value)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "joSzQdfYNmmw",
+ "outputId": "93c8fb8b-5b17-422e-f8c9-b836a6edfc49"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Max Value: 45\n",
+ "Min Value: 10\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Data Transformation:\n",
+ "\n",
+ "data = [1, 2, 3, 4, 5]\n",
+ "squared_data = []\n",
+ "for value in data:\n",
+ " squared_data.append(value ** 2)\n",
+ "print(\"Squared Data:\", squared_data)\n"
+ ],
+ "metadata": {
+ "id": "0ydYdKiuMOaj",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "09735ddc-69b7-4033-b5f1-dde043da2569"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Squared Data: [1, 4, 9, 16, 25]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Removing Outliers:\n",
+ "data = [10, 15, 20, 500, 25]\n",
+ "threshold = 100\n",
+ "filtered_data = [value for value in data if value <= threshold]\n",
+ "print(\"Filtered Data:\", filtered_data)\n"
+ ],
+ "metadata": {
+ "id": "jVeaGpfy0kU9",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "3bcecd4c-367c-43e8-ab6d-7dcc71225f98"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Filtered Data: [10, 15, 20, 25]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Calculating Percentages:\n",
+ "\n",
+ "data = [20,30,40,10,5]\n",
+ "total = sum(data)\n",
+ "percentages = [(value / total) * 100 for value in data]\n",
+ "print(\"Percentages:\", percentages)"
+ ],
+ "metadata": {
+ "id": "D8ci88PB0kZA",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "48f8b901-5307-44c1-f66f-f36027b290b6"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Percentages: [19.047619047619047, 28.57142857142857, 38.095238095238095, 9.523809523809524, 4.761904761904762]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Grouping Data by Date:\n",
+ "\n",
+ "data = [\n",
+ " {\"date\": \"2023-01-01\", \"value\": 10},\n",
+ " {\"date\": \"2023-01-02\", \"value\": 15},\n",
+ " {\"date\": \"2023-01-01\", \"value\": 20},\n",
+ "]\n",
+ "\n",
+ "daily_totals = {}\n",
+ "for entry in data:\n",
+ " date = entry[\"date\"]\n",
+ " value = entry[\"value\"]\n",
+ " if date in daily_totals:\n",
+ " daily_totals[date] += value\n",
+ " else:\n",
+ " daily_totals[date] = value\n",
+ "print(\"Daily Totals:\",daily_totals)\n"
+ ],
+ "metadata": {
+ "id": "SnR13BQWmuFN",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "bf2c5d10-af11-4222-8986-0ff46a87a5b2"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Daily Totals: {'2023-01-01': 30, '2023-01-02': 15}\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Data Visualization using Matplotlib:\n",
+ "\n",
+ "import matplotlib.pyplot as plt\n",
+ "\n",
+ "data = [10,20,30,40,50]\n",
+ "x_values = range(len(data))\n",
+ "plt.bar(x_values, data)\n",
+ "plt.xticks(x_values,[\"A\",\"B\",\"C\",\"D\",\"E\"])\n",
+ "plt.xlabel(\"Categories\")\n",
+ "plt.ylabel(\"Values\")\n",
+ "plt.title(\"Bar Chart\")\n",
+ "plt.show()\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 472
+ },
+ "id": "8-FkLxOnP1L-",
+ "outputId": "eb6380ff-39b3-4beb-eeb3-9fc1b9ef452a"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "display_data",
+ "data": {
+ "text/plain": [
+ "