{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## While loop" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#first while loop\n", "health = 10\n", "while health > 0 : \n", " print(health)\n", " health-=1\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Exercises\n", "flag = True\n", "while flag :\n", " ans= input(\"Enter input\")\n", " if ans.lower() == \"quit\" :\n", " flag = False" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "game_over = False\n", "while not game_over:\n", " for i in range(5):\n", " print(i)\n", " if i+1 == 3:\n", " game_over = True\n", " break\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## For Loops" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#for loop using range\n", "for num in range(5):\n", " print(\"Value : {}\".format(num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#providing start,stop and step for range\n", "for num in range(0,10,2) :\n", " print(\"Value : {}\".format(num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#printing all characters in a name using for loop\n", "name =\"John Smith\"\n", "for character in name:\n", " print(\"Value : {}\".format(character))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#using two or more loops together\n", "for i in range(2):#outer loop\n", " for j in range(3):#inner loop\n", " print(i,j)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#using continue statement with a for loop\n", "for num in range(5):\n", " if num ==3:\n", " continue\n", " print(\"Value : {}\".format(num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#using break statement with a for loop\n", "for num in range(5):\n", " if num ==3:\n", " break\n", " print(\"Value : {}\".format(num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#setting a placeholder using the pass keyword\n", "for i in range(5):\n", " #to do\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Exercises\n", "for num in range(100) :\n", " if num % 3 == 0 :\n", " print(num)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "ans=input(\"Enter a word\")\n", "for c in ans:\n", " if c in \"aeiou\":\n", " print(c)\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 10, 15.2, 20]\n" ] } ], "source": [ "#declaring a list of numbers\n", "nums =[5,10,15.2,20]\n", "print(nums)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "20\n" ] } ], "source": [ "#accessing elements within a list\n", "print(nums[1])\n", "num = nums[3]\n", "print(num)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4.3, 'word', True]\n" ] } ], "source": [ "#declaring a list of mixed datatypes\n", "num=4.3\n", "data=[num,\"word\",True]\n", "print(data)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 'book', [34, 'Hello'], True]\n", "[34, 'Hello']\n" ] } ], "source": [ "#understanding lists within a list\n", "data = [5,\"book\",[34,\"Hello\"],True]\n", "print(data)\n", "print(data[2])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "34\n", "Hello\n", "e\n" ] } ], "source": [ "#using double bracket notation to access lists within lists\n", "print(data[2][0])\n", "inner_list = data[2]\n", "print(inner_list[1])\n", "print(data[2][1][1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#changing the values in a list through index\n", "data = [5,10,15,20]\n", "print(data)\n", "data[1]= 100\n", "print(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variable storage" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1158508121352\n", "1158508121352\n" ] } ], "source": [ "a=[5,10]\n", "print(id(a)) #large number representing location in memory\n", "b=a\n", "print(id(b))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a: [5, 10]\tb: [5, 10]\n", "Location at a[0] : 140716806545936\tLocation at b[0] : 140716806545936\n", "a: [20, 10]\tb: [20, 10]\n", "a: [20, 30]\tb: [20, 30]\n" ] } ], "source": [ "#understanding how lists are stored\n", "a=[5,10]\n", "b=a\n", "print(\"a: {}\\tb: {}\".format(a,b))\n", "print(\"Location at a[0] : {}\\tLocation at b[0] : {}\".format(id(a[0]),id(b[0])))\n", "b[0]=20 #redeclaring b[0] also changes a[0]\n", "print(\"a: {}\\tb: {}\".format(a,b))\n", "a[1]=30 #redeclaring a[1] also changes b[1]\n", "print(\"a: {}\\tb: {}\".format(a,b))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#using [:] to copy a list\n", "data = [5,10,15,2]\n", "data_copy=data[:]\n", "data_copy[0]=50\n", "print(\"data : {}\\ndata_copy : {}\".format(data,data_copy))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#using copy function to copy a list\n", "data = [5,10,15,2]\n", "data_copy=data.copy()\n", "data_copy[0]=50\n", "print(\"data : {}\\ndata_copy : {}\".format(data,data_copy))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Exercises\n", "sports = [\"Cricket\",\"Football\",\"Basketball\",\"Volleyball\"]\n", "print(\"I like to play {}\\nI like to play {}\\nI like to play {}\\nI like to play {}\".format(sports[0],sports[1],sports[2],sports[3]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "names = [\"John\",\"Ibrahim\",\"Sam\",\"Kelly\"]\n", "print(\"{} , {} , {} , {}\".format(names[0][0],names[1][0],names[2][0],names[3][0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Working with Lists" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#checking the nu: of items in a list\n", "num = [5,10,15]\n", "print(len(num))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10, 15]\n", "[5, 10]\n", "[5, 15, 25]\n", "[25, 30]\n", "[30, 25, 20, 15, 10, 5]\n" ] } ], "source": [ "#accessing specific items in a list using slicing [start:stop:step]\n", "num = [5,10,15,20,25,30]\n", "print(num[1:3])\n", "print(num[:2])\n", "print(num[::2])\n", "print(num[-2:])\n", "print(num[-1::-1])#reverse a list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#adding an item to the end of the list\n", "nums=[5,10,15]\n", "nums.append(1)\n", "print(nums)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['ball', 'base', 'glove']\n" ] } ], "source": [ "#adding an item at an index position\n", "words=[\"ball\",\"glove\"]\n", "words.insert(1,\"base\")\n", "print(words)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 'ball', True]\n", "5 \n", " ['ball']\n" ] } ], "source": [ "#using pop to remove items and save it to a variable\n", "items=[5,\"ball\",True]\n", "print(items)\n", "items.pop()#removes last item from the list\n", "removed_item=items.pop(0)#removes item and saves it to a variable\n", "print(removed_item,\"\\n\",items)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['baseball', 'football', 'hockey']\n" ] } ], "source": [ "#using remove method with try and except\n", "sports=[\"baseball\",\"soccer\",\"football\",\"hockey\"]\n", "try:\n", " sports.remove(\"soccer\")\n", "except:\n", " print(\"Item does not exist in the list\")\n", "print(sports)\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#using min,max,sum\n", "num=[5,3,9]\n", "print(min(num))\n", "print(max(num))\n", "print(sum(num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#using sorted on lists for numerical and alphabetical data\n", "num=[5,8,0,2]\n", "sorted_num = sorted(num)\n", "print(sorted_num,\"\\n\",num)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#sorting a list in place \n", "num=[5,8,0,2]\n", "print(num)\n", "num.sort()\n", "print(num)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#using conditional statements in list\n", "names=[\"Jack\",\"Robert\",\"Mary\"]\n", "if \"Jack\" in names:\n", " print(\"Found\")\n", "if \"Jimmy\" not in names:\n", " print(\"Not Found\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#checking empty lists\n", "nums=[]\n", "if not nums:\n", " print(\"Empty\")\n", "if nums == []:\n", " print(\"Empty\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#using for loop to print items in a list\n", "sports=[\"Baseball\",\"Hockey\",\"Football\",\"Basketball\"]\n", "for s in sports:\n", " print(s)\n", "print(sports)\n", "#using while loop to print items in a list\n", "while sports:\n", " print(sports.pop())\n", "print(sports)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#using while loop to remove items from a list\n", "names = [\"Bob\",\"Jack\",\"Rob\",\"Bob\",\"Robert\"]\n", "while \"Bob\" in names:\n", " names.remove(\"Bob\")\n", "print(names)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Exercises\n", "names=[\"Bob\",\"Kenny\",\"Amanda\",\"Bob\",\"Kenny\"]\n", "distinct_names=[]\n", "for n in names:\n", " if n not in distinct_names:\n", " distinct_names.append(n)\n", "print(distinct_names) \n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "flag = True\n", "inp=[]\n", "while flag:\n", " ans = input(\"Enter input\")\n", " if ans.lower() == \"quit\":\n", " flag= False\n", " break\n", " else:\n", " inp.append(ans)\n", "for n in inp:\n", " print(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Weekly exercise" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "size = int(input(\"Enter size of pyramid:\"))\n", "spaces = size\n", "for i in range(1,size+1):\n", " print(\" \"*spaces+\"X \"*i)\n", " spaces-=1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "names = ['John',' ','Amanda',5]\n", "for chr in names:\n", " if type(chr) != 'str':#check element is a string\n", " chr=str(chr)\n", " name = chr.strip()#strip spaces\n", " if len(name) != 0:\n", " print(name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "temps=[32,12,44,29]\n", "fah=[]\n", "for t in temps:\n", " f = (9 * t ) / 5 + 32\n", " fah.append(f)\n", "print(fah)\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }