Skip to content

Commit 5cbcb25

Browse files
committed
python assignments
0 parents  commit 5cbcb25

1 file changed

Lines changed: 1 addition & 0 deletions

File tree

Module-1-Python.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyPy+mUUUYhVbMHLcL8Et0t6"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"lHU25EyQ7gdw","executionInfo":{"status":"ok","timestamp":1663342391462,"user_tz":-330,"elapsed":9465,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"67afaad2-9585-4816-9fa9-fc0ebbce17d9"},"outputs":[{"output_type":"stream","name":"stdout","text":["enter a number: 1\n","1.0\n"]}],"source":["#Write a python program to sum of the first n positive integers.\n","n = int(input(\"enter a number: \"))\n","sum = (n * (n + 1)) / 2\n","print(sum)"]},{"cell_type":"code","source":["#Write a Python program to count occurrences of a substring in a string.\n","str1 = ' welcome to the world of python.'\n","print()\n","print(str1.count(\"world\"))\n","print()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"qsuwfp3J7lqU","executionInfo":{"status":"ok","timestamp":1663342476650,"user_tz":-330,"elapsed":7,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"07658517-4e32-4f71-e003-ef14ab395fff"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["\n","1\n","\n"]}]},{"cell_type":"code","source":["#Write a Python program to count the occurrences of each word in a given sentence.\n","def word_count(str):\n"," counts = dict()\n"," words = str.split()\n","\n"," for word in words:\n"," if word in counts:\n"," counts[word] += 1\n"," else:\n"," counts[word] = 1\n"," return counts\n","\n","print( word_count('hello, i am srushti'))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"gro5r5Vx7_Tg","executionInfo":{"status":"ok","timestamp":1663342551166,"user_tz":-330,"elapsed":10,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"e577f46f-b0b5-4366-840d-204913f62fdb"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["{'hello,': 1, 'i': 1, 'am': 1, 'srushti': 1}\n"]}]},{"cell_type":"code","source":["#Write a Python program to get a single string from two given strings, separated by a space and swap the first\n","#two characters of each string.\n","def chars_mix_up(a, b):\n"," new_a = b[:2] + a[2:]\n"," new_b = a[:2] + b[2:]\n","\n"," return new_a + ' ' + new_b\n","print(chars_mix_up('abc', 'xyz'))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"B9SVcC5e8RrR","executionInfo":{"status":"ok","timestamp":1663342807220,"user_tz":-330,"elapsed":7,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"98c18526-f1a5-4419-a36b-4839f729057d"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["xyc abz\n"]}]},{"cell_type":"code","source":["#Write a Python program to add 'ing' at the end of a given string (length should be at least 3).\n","#If the given string already ends with 'ing' then add 'ly' instead If the string length of the given\n","#tring is less than 3, leave it unchanged\n","string = input()\n","if len(string)<3:\n"," print(string)\n","else :\n"," print(string + 'ly') "],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"DYwuQM4f9P8U","executionInfo":{"status":"ok","timestamp":1663343028156,"user_tz":-330,"elapsed":32771,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"35d437dc-d4da-4d9c-db23-5a633f32a7c5"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["being\n","beingly\n"]}]},{"cell_type":"code","source":["#Write a Python program to find the first appearance of the substring 'not' and 'poor' from a given string, if\n","#'not' follows the 'poor', replace the whole 'not'...'poor' substring with 'good'.\n","#Return the resulting string\n","a = input('enter a string:')\n","b = a\n","c=d=0\n","c=a.find('not')\n","d=a.find('poor')\n","if c>=0 and d >=0:\n"," b=b.replace(b[c:d+4],'good')\n"," print(b)\n","\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"5gOYoP1o9-HS","executionInfo":{"status":"ok","timestamp":1663343194531,"user_tz":-330,"elapsed":15256,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"eccf2442-e271-4a32-84c7-20ae920d93f8"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["enter a string:her work is not so poor\n","her work is good\n"]}]},{"cell_type":"code","source":["#Program to find Greatest Common Divisor of two numbers.\n","#For example, the GCD of 20 and 28 is 4 and GCD of 98 and 56 is 14.\n","def gcd(x,y):\n"," gcd=1\n"," if x % y ==0:\n"," return y\n"," for k in range(int(y/2),0,-1):\n"," if x % k ==0 and y % k ==0:\n"," gcd = k\n"," break\n"," return gcd\n"," print(\"gcd of 143 and 175 = \",gcd()) "],"metadata":{"id":"DWNKRJjC-rL0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["def find.gcd(a,b):\n"," if (b==0):\n"," return a\n"," else:\n"," return gcd(b,a%b)\n","\n"," num1=int(input(\"enter the number:\")) \n"," num2=int(input(\"enter the number:\"))\n","\n"," val=findgcd(num1,num2)\n"," print(\" the result of{0} and {1}={2}\".format(num1,num2,val)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":131},"id":"6TDlFkWWAl2m","executionInfo":{"status":"error","timestamp":1663343805903,"user_tz":-330,"elapsed":514,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"72e6e49c-a936-40c0-d906-15292e8b2a1e"},"execution_count":null,"outputs":[{"output_type":"error","ename":"IndentationError","evalue":"ignored","traceback":["\u001b[0;36m File \u001b[0;32m\"<tokenize>\"\u001b[0;36m, line \u001b[0;32m7\u001b[0m\n\u001b[0;31m num1=int(input(\"enter the number:\"))\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unindent does not match any outer indentation level\n"]}]},{"cell_type":"code","source":["#Write a Python program to check whether a list contains a sublist.\n","list1 = [5, 6, 3, 8, 2, 1, 7, 1]\n"," \n","print(\"the original list : \" + str(list1))\n"," \n","sublist = [8, 2, 1]\n"," \n","res = False\n","for idx in range(len(list1) - len(sublist) + 1):\n"," if list1[idx : idx + len(sublist)] == sublist:\n"," res = True\n"," break\n"," \n","print(\"Is sublist present in list ? : \" + str(res))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"-gOJUoLxAzTF","executionInfo":{"status":"ok","timestamp":1663509554713,"user_tz":420,"elapsed":434,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"0f533e6e-3d1e-4b6b-b0c8-b87dfffcf2fa"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["the original list : [5, 6, 3, 8, 2, 1, 7, 1]\n","Is sublist present in list ? : True\n"]}]},{"cell_type":"code","source":["#Write a Python program to find the second smallest number in a list.\n","l1 = [2,5,7,8,4]\n","l1.sort()\n","print(\"second smallest number is:\",l1[1])\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"MYvugwNFCG48","executionInfo":{"status":"ok","timestamp":1663344112396,"user_tz":-330,"elapsed":3,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"f8e74050-5e17-4763-95c3-1b89ca85e9d1"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["second smallest number is: 4\n"]}]},{"cell_type":"code","source":["#Write a Python program to get unique values from a list.\n","list=[ 1,3,5,7,9,3,7,3]\n","list1=[]\n","for a in list:\n"," if a not in list1:\n"," list1.append(a)\n","print(list1) \n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"9fUNj2yDCO0e","executionInfo":{"status":"ok","timestamp":1663344805250,"user_tz":-330,"elapsed":619,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"a607b40f-5d45-4073-cdf2-4d69b2c0d311"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["[1, 3, 5, 7, 9]\n"]}]},{"cell_type":"code","source":["#Write a Python program to unzip a list of tuples into individual lists.\n","list1 = [(5,6),(7,5),(9,3)]\n"," (zip(*list1))\n"," "],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Cgr3S4QxEx3i","executionInfo":{"status":"ok","timestamp":1663345021364,"user_tz":-330,"elapsed":395,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"e0700137-0f7b-4531-95fe-a9328610b647"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["<zip object at 0x7fdcddd85460>\n"]}]},{"cell_type":"code","source":["#Write a Python program to convert a list of tuples into a dictionary\n","l = [(\"a\",3),(\"a\",5),(\"b\",8),(\"b\",7)]\n","\n","d={}\n","\n","for a,b in l:\n"," d.setdefault(a,[]).append(b)\n"," print(d)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"ng2uoFQxFl2l","executionInfo":{"status":"ok","timestamp":1663447170514,"user_tz":420,"elapsed":5,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"0c24cb79-542a-495c-cb10-4d9290f5bc88"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["{'a': [3]}\n","{'a': [3, 5]}\n","{'a': [3, 5], 'b': [8]}\n","{'a': [3, 5], 'b': [8, 7]}\n"]}]},{"cell_type":"code","source":["#Write a Python program to sort a dictionary (ascending /descending) by value.\n","\n","d = {1:2,3:4,5:6,7:8}\n","print('original dictionary : ',d)\n","sorted_d = sorted(d.items())\n","print('dictionary in ascending order : ',sorted_d)\n","sorted_d_reverse = sorted(d.items(),reverse= True)\n","print('dictionary in decending order : ',sorted_d_reverse)"],"metadata":{"id":"p38wgp0KLSRD","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1663510961224,"user_tz":420,"elapsed":40,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"20865684-2391-47a0-8442-45c4a348bb34"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["original dictionary : {1: 2, 3: 4, 5: 6, 7: 8}\n","dictionary in ascending order : [(1, 2), (3, 4), (5, 6), (7, 8)]\n","dictionary in decending order : [(7, 8), (5, 6), (3, 4), (1, 2)]\n"]}]},{"cell_type":"code","source":["#Write a Python program to find the highest 3 values in a dictionary.\n","d ={'A':2,'B':6,'C':8,'D':9}\n","D= sorted(d,key=d.get,reverse=True)\n","for index in range(3):\n"," print(D[index])"],"metadata":{"id":"PJ9BkAIK-ss6","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1663520325042,"user_tz":420,"elapsed":12,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"921a38b4-e587-4db7-f2b2-63603b631b09"},"execution_count":2,"outputs":[{"output_type":"stream","name":"stdout","text":["D\n","C\n","B\n"]}]},{"cell_type":"code","source":["#Given a number n, write a python program to make and print the list of Fibonacci series up to n.\n","#Input : n=7\n","#Hint : first 7 numbers in the series\n","#Expected output :\n","#First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13\n","n = int(input(\"enter the number :\"))\n","f1,f2=0,1\n","if n==1:\n"," print(f1)\n","else:\n"," print(f1,f2,end=' ')\n"," for i in range(3,n+1):\n"," f3 = f1 + f2\n"," print(f3, end = ' ')\n"," f1 = f2\n"," f2 = f3\n","\n","\n","\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"DXRmqrYIisib","executionInfo":{"status":"ok","timestamp":1663521465338,"user_tz":420,"elapsed":5009,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"be99b1cc-3f0c-43e7-e7d8-1e07a8cda638"},"execution_count":7,"outputs":[{"output_type":"stream","name":"stdout","text":["enter the number :7\n","0 1 1 2 3 5 8 "]}]},{"cell_type":"code","source":["#Counting the frequencies in a list using a dictionary in Python.\n","#Input : [1, 1, 1, 5, 5, 3, 1, 3, 3, 1,4, 4, 4, 2, 2, 2, 2]\n","#Expected output : 1 : 5 , 2 : 4 , 3 : 3 , 4 : 3 , 5 : 2\n","\n","num = [1,1,1,5,5,3,1,3,3,1,4,4,4,2,2,2,2]\n","dict={}\n","for number in num:\n"," if num in dict:\n"," dict1[num]=dict[num]+1\n"," \n"," print(dict1) "],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":235},"id":"yvVdhjJLo8dM","executionInfo":{"status":"error","timestamp":1663522426318,"user_tz":420,"elapsed":422,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"82735fc0-28d8-4d77-df52-2cb90d987759"},"execution_count":13,"outputs":[{"output_type":"error","ename":"TypeError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)","\u001b[0;32m<ipython-input-13-606a5e956c34>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mdict\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mnum\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mnum\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdict\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 9\u001b[0m \u001b[0mdict1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdict\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"]}]},{"cell_type":"code","source":["list1=[1, 1, 1, 5, 5, 3, 1, 3, 3, 1,4, 4, 4, 2, 2, 2, 2]\n","\n","d={}\n"," for i in list1:\n"," if i in d:\n"," f[i]+=1\n"," else:\n"," f[i]=1\n","\n","print(d) "],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":131},"id":"QdL8efXBqmIZ","executionInfo":{"status":"error","timestamp":1663522840740,"user_tz":420,"elapsed":625,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"b96e4ae6-94c4-4d72-c0a3-7dd40e5624fb"},"execution_count":22,"outputs":[{"output_type":"error","ename":"IndentationError","evalue":"ignored","traceback":["\u001b[0;36m File \u001b[0;32m\"<ipython-input-22-347d81fde849>\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m for i in list1:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n"]}]},{"cell_type":"code","source":["list1=[1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2]\n","\n","for element in list1:\n"," freq=(element:list1.count(element))\n"," \n"," print(freq)\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":131},"id":"2B2NxysprajI","executionInfo":{"status":"error","timestamp":1663523202779,"user_tz":420,"elapsed":676,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"}},"outputId":"cba1b828-8d1b-43a6-a8e7-63ccfe49b4fb"},"execution_count":26,"outputs":[{"output_type":"error","ename":"SyntaxError","evalue":"ignored","traceback":["\u001b[0;36m File \u001b[0;32m\"<ipython-input-26-d6a37d48d799>\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m freq=(element:list1.count(element))\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"]}]},{"cell_type":"code","source":["#Write a python program using function to find the sum of odd series and even series\n","#Odd series: 12/ 1! + 32/ 3! + 52/ 5!+......n\n","#Even series: 22/ 2! + 42/ 4! + 62/ 6!+......n\n"],"metadata":{"id":"EzHKhHNQs9tI"},"execution_count":null,"outputs":[]}]}

0 commit comments

Comments
 (0)