{ "cells": [ { "cell_type": "markdown", "id": "482260fe-2993-428c-b503-1790f5bb9488", "metadata": { "id": "482260fe-2993-428c-b503-1790f5bb9488" }, "source": [ "# Python Challenge Questions: For Loop" ] }, { "cell_type": "markdown", "id": "e54a1fff-4da8-48db-a941-de92c9383219", "metadata": { "id": "e54a1fff-4da8-48db-a941-de92c9383219" }, "source": [ "# Question 1:\n", "\n", "Given a nested list of integers, write a Python code using a for loop and list comprehension to flatten the nested list and then create a new list that contains only the square of each even number from the flattened list.\n", "\n", "**Expected Output**\n", "\n", "[4, 16, 36, 64, 100]\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8b430c36-fdda-48d5-94c3-ad73174d5383", "metadata": { "id": "8b430c36-fdda-48d5-94c3-ad73174d5383", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "f9e350a4-11e4-47cb-b973-c75c2e5bbc1a" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[4, 16, 36, 64, 100]\n" ] } ], "source": [ "nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]\n", "\n", "# creating an empty list to store the elements (values) from nested_list\n", "flat_list = []\n", "\n", "# iterating over the nested_list\n", "for item in nested_list:\n", " # checking the iterator type, if it is list, then again iterate over it\n", " if type(item) == list:\n", " # iterating over the list (sub-element in nested_list)\n", " for sub_item in item:\n", " # adding the values into the newly created list (flat_list)\n", " flat_list.append(sub_item)\n", " # else the iterator type is not list, but a value, then adding the value into the newly created list (flat_list)\n", " else:\n", " flat_list.append(sub_item)\n", "\n", "# using list comprehension iterating over all the elements in flat_list, then extracting those elements which are even, and then storing the squares of it into new list\n", "list_containing_square_of_even_numbers_from_flattened_list = [i**2 for i in flat_list if ((i % 2) == 0)]\n", "\n", "# printing the new list containing the square of even numbers from flat_list\n", "print(list_containing_square_of_even_numbers_from_flattened_list)" ] }, { "cell_type": "markdown", "id": "75bf6ef9-cbfd-4a5f-a9d3-49b2b5f426dc", "metadata": { "id": "75bf6ef9-cbfd-4a5f-a9d3-49b2b5f426dc" }, "source": [ "# Question 2:\n", "\n", "You have a list of strings where each string represents a sentence. Write a Python code using a for loop and while loop to count the number of vowels in each sentence. Store the results in a dictionary where the key is the sentence and the value is the count of vowels in that sentence.\n", "\n", "**Expected Output" ] }, { "cell_type": "markdown", "source": [ "# **Using For Loop**" ], "metadata": { "id": "Dn40FtL9tIDy" }, "id": "Dn40FtL9tIDy" }, { "cell_type": "code", "source": [ "sentences_list = [\"The sun rises in the east\", \"She smiled warmly\", \"The cat chased the mouse\", \"His laughter echoed through the room\", \"The coffee smelled delicious\"]\n", "\n", "# creating an empty dictionary to store the key-value pair of sentence & its vowel count\n", "dict_sentence_vowelCount = {}\n", "\n", "# iterating over each sentence in a list\n", "for sentence in sentences_list:\n", " # initializing a variable to store and manage the vowel count in each sentence at every iteration\n", " vowelCount = 0\n", " # iterating over each character in a sentence (string)\n", " for char in sentence.lower():\n", " # checking if encountered character is vowel (a/e/i/o/u) or not and if it is vowel then incrementing the value of vowelCount variable by 1\n", " if char in ['a', 'e', 'i', 'o', 'u']:\n", " vowelCount += 1\n", " # adding the key-value pair of sentence-&-vowelCount into the dictionary\n", " dict_sentence_vowelCount[sentence] = vowelCount\n", "\n", "# printing the dictionary containing the key-value pair of sentence-VowelCount\n", "print(dict_sentence_vowelCount)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "szZ5_VkuiwqZ", "outputId": "40df589f-7c63-40d4-adce-9b19b19e4bc8" }, "id": "szZ5_VkuiwqZ", "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{'The sun rises in the east': 8, 'She smiled warmly': 4, 'The cat chased the mouse': 8, 'His laughter echoed through the room': 12, 'The coffee smelled delicious': 11}\n" ] } ] }, { "cell_type": "markdown", "source": [ "# **Using While Loop**" ], "metadata": { "id": "EJ07q0FVtOOu" }, "id": "EJ07q0FVtOOu" }, { "cell_type": "code", "source": [ "sentences_list = [\"The sun rises in the east\", \"She smiled warmly\", \"The cat chased the mouse\", \"His laughter echoed through the room\", \"The coffee smelled delicious\"]\n", "\n", "# creating an empty dictionary to store key-value pair of sentence & vowel count\n", "dictionary_sentence_vowel = {}\n", "\n", "# variable to manage the iteration so that, it doesn't throw 'OutOfIndex' error\n", "count_sentences = 0\n", "\n", "# performing iteration till the length (size) of list of sentences\n", "while count_sentences < len(sentences_list):\n", " # variable to store the vowel count in each sentence at every iteration\n", " count_vowel = 0\n", " # variable to manage the iteration, so that, it doesn't throw 'OutOfIndex' error\n", " len_sentence = 0\n", " # performing iteration till end of sentence (string)\n", " while len_sentence < len(sentences_list[count_sentences]):\n", " # checking if encountered character (iterator) is vowel or not, and if it is then incrementing the vowel count variable by 1\n", " if sentences_list[count_sentences][len_sentence] in ['a', 'A', 'e', 'E', 'i', 'I', 'o', 'O','u', 'U']:\n", " count_vowel += 1\n", " len_sentence += 1\n", " # adding the sentence-string (key) to the dictionary with count_vowel (pair)\n", " dictionary_sentence_vowel[sentences_list[count_sentences]] = count_vowel\n", " count_sentences += 1\n", "\n", "# printing the dictionary containing the key-value pair of sentence & count_vowel\n", "print(dictionary_sentence_vowel)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "XmBRR1u2tS-_", "outputId": "3d5922fc-0a79-441d-8704-8e7e745704c5" }, "id": "XmBRR1u2tS-_", "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{'The sun rises in the east': 8, 'She smiled warmly': 4, 'The cat chased the mouse': 8, 'His laughter echoed through the room': 12, 'The coffee smelled delicious': 11}\n" ] } ] } ], "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.11.8" }, "colab": { "provenance": [] } }, "nbformat": 4, "nbformat_minor": 5 }