{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyN6IZGLCHd02xg2pwjuUNRN",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"
"
]
},
{
"cell_type": "markdown",
"source": [
"#Python practice\n"
],
"metadata": {
"id": "5ltHj5TjGh9A"
}
},
{
"cell_type": "markdown",
"source": [
"Write a program that contains a function that has one parameter, n, representing an integer\n",
"greater than 0. The function should return n! (n factorial). Then write a main function that calls this\n",
"function with the values 1 through 20, one at a time, printing the returned results. This is what your\n",
"output should look like:\n",
"\n",
"1 1\n",
"\n",
"2 2\n",
"\n",
"3 6\n",
"\n",
"4 24\n",
"\n",
"5 120\n",
"\n",
"6 720\n",
"\n",
"7 5040\n",
"\n",
"8 40320\n",
"\n",
"9 362880"
],
"metadata": {
"id": "pEyv9xJmJdU2"
}
},
{
"cell_type": "code",
"source": [
"def fact(n):\n",
" f = 1\n",
" for i in range(1,n+1):\n",
" f = f*i\n",
" return f\n",
"\n",
"if __name__==\"__main__\":\n",
" for i in range(1,21):\n",
" print(i,fact(i))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QWU14zmJGmZQ",
"outputId": "31dc0898-6412-4198-afc7-92f6cf5729a6"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1 1\n",
"2 2\n",
"3 6\n",
"4 24\n",
"5 120\n",
"6 720\n",
"7 5040\n",
"8 40320\n",
"9 362880\n",
"10 3628800\n",
"11 39916800\n",
"12 479001600\n",
"13 6227020800\n",
"14 87178291200\n",
"15 1307674368000\n",
"16 20922789888000\n",
"17 355687428096000\n",
"18 6402373705728000\n",
"19 121645100408832000\n",
"20 2432902008176640000\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"id": "3ZtRsQM30_PF"
}
},
{
"cell_type": "markdown",
"source": [
"Python Program to get Maximum product of elements of list in a 2D list\n",
"Input : [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
"Output : 504\n",
"Explanation:1*2*3 = 6, 4*5*6 = 120, 7*8*9 = 504\n",
"Since maximum is 504.\n"
],
"metadata": {
"id": "XTAzgqMQJSeH"
}
},
{
"cell_type": "code",
"source": [
"def getmax(l):\n",
" sublst = []\n",
" result = []\n",
" for i in range(0, len(l)):\n",
" sublst = l[i]\n",
" #print(sublst)\n",
" p=1\n",
" for j in sublst:\n",
" p = p*j\n",
" result.append(p)\n",
" #print(result)\n",
" return max(result)\n",
"\n",
"if __name__==\"__main__\":\n",
" lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
" print(getmax(lst))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "EJr_h9g9JXle",
"outputId": "99e36567-6772-4039-e03a-e5353e625b96"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"504\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Sum of number digits in List\n",
"The original list is : [12, 67, 98, 34]\n",
"List Integer Summation : [3, 13, 17, 7]\n"
],
"metadata": {
"id": "LI5BVO7u7Nqp"
}
},
{
"cell_type": "code",
"source": [
"l=[12,67,98,34]\n",
"new_lst=[]\n",
"for i in l:\n",
" sum = 0\n",
" while i!=0:\n",
" sum = sum + i%10\n",
" i=i//10 \n",
" new_lst.append(sum)\n",
"print(new_lst)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OTlUTshx0dOD",
"outputId": "bdb751ab-832d-4b43-c9a4-f90bd11cb8ae"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[3, 13, 17, 7]\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "GRsJ4J0RH1e3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Program to print duplicates from a list of integers\n",
"\n",
"Input : list = [-1, 1, -1, 8]\n",
"Output : output_list = [-1]\n",
"\n",
"Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]\n",
"Output : output_list = [20, 30, -20, 60]\n"
],
"metadata": {
"id": "mTDHQ9--HYGy"
}
},
{
"cell_type": "code",
"source": [
"p = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] \n",
"s=[]\n",
"for i in p:\n",
" if p.count(i)>1:\n",
" s.append(i)\n",
"\n",
"e=list(set(s))\n",
"print(e)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "e4epIrxLGDeL",
"outputId": "07fc3b1f-2ee7-4295-8de0-2807baa12edf"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[20, -20, 30, 60]\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"------------------------------------------------------------------\n",
"Uncommon elements in Lists of List\n",
"The original list 1 : [[1, 2], [3, 4], [5, 6]]\n",
"The original list 2 : [[3, 4], [5, 7], [1, 2]]\n",
"The uncommon of two lists is : [[5, 6], [5, 7]]\n",
"\n",
"------------------------------------------------------------------"
],
"metadata": {
"id": "7PJ-xdJtKqmB"
}
},
{
"cell_type": "code",
"source": [
"l1 = [[1,2], [3,4], [5,6]]\n",
"l2 = [[3,4], [5,7], [1,2]]\n",
"l3 = []\n",
"l4 = []\n",
"\n",
"for i in l1:\n",
" if i in l2:\n",
" l3.append(i)\n",
" elif i not in l2:\n",
" l4.append(i)\n",
"\n",
"for i in l2:\n",
" if i in l1:\n",
" l3.append(i)\n",
" elif i not in l1:\n",
" l4.append(i)\n",
"\n",
"\n",
"print(l4)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "nP8bQbMsF2Ft",
"outputId": "329c9bc4-b97e-46f0-860f-2d9089e0acd8"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[5, 6], [5, 7]]\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8g_mWTywSwYz",
"outputId": "81464a27-1cdf-47b4-d547-5b4b711df6df"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"findall: ['o', 'o', 'o', 'o']\n",
"search: \n",
"split: ['The quick br', 'wn f', 'x jumps ', 'ver the lazy d', 'g.']\n",
"sub: The quick broown foox jumps oover the lazy doog.\n",
"\n",
"\n",
"\n",
"\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"n=8888218321\n",
"sum = 0\n",
"\n",
"while n!=0:\n",
" temp=n%10\n",
" sum = sum + temp\n",
" n=n//10\n",
" \n",
"print(sum)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "X1zySqqM0Xkz",
"outputId": "d35ad5fc-8496-4d30-a152-d7fe07ac189a"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"49\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"\n",
"a = np.random.randint(1,100,20)\n",
"print(\"input numpy array: \", a)\n",
"\n",
"\n",
"def primeno(n):\n",
" flag = False\n",
" for i in range(2,n):\n",
" if (n % i) == 0:\n",
" # if factor is found, set flag to True\n",
" flag = True\n",
" # break out of loop\n",
" break\n",
" if not flag:\n",
" return n\n",
"\n",
"lst = []\n",
"for i in a:\n",
" lst.append(primeno(i))\n",
"new_list = [i for i in lst if i is not None]\n",
"#print(new_list)\n",
"a1 = np.array(new_list)\n",
"print(\"output prime array: \", a1)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jOlKDNH2rfIB",
"outputId": "4e613ce3-c219-4507-d880-af51a6a8ea18"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"input numpy array: [ 7 50 10 56 99 21 90 9 91 60 57 35 51 69 18 9 24 6 35 57]\n",
"output prime array: [7]\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Reverse Row sort in Lists of List The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]] The reverse sorted Matrix is : [[6, 4, 1], [8, 7], [10, 8, 4]]"
],
"metadata": {
"id": "LEMUZgUCfc4r"
}
},
{
"cell_type": "code",
"source": [
"l = [[4, 1, 6], [7, 8], [4, 10, 8]]\n",
"for i in range(len(l)):\n",
" l[i].sort(reverse=True)\n",
"print(l)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Wgrjhy7JfV5g",
"outputId": "171d055c-366a-4e03-cabd-c3dd683846f3"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[6, 4, 1], [8, 7], [10, 8, 4]]\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"\n",
"Python program to find the sum of a Sublist\n",
"The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]\n",
"result is [11,15,22]\n"
],
"metadata": {
"id": "4fi3g7_r6KTb"
}
},
{
"cell_type": "code",
"source": [
"l = [[4, 1, 6], [7, 8], [4, 10, 8]]\n",
"newlst = []\n",
"for i in range(len(l)):\n",
" temp = sum(l[i])\n",
" newlst.append(temp)\n",
"\n",
" \n",
"print(newlst)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PsyMD5ko6Lbi",
"outputId": "28ac6375-dcd9-4a4d-cf8f-6349899cda03"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[11, 15, 22]\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"------------------------------------------------------------------\n",
"Python Program to Square Each Odd Number in a List using List Comprehension\n",
"data=[1,2,3,4,5,6,7]\n",
"[1, 9, 25, 49] "
],
"metadata": {
"id": "IYeLDtig62RJ"
}
},
{
"cell_type": "code",
"source": [
"l= [1,2,3,4,5,6,7]\n",
"lst = [ x**2 for x in l if x%2==1]\n",
"print(lst)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PNzaN2DP64Ug",
"outputId": "9bf072c7-9d22-4316-cf05-95e2d88555d8"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[1, 9, 25, 49]\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"\n",
"---------------------------------------------------------------\n",
"Python program to print even length words in a string\n",
"Input: s = \"This is a python language\"\n",
"Output: This is python language"
],
"metadata": {
"id": "M0mD0xvj71R_"
}
},
{
"cell_type": "code",
"source": [
"s = \"This is a python language\" \n",
"s1 = s.split(\" \")\n",
"s2 = [x for x in s1 if len(x)%2==0]\n",
"f = \" \".join(s2)\n",
"print(f)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "gq-v_Bo-73X9",
"outputId": "2a0e7bb8-30e7-4c10-9465-9661e61f332a"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"This is python language\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Python program to capitalize the first and last character of each word in a string\n",
"Given the string, the task is to capitalise the first and last character of each word in a string. Examples:\n",
"\n",
"Input: hello world \n",
"Output: HellO WorlD"
],
"metadata": {
"id": "B3-sqYad9hIQ"
}
},
{
"cell_type": "code",
"source": [
"string = \"hello world\"\n",
"\n",
"#method 1\n",
"s1 = string.split(\" \")\n",
"lst = []\n",
"one = []\n",
"for i in range(0,len(s1)):\n",
" lst.append(s1[i][1:-1])\n",
" temp = s1[i][0].upper()+lst[i]+s1[i][-1].upper()\n",
" one.append(temp)\n",
"\n",
"f = \" \".join(one)\n",
"print(f)\n",
"\n",
"#method 2\n",
"print(string[0:][0].upper()+string[1:4]+string[4].upper()+\" \"+string[6].upper()+string[7:-1]+string[-1].upper())"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "0FJ7Stcc9jNn",
"outputId": "97345e11-c325-48ac-b5a4-56f9e78a83ee"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"HellO WorlD\n",
"HellO WorlD\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import re\n",
"s = \"The quick brown fox jumps over the lazy dog.\"\n",
"\n",
"print(\"findall: \",re.findall('o',s))\n",
"print(\"search: \",re.search('o',s))\n",
"print(\"split: \",re.split('o',s))\n",
"print(\"sub: \",re.sub('o',\"oo\",s))\n",
"\n",
"for i in re.finditer('o',s):\n",
" print(i)\n",
"\n",
"\n",
"pat = re.compile(r'o')"
],
"metadata": {
"id": "1fsF6mo-wOIA",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "4f017b22-b5af-4049-8f84-f38174d3ace7"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"findall: ['o', 'o', 'o', 'o']\n",
"search: \n",
"split: ['The quick br', 'wn f', 'x jumps ', 'ver the lazy d', 'g.']\n",
"sub: The quick broown foox jumps oover the lazy doog.\n",
"\n",
"\n",
"\n",
"\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "CxPKn4W8fSoC"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Test question\n",
"1. ****1234 card number\n",
"2. factorial of dictionaries\n",
"3. prime number numpy array and prime tuples of that array\n",
"4. 4 and 6 digits- valid / Invalid"
],
"metadata": {
"id": "Oi5VvGmE1A3S"
}
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"id": "RMemFt2s1qXL"
}
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')\n",
"path = \"copied path\"\n",
"df_bonus = pd.read_csv(path)"
],
"metadata": {
"id": "O41-Ooloxu90"
},
"execution_count": null,
"outputs": []
}
]
}