{"cells":[{"cell_type":"markdown","metadata":{},"source":["### 1.Write a python program to sum of the first n positive integers"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":9465,"status":"ok","timestamp":1663342391462,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":-330},"id":"lHU25EyQ7gdw","outputId":"67afaad2-9585-4816-9fa9-fc0ebbce17d9"},"outputs":[{"name":"stdout","output_type":"stream","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":"markdown","metadata":{},"source":["### 2.Write a Python program to count occurrences of a substring in a string."]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":7,"status":"ok","timestamp":1663342476650,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":-330},"id":"qsuwfp3J7lqU","outputId":"07658517-4e32-4f71-e003-ef14ab395fff"},"outputs":[{"name":"stdout","output_type":"stream","text":["\n","1\n","\n"]}],"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()"]},{"cell_type":"markdown","metadata":{},"source":["### 3.Write a Python program to count the occurrences of each word in a given sentence."]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":10,"status":"ok","timestamp":1663342551166,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":-330},"id":"gro5r5Vx7_Tg","outputId":"e577f46f-b0b5-4366-840d-204913f62fdb"},"outputs":[{"name":"stdout","output_type":"stream","text":["{'hello,': 1, 'i': 1, 'am': 1, 'srushti': 1}\n"]}],"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'))"]},{"cell_type":"markdown","metadata":{},"source":["### 4.Write a Python program to get a single string from two given strings, separated by a space and swap the first two characters of each string"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":7,"status":"ok","timestamp":1663342807220,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":-330},"id":"B9SVcC5e8RrR","outputId":"98c18526-f1a5-4419-a36b-4839f729057d"},"outputs":[{"name":"stdout","output_type":"stream","text":["xyc abz\n"]}],"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'))"]},{"cell_type":"markdown","metadata":{},"source":["### 5. Write a Python program to add 'ing' at the end of a given string (length should be at least 3)If the given string already ends with 'ing' then add 'ly' instead If the string length of the given string is less than 3, leave it unchanged"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":32771,"status":"ok","timestamp":1663343028156,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":-330},"id":"DYwuQM4f9P8U","outputId":"35d437dc-d4da-4d9c-db23-5a633f32a7c5"},"outputs":[{"name":"stdout","output_type":"stream","text":["being\n","beingly\n"]}],"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') "]},{"cell_type":"markdown","metadata":{},"source":["### 6.Write a Python program to find the first appearance of the substring 'not' and 'poor' from a given string, if'not' follows the 'poor', replace the whole 'not'...'poor' substring with 'good'Return the resulting string"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":15256,"status":"ok","timestamp":1663343194531,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":-330},"id":"5gOYoP1o9-HS","outputId":"eccf2442-e271-4a32-84c7-20ae920d93f8"},"outputs":[{"name":"stdout","output_type":"stream","text":["enter a string:her work is not so poor\n","her work is good\n"]}],"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"]},{"cell_type":"markdown","metadata":{},"source":["### 7.Program to find Greatest Common Divisor of two numbers.For example, the GCD of 20 and 28 is 4 and GCD of 98 and 56 is 14."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"DWNKRJjC-rL0"},"outputs":[],"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()) "]},{"cell_type":"markdown","metadata":{},"source":["### 8.Write a Python program to check whether a list contains a sublist.\n"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":434,"status":"ok","timestamp":1663509554713,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":420},"id":"-gOJUoLxAzTF","outputId":"0f533e6e-3d1e-4b6b-b0c8-b87dfffcf2fa"},"outputs":[{"name":"stdout","output_type":"stream","text":["the original list : [5, 6, 3, 8, 2, 1, 7, 1]\n","Is sublist present in list ? : True\n"]}],"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))"]},{"cell_type":"markdown","metadata":{},"source":["### 9.Write a Python program to find the second smallest number in a list."]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":3,"status":"ok","timestamp":1663344112396,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":-330},"id":"MYvugwNFCG48","outputId":"f8e74050-5e17-4763-95c3-1b89ca85e9d1"},"outputs":[{"name":"stdout","output_type":"stream","text":["second smallest number is: 4\n"]}],"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"]},{"cell_type":"markdown","metadata":{},"source":["### 10.Write a Python program to get unique values from a list."]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":619,"status":"ok","timestamp":1663344805250,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":-330},"id":"9fUNj2yDCO0e","outputId":"a607b40f-5d45-4073-cdf2-4d69b2c0d311"},"outputs":[{"name":"stdout","output_type":"stream","text":["[1, 3, 5, 7, 9]\n"]}],"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"]},{"cell_type":"markdown","metadata":{},"source":["### 11.Write a Python program to unzip a list of tuples into individual lists."]},{"cell_type":"code","execution_count":2,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":395,"status":"ok","timestamp":1663345021364,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":-330},"id":"Cgr3S4QxEx3i","outputId":"e0700137-0f7b-4531-95fe-a9328610b647"},"outputs":[{"name":"stdout","output_type":"stream","text":["[(5, 7, 9, 1), (6, 5, 3, 5)]\n"]}],"source":["#Write a Python program to unzip a list of tuples into individual lists.\n","l1 = [(5,6),(7,5),(9,3),(1,5)]\n","print(list(zip(*l1)))\n"," "]},{"cell_type":"markdown","metadata":{},"source":["### 12.Write a Python program to convert a list of tuples into a dictionary"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":5,"status":"ok","timestamp":1663447170514,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":420},"id":"ng2uoFQxFl2l","outputId":"0c24cb79-542a-495c-cb10-4d9290f5bc88"},"outputs":[{"name":"stdout","output_type":"stream","text":["{'a': [3]}\n","{'a': [3, 5]}\n","{'a': [3, 5], 'b': [8]}\n","{'a': [3, 5], 'b': [8, 7]}\n"]}],"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)"]},{"cell_type":"markdown","metadata":{},"source":["### 13.Write a Python program to sort a dictionary (ascending /descending) by value."]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":40,"status":"ok","timestamp":1663510961224,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":420},"id":"p38wgp0KLSRD","outputId":"20865684-2391-47a0-8442-45c4a348bb34"},"outputs":[{"name":"stdout","output_type":"stream","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"]}],"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)"]},{"cell_type":"markdown","metadata":{},"source":["### 14.Write a Python program to find the highest 3 values in a dictionary."]},{"cell_type":"code","execution_count":2,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":12,"status":"ok","timestamp":1663520325042,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":420},"id":"PJ9BkAIK-ss6","outputId":"921a38b4-e587-4db7-f2b2-63603b631b09"},"outputs":[{"name":"stdout","output_type":"stream","text":["D\n","C\n","B\n"]}],"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])"]},{"cell_type":"markdown","metadata":{},"source":["### 14.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"]},{"cell_type":"code","execution_count":7,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":5009,"status":"ok","timestamp":1663521465338,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":420},"id":"DXRmqrYIisib","outputId":"be99b1cc-3f0c-43e7-e7d8-1e07a8cda638"},"outputs":[{"name":"stdout","output_type":"stream","text":["enter the number :7\n","0 1 1 2 3 5 8 "]}],"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"]},{"cell_type":"markdown","metadata":{},"source":["### 15.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"]},{"cell_type":"code","execution_count":2,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":235},"executionInfo":{"elapsed":422,"status":"error","timestamp":1663522426318,"user":{"displayName":"Srushti Patel","userId":"04188926097343651666"},"user_tz":420},"id":"yvVdhjJLo8dM","outputId":"82735fc0-28d8-4d77-df52-2cb90d987759"},"outputs":[{"name":"stdout","output_type":"stream","text":[" 1 : 5\n"," 5 : 2\n"," 3 : 3\n"," 4 : 3\n"," 2 : 4\n"]}],"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","def CountFrequency(my_list):\n"," \n"," # Creating an empty dictionary\n"," freq = {}\n"," for items in my_list:\n"," freq[items] = my_list.count(items)\n"," \n"," for key, value in freq.items():\n"," print (\"% d : % d\"%(key, value))\n"," \n","# Driver function\n","if __name__ == \"__main__\":\n"," my_list =[1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2]\n"," CountFrequency(my_list)"]},{"cell_type":"markdown","metadata":{},"source":["### 16.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"]},{"cell_type":"code","execution_count":12,"metadata":{"id":"EzHKhHNQs9tI"},"outputs":[{"name":"stdout","output_type":"stream","text":["the sum of odd numbers 9\n","sum of even numbers 10\n"]}],"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","\n","odd_series = [1,3,5,7,9]\n","even_series = [2,4,6,8,10]\n","odd_sum =0\n","even_sum =0\n","for i in odd_series:\n"," sum =odd_sum +i \n","print(\"the sum of odd numbers\",sum)\n"," \n","for i in even_series:\n"," sum =even_sum+i\n","print(\"sum of even numbers\",sum)"]},{"cell_type":"markdown","metadata":{},"source":["### 17.Python Program to Find Factorial of Number Using Recursion"]},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["The factorial of 7 is 5040\n"]}],"source":["# Factorial of a number using recursion\n","\n","def recur_factorial(n):\n"," if n == 1:\n"," return n\n"," else:\n"," return n*recur_factorial(n-1)\n","\n","num = 7\n","\n","print(\"The factorial of\", num, \"is\", recur_factorial(num))"]},{"cell_type":"markdown","metadata":{},"source":["### 18.Write a Python function that takes a list and returns a new list with unique elements of the first list"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[1, 2, 3, 4, 5]\n"]}],"source":["def unique_list (l):\n","\n"," x=[]\n"," for a in l:\n"," if a not in x:\n"," x.append(a)\n"," return x\n"," \n","print(unique_list([1,2,3,1,2,3,4,5]))"]},{"cell_type":"markdown","metadata":{},"source":["## - password generator"]},{"cell_type":"markdown","metadata":{},"source":["## - Mini project : \n","Problem Statement : Password Generator \n","Make a program to generate a strong password using the input given by the user. To generate a password, \n","randomly take some words from the user input and then include numbers, special characters and capital \n","letters to generate the password. Also, keep a check that password length is more than 8 characters. \n","Note: Include Exception handling wherever required. Also, make a ‘User’ class and store the details like user \n","id, name and password of each user as a tuple"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["('srushti', '12345', '4A,^=%>C*3')\n"]}],"source":["#import random\n","import random\n","\n","#input characters\n","az = input('enter chracters')\n","\n","#import string\n","import string\n","\n","#including data\n","upper=string.ascii_uppercase #uppercase characters\n","num =string.digits #numbers\n","symbols =string.punctuation #symbols\n","\n","#adding data\n","add =upper+num+symbols\n","\n","#collect data\n","user =list()\n","name= input('enter your name: ')\n","user_id = input('enter your id:')\n","password =''.join(random.choices(add,k=10))\n","user.extend([name,user_id,password])\n","print(tuple(user))\n"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":[]}],"metadata":{"colab":{"authorship_tag":"ABX9TyPy+mUUUYhVbMHLcL8Et0t6","provenance":[]},"kernelspec":{"display_name":"Python 3.9.12 ('base')","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.12"},"vscode":{"interpreter":{"hash":"daee99e6893c0f9b4fa2cee7c11289a4ac1788238c3c1d2caa83921531d37465"}}},"nbformat":4,"nbformat_minor":0}