CoolFace
Datasetpublic

Ace-xvi/Generative_AI

sourceHugging Facemitupdated 11mo agoView on Hugging Face
0likes20downloads
Day8_08_07_2023 .ipynb663 linesDownload Raw Back to 1_Adrash Python Session
1{2 "cells": [3  {4   "cell_type": "markdown",5   "id": "8428fb4f",6   "metadata": {},7   "source": [8    "# Agenda:\n",9    "- Recap\n",10    "- List compregensions\n",11    "- Dictionary Comprehensions\n",12    "- Functions\n",13    "- Various ways of passing parameters to functions\n",14    "- Deep copy and shallow copy\n",15    "- Modules"16   ]17  },18  {19   "cell_type": "code",20   "execution_count": 2,21   "id": "a6fcabc5",22   "metadata": {},23   "outputs": [24    {25     "name": "stdout",26     "output_type": "stream",27     "text": [28      "[1, 2, 3, 4, 9, 8, 10, 5, 6]\n"29     ]30    }31   ],32   "source": [33    "ip_li = [[1,2,3,4],[9,8,10],[5,6]]\n",34    "op_li = []\n",35    "for inner_li in ip_li:\n",36    "    for ele in inner_li:\n",37    "        op_li.append(ele)\n",38    "print(op_li)"39   ]40  },41  {42   "cell_type": "code",43   "execution_count": null,44   "id": "f8500990",45   "metadata": {},46   "outputs": [],47   "source": [48    "### Write a program to find prime numbers from 0 till a given number\n",49    "### Number which is divisable only by itself and 1"50   ]51  },52  {53   "cell_type": "code",54   "execution_count": null,55   "id": "5bbd5839",56   "metadata": {},57   "outputs": [],58   "source": [59    "min_num = input('Please enter the min_num\\n')\n",60    "max_num = input('Please enter the max_num\\n')"61   ]62  },63  {64   "cell_type": "code",65   "execution_count": 15,66   "id": "363e0341",67   "metadata": {},68   "outputs": [],69   "source": [70    "# min_num = 50\n",71    "# max_num = 100\n",72    "primes  = []\n",73    "for i in range(min_num,max_num): # Loop to run through all the numbers\n",74    "    z = 99\n",75    "    for j in range(2,i): # Weather number is divisable by any other number\n",76    "        if i%j==0:\n",77    "            z = 78\n",78    "    if z == 99:\n",79    "        primes.append(i)"80   ]81  },82  {83   "cell_type": "code",84   "execution_count": 16,85   "id": "02303962",86   "metadata": {},87   "outputs": [88    {89     "data": {90      "text/plain": [91       "[53, 59, 61, 67, 71, 73, 79, 83, 89, 97]"92      ]93     },94     "execution_count": 16,95     "metadata": {},96     "output_type": "execute_result"97    }98   ],99   "source": [100    "primes"101   ]102  },103  {104   "cell_type": "code",105   "execution_count": 26,106   "id": "b2872e25",107   "metadata": {},108   "outputs": [],109   "source": [110    "l1 = [34432,56567567,878787,'python','c','java','c#','scala']\n",111    "l2 = ['Python','C','Java','C#','Scala']"112   ]113  },114  {115   "cell_type": "code",116   "execution_count": 30,117   "id": "47911227",118   "metadata": {},119   "outputs": [120    {121     "name": "stdout",122     "output_type": "stream",123     "text": [124      "Both List have same string\n"125     ]126    }127   ],128   "source": [129    "flag = 0\n",130    "for ind,ele in enumerate(l2,3):\n",131    "    if ele.lower() != l1[ind]:\n",132    "        flag = 1\n",133    "if flag == 0:\n",134    "    print('Both List have same string')"135   ]136  },137  {138   "cell_type": "code",139   "execution_count": 33,140   "id": "919a219e",141   "metadata": {},142   "outputs": [],143   "source": [144    "l1 = ['python','c','java','c#','sdufysduf']\n",145    "l2 = ['Python','C','Java','C#','Scala']"146   ]147  },148  {149   "cell_type": "code",150   "execution_count": 35,151   "id": "81342f56",152   "metadata": {},153   "outputs": [154    {155     "name": "stdout",156     "output_type": "stream",157     "text": [158      "Lists have different string\n"159     ]160    }161   ],162   "source": [163    "flag = 0\n",164    "for ind,ele in enumerate(l2,0):\n",165    "    if ele.lower() != l1[ind]:\n",166    "        flag = 1\n",167    "if flag == 0:\n",168    "    print('Both List have same string')\n",169    "else:\n",170    "    print('Lists have different string')\n"171   ]172  },173  {174   "cell_type": "markdown",175   "id": "7d2fe928",176   "metadata": {},177   "source": [178    "### Dictionary comprehension"179   ]180  },181  {182   "cell_type": "code",183   "execution_count": 44,184   "id": "1f7b6f81",185   "metadata": {},186   "outputs": [],187   "source": [188    "text_nums=['Zero','One','Two','Three','Four','Five']\n",189    "num_text_di = {i:text_nums[i] for i in range(6)}\n",190    "li_ind = [i for i in range(6)]"191   ]192  },193  {194   "cell_type": "code",195   "execution_count": 45,196   "id": "dd5f7463",197   "metadata": {},198   "outputs": [199    {200     "data": {201      "text/plain": [202       "{0: 'Zero', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five'}"203      ]204     },205     "execution_count": 45,206     "metadata": {},207     "output_type": "execute_result"208    }209   ],210   "source": [211    "num_text_di"212   ]213  },214  {215   "cell_type": "code",216   "execution_count": 46,217   "id": "9ca4800d",218   "metadata": {},219   "outputs": [220    {221     "data": {222      "text/plain": [223       "[0, 1, 2, 3, 4, 5]"224      ]225     },226     "execution_count": 46,227     "metadata": {},228     "output_type": "execute_result"229    }230   ],231   "source": [232    "li_ind"233   ]234  },235  {236   "cell_type": "markdown",237   "id": "4e2090be",238   "metadata": {},239   "source": [240    "### Problem Statement : Write a dictionary comprehension for elements in a given list,\n",241    "### with key as element of list, and value as weather number is even or odd"242   ]243  },244  {245   "cell_type": "code",246   "execution_count": 62,247   "id": "c5cc6d06",248   "metadata": {},249   "outputs": [],250   "source": [251    "ip = [7,4,6,88,92,31,5]\n",252    "op = {7:'Odd',4:'Even',6:'Even',88:'Even',92:'Even',31:'Odd',5:'Odd'} "253   ]254  },255  {256   "cell_type": "code",257   "execution_count": 63,258   "id": "1afac652",259   "metadata": {},260   "outputs": [],261   "source": [262    "op_di = {i: 'even' if i%2==0 else 'odd' for i in ip}"263   ]264  },265  {266   "cell_type": "code",267   "execution_count": 64,268   "id": "589c5677",269   "metadata": {},270   "outputs": [271    {272     "data": {273      "text/plain": [274       "{7: 'odd', 4: 'even', 6: 'even', 88: 'even', 92: 'even', 31: 'odd', 5: 'odd'}"275      ]276     },277     "execution_count": 64,278     "metadata": {},279     "output_type": "execute_result"280    }281   ],282   "source": [283    "op_di"284   ]285  },286  {287   "cell_type": "code",288   "execution_count": 65,289   "id": "38724ded",290   "metadata": {},291   "outputs": [],292   "source": [293    "### Equivalent for loop is given below"294   ]295  },296  {297   "cell_type": "code",298   "execution_count": 66,299   "id": "943906f6",300   "metadata": {},301   "outputs": [],302   "source": [303    "op_di = {}\n",304    "for i in ip :\n",305    "    if i%2 == 0:\n",306    "        op_di.update({i:'even'})\n",307    "    else:\n",308    "        op_di.update({i:'odd'})"309   ]310  },311  {312   "cell_type": "code",313   "execution_count": 67,314   "id": "1536df7d",315   "metadata": {},316   "outputs": [317    {318     "data": {319      "text/plain": [320       "{7: 'odd', 4: 'even', 6: 'even', 88: 'even', 92: 'even', 31: 'odd', 5: 'odd'}"321      ]322     },323     "execution_count": 67,324     "metadata": {},325     "output_type": "execute_result"326    }327   ],328   "source": [329    "op_di"330   ]331  },332  {333   "cell_type": "markdown",334   "id": "8b69c7da",335   "metadata": {},336   "source": [337    "### Set Comprehension"338   ]339  },340  {341   "cell_type": "code",342   "execution_count": 78,343   "id": "14a300da",344   "metadata": {},345   "outputs": [],346   "source": [347    "tu = (1,3,4,6,8,9,22,3,5,6,1,9)\n",348    "# Set comprehension\n",349    "se_comp_res= {i for i in tu if i%3==0}\n",350    "# List comprahension---- Just to compare with set comprehension we have written\n",351    "li_comp_re = [i for i in tu if i%3==0]"352   ]353  },354  {355   "cell_type": "code",356   "execution_count": 81,357   "id": "81adf3fd",358   "metadata": {},359   "outputs": [360    {361     "name": "stdout",362     "output_type": "stream",363     "text": [364      "{9, 3, 6}\n",365      "[3, 6, 9, 3, 6, 9]\n"366     ]367    }368   ],369   "source": [370    "print(se_comp_res)\n",371    "print(li_comp_re)"372   ]373  },374  {375   "cell_type": "code",376   "execution_count": 83,377   "id": "a1dcf356",378   "metadata": {},379   "outputs": [380    {381     "name": "stdout",382     "output_type": "stream",383     "text": [384      "Python\n",385      "C\n",386      "Java\n"387     ]388    }389   ],390   "source": [391    "li =['Python','C','Java','C##']\n",392    "i =0\n",393    "while i<3:\n",394    "    print(li[i])\n",395    "    i=i+1\n",396    "    "397   ]398  },399  {400   "cell_type": "code",401   "execution_count": 90,402   "id": "5ad6520c",403   "metadata": {},404   "outputs": [405    {406     "name": "stdout",407     "output_type": "stream",408     "text": [409      "Please enter a integer\n",410      "2\n",411      "Please enter a integer\n",412      "3\n",413      "Please enter a integer\n",414      "4\n"415     ]416    }417   ],418   "source": [419    "li = []\n",420    "for i in range(3):\n",421    "    num = int(input('Please enter a integer\\n'))\n",422    "    li.append(num)\n",423    "### Not possible to take arbitrary number of inputs from user "424   ]425  },426  {427   "cell_type": "code",428   "execution_count": 97,429   "id": "d5ab2dd9",430   "metadata": {},431   "outputs": [432    {433     "name": "stdout",434     "output_type": "stream",435     "text": [436      "Please enter a integer, if you want to stop enter \"STOP\"\n",437      "12\n",438      "Please enter a integer, if you want to stop enter \"STOP\"\n",439      "45\n",440      "Please enter a integer, if you want to stop enter \"STOP\"\n",441      "STOP\n"442     ]443    }444   ],445   "source": [446    "li =[]\n",447    "ip = ''\n",448    "while ip != 'STOP':\n",449    "    ip = input('Please enter a integer, if you want to stop enter \"STOP\"\\n')\n",450    "    if ip.isdigit(): \n",451    "        li.append(int(ip))"452   ]453  },454  {455   "cell_type": "code",456   "execution_count": 98,457   "id": "f9f0f68a",458   "metadata": {},459   "outputs": [460    {461     "name": "stdout",462     "output_type": "stream",463     "text": [464      "Please enter a integer, if you want to stop enter \"STOP\"\n",465      "1\n",466      "Please enter a integer, if you want to stop enter \"STOP\"\n",467      "2\n",468      "Please enter a integer, if you want to stop enter \"STOP\"\n",469      "jghfdjh\n",470      "Your input is not an integer or STOP\n",471      "Please enter a integer, if you want to stop enter \"STOP\"\n",472      "STOP\n",473      "Your input is not an integer or STOP\n"474     ]475    }476   ],477   "source": [478    "li =[]\n",479    "ip = ''\n",480    "while ip != 'STOP':\n",481    "    ip = input('Please enter a integer, if you want to stop enter \"STOP\"\\n')\n",482    "    if ip.isdigit(): \n",483    "        li.append(int(ip))\n",484    "    else:\n",485    "        print('Your input is not an integer or STOP')"486   ]487  },488  {489   "cell_type": "code",490   "execution_count": 107,491   "id": "ca965a88",492   "metadata": {},493   "outputs": [494    {495     "name": "stdout",496     "output_type": "stream",497     "text": [498      "Please enter a integer, if you want to stop enter \"STOP\"\n",499      "1.5\n",500      "Your input is not an integer or STOP\n",501      "Please enter a integer, if you want to stop enter \"STOP\"\n",502      "1.5\n",503      "Your input is not an integer or STOP\n",504      "Please enter a integer, if you want to stop enter \"STOP\"\n",505      "STOP\n",506      "Your input is not an integer or STOP\n"507     ]508    },509    {510     "ename": "ZeroDivisionError",511     "evalue": "division by zero",512     "output_type": "error",513     "traceback": [514      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",515      "\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",516      "Input \u001b[1;32mIn [107]\u001b[0m, in \u001b[0;36m<cell line: 12>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m     10\u001b[0m         \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mYour input is not an integer or STOP\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m---> 12\u001b[0m average \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43msum\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mli\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mli\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     14\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mAverage of numbers entered is \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m+\u001b[39m\u001b[38;5;28mstr\u001b[39m(average))\n",517      "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"518     ]519    }520   ],521   "source": [522    "### WRITE A PROGRAM TO TAKE ARBITRARY NUMBER OF INPUTS FROM THE USER AND PRINT THEIR AVERAGE  \n",523    "\n",524    "li =[]\n",525    "ip = ''\n",526    "while ip != 'STOP':\n",527    "    ip = input('Please enter a integer, if you want to stop enter \"STOP\"\\n')\n",528    "    if ip.isdecimal(): \n",529    "        li.append(float(ip))\n",530    "    else:\n",531    "        print('Your input is not an integer or STOP')\n",532    "\n",533    "average = sum(li)/len(li)\n",534    "\n",535    "print('\\nAverage of numbers entered is '+str(average))"536   ]537  },538  {539   "cell_type": "code",540   "execution_count": 104,541   "id": "c0f8b835",542   "metadata": {},543   "outputs": [544    {545     "data": {546      "text/plain": [547       "[]"548      ]549     },550     "execution_count": 104,551     "metadata": {},552     "output_type": "execute_result"553    }554   ],555   "source": [556    "li "557   ]558  },559  {560   "cell_type": "code",561   "execution_count": 135,562   "id": "88f75187",563   "metadata": {},564   "outputs": [],565   "source": [566    "res_nums = '31.64'.split('.')\n",567    "isdig = [i.isdigit() for i in res_nums]"568   ]569  },570  {571   "cell_type": "code",572   "execution_count": 136,573   "id": "9c9498b7",574   "metadata": {},575   "outputs": [576    {577     "data": {578      "text/plain": [579       "2"580      ]581     },582     "execution_count": 136,583     "metadata": {},584     "output_type": "execute_result"585    }586   ],587   "source": [588    "sum(isdig)"589   ]590  },591  {592   "cell_type": "code",593   "execution_count": 139,594   "id": "4914ac57",595   "metadata": {},596   "outputs": [597    {598     "name": "stdout",599     "output_type": "stream",600     "text": [601      "Please enter a integer, if you want to stop enter \"STOP\"\n",602      "1.2\n",603      "Please enter a integer, if you want to stop enter \"STOP\"\n",604      "1.2\n",605      "Please enter a integer, if you want to stop enter \"STOP\"\n",606      "STOP\n",607      "Your input is not an integer or STOP\n",608      "\n",609      "Average of numbers entered is 1.2\n"610     ]611    }612   ],613   "source": [614    "li =[]\n",615    "ip = ''\n",616    "while ip != 'STOP':\n",617    "    ip = input('Please enter a integer, if you want to stop enter \"STOP\"\\n')\n",618    "    \n",619    "    res_nums=ip.split('.')\n",620    "    isdig = [i.isdigit() for i in res_nums]\n",621    "    \n",622    "    if len(res_nums)<=2 and sum(isdig)==2: \n",623    "        li.append(float(ip))\n",624    "    else:\n",625    "        print('Your input is not an integer or STOP')\n",626    "\n",627    "average = sum(li)/len(li)\n",628    "\n",629    "print('\\nAverage of numbers entered is '+str(average))"630   ]631  },632  {633   "cell_type": "code",634   "execution_count": null,635   "id": "66cacd3b",636   "metadata": {},637   "outputs": [],638   "source": []639  }640 ],641 "metadata": {642  "kernelspec": {643   "display_name": "Python 3 (ipykernel)",644   "language": "python",645   "name": "python3"646  },647  "language_info": {648   "codemirror_mode": {649    "name": "ipython",650    "version": 3651   },652   "file_extension": ".py",653   "mimetype": "text/x-python",654   "name": "python",655   "nbconvert_exporter": "python",656   "pygments_lexer": "ipython3",657   "version": "3.9.12"658  }659 },660 "nbformat": 4,661 "nbformat_minor": 5662}663