Created
July 21, 2023 19:14
-
-
Save JenniferNorthrup/da9f53ad7db83c7d191a079ce43f4cd1 to your computer and use it in GitHub Desktop.
Practice+Exercise+-+Looping_Statements.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/JenniferNorthrup/da9f53ad7db83c7d191a079ce43f4cd1/practice-exercise-looping_statements.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "8-uvpuqmgFN1" | |
}, | |
"source": [ | |
"# **LOOPING STATEMENTS EXERCISE**" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "s5HQs3DmgFN3" | |
}, | |
"source": [ | |
"### 1. Print -10 to -1 using for loop" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "JKsgdznsgFN3", | |
"outputId": "d283e3a2-40af-419c-ae25-6fba1a0cbac6" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"-10\n", | |
"-9\n", | |
"-8\n", | |
"-7\n", | |
"-6\n", | |
"-5\n", | |
"-4\n", | |
"-3\n", | |
"-2\n", | |
"-1\n" | |
] | |
} | |
], | |
"source": [ | |
"for number in range(-10, 0, 1):\n", | |
" print(number)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "zFzWfi65gFN4" | |
}, | |
"source": [ | |
"### 2. Using while loop print numbers 1 to 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "Pgmqh6i5gFN4", | |
"outputId": "2dfa0fb4-039d-46a1-c3a4-89b43246b760" | |
}, | |
"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" | |
] | |
} | |
], | |
"source": [ | |
"num = 1\n", | |
"\n", | |
"while num <= 10:\n", | |
" print(num)\n", | |
" num +=1" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "FcdduEumgFN4" | |
}, | |
"source": [ | |
"### 3. Using for loop print first ten even numbers" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "gYw2Sgr7gFN4", | |
"outputId": "0968d545-f06e-49e8-e4c5-c138f739341b" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"2\n", | |
"4\n", | |
"6\n", | |
"8\n", | |
"10\n", | |
"12\n", | |
"14\n", | |
"16\n", | |
"18\n", | |
"20\n" | |
] | |
} | |
], | |
"source": [ | |
"count = 1\n", | |
"\n", | |
"for number in range(1, 21):\n", | |
" if count <= 10 and number % 2 == 0:\n", | |
" count +=1\n", | |
" print(number)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Bi2EpR0JgFN5" | |
}, | |
"source": [ | |
"### 4. Using while loop generate the table of 5" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "QUQ1EUaGgFN5", | |
"outputId": "8b496c71-4e51-4198-c4ec-cd9e9b87b838" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"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", | |
"5 X 11 = 55\n", | |
"5 X 12 = 60\n" | |
] | |
} | |
], | |
"source": [ | |
"number = 5\n", | |
"multiplier = 1\n", | |
"\n", | |
"while multiplier <= 12:\n", | |
" print(f\"{number} X {multiplier} = {number * multiplier}\")\n", | |
" multiplier += 1" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "bVIMzjEbgFN5" | |
}, | |
"source": [ | |
"### 5. Using for loop generate the table of 8" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "fJSk4PiCgFN5", | |
"outputId": "56e2f4b1-3e17-450f-98a1-54cfa66e640b" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"8 X 1 = 8\n", | |
"8 X 2 = 16\n", | |
"8 X 3 = 24\n", | |
"8 X 4 = 32\n", | |
"8 X 5 = 40\n", | |
"8 X 6 = 48\n", | |
"8 X 7 = 56\n", | |
"8 X 8 = 64\n", | |
"8 X 9 = 72\n", | |
"8 X 10 = 80\n", | |
"8 X 11 = 88\n", | |
"8 X 12 = 96\n" | |
] | |
} | |
], | |
"source": [ | |
"number = 8\n", | |
"\n", | |
"for multiplier in range(1, 13):\n", | |
" print(f\"{number} X {multiplier} = {number * multiplier}\")\n", | |
" multiplier += 1" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "ZbO4nZnugFN5" | |
}, | |
"source": [ | |
"### 6. For numbers 1 to 100, if a number is divisible by 3, then print Fizz.\n", | |
"### If a number is divisible by 5, then print Buzz.\n", | |
"### If a number is divisible by 3 and 5 both, then print FizzBuzz\n", | |
"### If a number is neither divisible by 3 or 5, just print the number.\n", | |
"\n", | |
"Hint: use range(1,101) function to generate numbers from 1 to 100" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "99mv28n7gFN6", | |
"outputId": "ab704249-cec1-4362-bde3-0ff05f6f5b7d" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"1\n", | |
"2\n", | |
"Fizz\n", | |
"4\n", | |
"Buzz\n", | |
"Fizz\n", | |
"7\n", | |
"8\n", | |
"Fizz\n", | |
"Buzz\n", | |
"11\n", | |
"Fizz\n", | |
"13\n", | |
"14\n", | |
"FizzBuzz\n", | |
"16\n", | |
"17\n", | |
"Fizz\n", | |
"19\n", | |
"Buzz\n", | |
"Fizz\n", | |
"22\n", | |
"23\n", | |
"Fizz\n", | |
"Buzz\n", | |
"26\n", | |
"Fizz\n", | |
"28\n", | |
"29\n", | |
"FizzBuzz\n", | |
"31\n", | |
"32\n", | |
"Fizz\n", | |
"34\n", | |
"Buzz\n", | |
"Fizz\n", | |
"37\n", | |
"38\n", | |
"Fizz\n", | |
"Buzz\n", | |
"41\n", | |
"Fizz\n", | |
"43\n", | |
"44\n", | |
"FizzBuzz\n", | |
"46\n", | |
"47\n", | |
"Fizz\n", | |
"49\n", | |
"Buzz\n", | |
"Fizz\n", | |
"52\n", | |
"53\n", | |
"Fizz\n", | |
"Buzz\n", | |
"56\n", | |
"Fizz\n", | |
"58\n", | |
"59\n", | |
"FizzBuzz\n", | |
"61\n", | |
"62\n", | |
"Fizz\n", | |
"64\n", | |
"Buzz\n", | |
"Fizz\n", | |
"67\n", | |
"68\n", | |
"Fizz\n", | |
"Buzz\n", | |
"71\n", | |
"Fizz\n", | |
"73\n", | |
"74\n", | |
"FizzBuzz\n", | |
"76\n", | |
"77\n", | |
"Fizz\n", | |
"79\n", | |
"Buzz\n", | |
"Fizz\n", | |
"82\n", | |
"83\n", | |
"Fizz\n", | |
"Buzz\n", | |
"86\n", | |
"Fizz\n", | |
"88\n", | |
"89\n", | |
"FizzBuzz\n", | |
"91\n", | |
"92\n", | |
"Fizz\n", | |
"94\n", | |
"Buzz\n", | |
"Fizz\n", | |
"97\n", | |
"98\n", | |
"Fizz\n", | |
"Buzz\n" | |
] | |
} | |
], | |
"source": [ | |
"numbers = range(1, 101)\n", | |
"\n", | |
"for number in numbers:\n", | |
" if number % 3 == 0 and number % 5 == 0:\n", | |
" print(\"FizzBuzz\")\n", | |
" elif (number % 3 == 0):\n", | |
" print(\"Fizz\")\n", | |
" elif (number % 5 == 0):\n", | |
" print(\"Buzz\")\n", | |
" else:\n", | |
" print(number)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [], | |
"metadata": { | |
"id": "pa6mf5XJjUss" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "ds2wxHDZgFN6" | |
}, | |
"source": [ | |
"### 7. Using the range function find the sum of all numbers from 1 to 100.\n", | |
" For the numbers in the range (1,100):1+2+3+4+5......+100 = 5050" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "JpwenaMngFN6", | |
"outputId": "ce1a8b91-b6ec-461a-fe5f-df98f1a61998" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"5050\n" | |
] | |
} | |
], | |
"source": [ | |
"print(sum(range(1, 101)))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "aHfwBYQWgFN6" | |
}, | |
"source": [ | |
"### 8. Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, between 202 and 320 (both included)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "MztYpdnLgFN6", | |
"outputId": "6b716b14-0cee-4392-ad2b-2d659041a123" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"203\n", | |
"217\n", | |
"224\n", | |
"231\n", | |
"238\n", | |
"252\n", | |
"259\n", | |
"266\n", | |
"273\n", | |
"287\n", | |
"294\n", | |
"301\n", | |
"308\n" | |
] | |
} | |
], | |
"source": [ | |
"numbers = range(202, 321)\n", | |
"\n", | |
"for number in numbers:\n", | |
" if number % 7 == 0 and number % 5 != 0:\n", | |
" print(number)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "7YdFT4fogFN6" | |
}, | |
"source": [ | |
"### 9. Python Program to count the number of vowels in a string.\n", | |
"st = \"Data Science\"\n", | |
"\n", | |
"Output: No of vowels = 5" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "f52CQ91sgFN7", | |
"outputId": "67ff9a76-a9b5-4f5a-8354-16a248221c8f" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"5\n" | |
] | |
} | |
], | |
"source": [ | |
"st = \"Data Science\"\n", | |
"vowel_defn = \"aeiouAEIOU\"\n", | |
"\n", | |
"vowels = [ch for ch in st if ch in vowels]\n", | |
"print(len(vowels))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Kyw-aZDEgFN7" | |
}, | |
"source": [ | |
"### 10. Print Data Science 5 times using for loop" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "uO-DxrltgFN7", | |
"outputId": "dccf94c9-a6aa-48a7-b007-3c355579ec5a" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Data Science\n", | |
"Data Science\n", | |
"Data Science\n", | |
"Data Science\n", | |
"Data Science\n" | |
] | |
} | |
], | |
"source": [ | |
"for num in range (1, 6):\n", | |
" print(\"Data Science\")" | |
] | |
} | |
], | |
"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.9.7" | |
}, | |
"colab": { | |
"provenance": [], | |
"include_colab_link": true | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment