Skip to content
473 changes: 473 additions & 0 deletions Untitled2.ipynb

Large diffs are not rendered by default.

416 changes: 416 additions & 0 deletions Untitled3.ipynb

Large diffs are not rendered by default.

291 changes: 291 additions & 0 deletions Untitled4.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Untitled4.ipynb",
"version": "0.3.2",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"[View in Colaboratory](https://colab.research.google.com/github/ASIF8240233397/Assignment-1/blob/ASIF8240233397/Untitled4.ipynb)"
]
},
{
"metadata": {
"id": "OL6c8amtTUaD",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 119
},
"outputId": "4d45a09c-01b6-4b3b-90c4-e884cc97ac44"
},
"cell_type": "code",
"source": [
"# Simple while\n",
"# Loop runs till the condition is True\n",
"v1 = 5\n",
"while v1 <= 10:\n",
" print (v1)\n",
" v1 += 1"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "qLs4URYsTdRs",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# Infinite Loops\n",
"while 1:\n",
" print (1)\n",
" \n",
"while True:\n",
" print (1)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "9dbJ6GU-TgVm",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "2098c86e-161e-4f84-e794-16a0ec0e6b1c"
},
"cell_type": "code",
"source": [
"# One Liner while\n",
"v1 = 0\n",
"while v1 <= 40: v1 += 1\n",
"print (v1)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"41\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "nRh8cLVTTqcO",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "ac619988-f986-4d72-e401-30aa56bb2cd2"
},
"cell_type": "code",
"source": [
"v1 = 2\n",
"++v1\n",
"++v1\n",
"print (v1)"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"2\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "oLznfARXTvKn",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# Terminate loop on a certain user input\n",
"# Note: The loop will break only when the user inputs 100\n",
"v1 = 1\n",
"while v1 != 100:\n",
" v1 = int(input(\"Enter new v1: \"))\n",
" print (\"v1 modified to: \" + str(v1))"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "9SBSiHiGT3ia",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 'break' -> breaks out of loop, doesn't execute any statement after it\n",
"while 1:\n",
" v1 = int(input())\n",
" if v1 == 100: \n",
" break;\n",
" print (v1)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "pF7mmykoT9he",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 'continue' -> continues to next iteration, skips all statements after it for that iteration\n",
"# Note: When 'v1' < 100 the last print statement is skipped and the control moves to the next iteration\n",
"while 1:\n",
" print (\"Iteration begins\")\n",
" v1 = int(input())\n",
" if v1 == 100:\n",
" break;\n",
" elif v1 < 100:\n",
" print (\"v1 less than 100\")\n",
" continue;\n",
" print (\"Iteration complete\")"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "oWWDTaY9UHNv",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "f9853ef4-0d82-4127-b415-d2eddd09e9f9"
},
"cell_type": "code",
"source": [
"# while with lists\n",
"l1 = [\"Jennifer\", 12, \"Python\", 'A', 56, 'B', 2.12, \"Scarlett\"]\n",
"l2 = []\n",
"i = 0\n",
"while i < len(l1):\n",
" if type(l1[i]) == int:\n",
" l2.append(l1[i])\n",
" i += 1\n",
"print (l2)"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": [
"[12, 56]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "Qxh7TMd6UKEA",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "74503dc7-4707-4f3e-ef60-210abb272d16"
},
"cell_type": "code",
"source": [
"# Removing all instances of a specific value in list\n",
"l1 = ['A', 'B', 'C', 'D', 'A', 'E', 'Q', 'A', 'Z', 'A', 'Q', 'D', 'A']\n",
"while 'A' in l1: l1.remove('A')\n",
"print (l1)"
],
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": [
"['B', 'C', 'D', 'E', 'Q', 'Z', 'Q', 'D']\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "4gv8cxRlUOPF",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 90
},
"outputId": "0be2db46-ca04-4698-c835-d42574d6e07b"
},
"cell_type": "code",
"source": [
"# Filing a dictionary\n",
"d1 = {}\n",
"while 1:\n",
" key = input(\"Enter a key: \")\n",
" value = input(\"Enter a value: \")\n",
" d1[key] = value;\n",
" if input(\"exit? \") == \"yes\": break;\n",
"print (d1)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Enter a key: ASU\n",
"Enter a value: 250\n",
"exit? 25\n"
],
"name": "stdout"
}
]
}
]
}
Loading