{"id":26572,"date":"2023-05-27T16:20:12","date_gmt":"2023-05-27T10:50:12","guid":{"rendered":"https:\/\/tocxten.com\/?page_id=26572"},"modified":"2023-08-11T11:29:27","modified_gmt":"2023-08-11T05:59:27","slug":"python-lab-manual-part-i-2","status":"publish","type":"page","link":"https:\/\/tocxten.com\/index.php\/python-lab-manual-part-i-2\/","title":{"rendered":"PYTHON LAB MANUAL : PART I"},"content":{"rendered":"\n<h1 class=\"wp-block-heading has-small-font-size\"><strong>Dr. Thyagaraju G S&nbsp;<\/strong>and &nbsp;Ms.<strong>Palguni G T<\/strong><\/h1>\n\n\n\n<p><strong><u>Laboratory Program 1A: <\/u>&nbsp;<\/strong>Develop a program to read the student details like Name, USN, and Marks in three subjects. Display the student details, total marks and percentage with suitable messages.<\/p>\n\n\n\n<p><strong><u>Source Code:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def calculate_percentage(total_marks):\n    return (total_marks \/ (3*max_marks)) * 100\n\ndef display_results(name, usn, marks):\n    total_marks = sum(marks)\n    percentage = calculate_percentage(total_marks)\n\n    print(\"\\nStudent Details:\")\n    print(\"Name:\", name)\n    print(\"USN:\", usn)\n\n    print(\"\\nMarks:\")\n    print(\"Subject 1:\", marks&#91;0])\n    print(\"Subject 2:\", marks&#91;1])\n    print(\"Subject 3:\", marks&#91;2])\n\n    print(\"\\nTotal Marks:\", total_marks)\n    print(\"Percentage:\", percentage, \"%\")\n\n# Getting input from the user\nname = input(\"Enter student name: \")\nusn = input(\"Enter USN: \")\nmax_marks = int (input(\"Enter the max_marks (25\/50\/100\/Any max):\"))\nmarks = &#91;]\n\n# Reading marks for three subjects\nfor i in range(3):\n    subject_marks = float(input(\"Enter marks for subject {0} per {1}: \".format(i+1,max_marks)))\n    marks.append(subject_marks)\n\n# Displaying the results\ndisplay_results(name, usn, marks)<\/code><\/pre>\n\n\n\n<p><strong><u>Sample Output:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter student name: Palguni\nEnter USN: SDM12345\nEnter the max_marks (25\/50\/100\/Any max):25\nEnter marks for subject 1 per 25: 21\nEnter marks for subject 2 per 25: 22\nEnter marks for subject 3 per 25: 23\n\nStudent Details:\nName: Palguni\nUSN: SDM12345\n\nMarks:\nSubject 1: 21.0\nSubject 2: 22.0\nSubject 3: 23.0\n\nTotal Marks: 66.0\nPercentage: 88.0 %<\/code><\/pre>\n\n\n\n<p><strong><u>Laboratory Program 1B: <\/u>&nbsp;<\/strong>Develop a program to read the name and year of birth of a person. Display whether the person is a senior citizen or not.<\/p>\n\n\n\n<p><strong><u>Source Code:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def is_senior_citizen(year_of_birth):\n    current_year = 2023\n    age = current_year - year_of_birth\n    return age &gt;= 60\n\n# Getting input from the user\nname = input(\"Enter your name: \")\nyear_of_birth = int(input(\"Enter your year of birth: \"))\n\n# Checking if the person is a senior citizen\nif is_senior_citizen(year_of_birth):\n    print(name, \"is a senior citizen.\")\nelse:\n    print(name, \"is not a senior citizen.\")<\/code><\/pre>\n\n\n\n<p><strong><u>Sample Output:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter your name: Thyagaraju \nEnter your year of birth: 1975\nThyagaraju  is not a senior citizen.<\/code><\/pre>\n\n\n\n<p><strong><u>Laboratory Program 2A: <\/u>&nbsp;<\/strong>Develop a program to generate Fibonacci sequence of length (N). Read N from the console.<\/p>\n\n\n\n<p><strong><u>Source Code:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Program to display the Fibonacci sequence up to N\nN = int(input(\"Enter the length of the sequence \"))\n# first two terms\nt1, t2 = 0, 1\ncount = 1\n# check if the number of terms is valid\nif N &lt;= 0:\n   print(\"Please enter a positive integer\")\n# if there is only one term, return t1\nelif N == 1:\n   print(\"Fibonacci sequence upto \",N,\": \",end=' ')\n   print(n1)\n# generate fibonacci sequence\nelse:\n   print(\"Fibonacci sequence: \")\n   while count &lt;= N:\n       print(t1,end=' ')\n       nexterm = t1 + t2\n       # update values\n       t1 = t2\n       t2 = nexterm\n       count += 1<\/code><\/pre>\n\n\n\n<p><strong><u>Sample Output:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the length of the sequence 10\nFibonacci sequence: \n0 1 1 2 3 5 8 13 21 34 <\/code><\/pre>\n\n\n\n<p><strong><u>Laboratory Program 2B: <\/u>&nbsp;<\/strong>Write a function to calculate factorial of a number. Develop a program to compute binomial<br>coefficient (Given N and R).<\/p>\n\n\n\n<p><strong><u>Source Code:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># definition \ndef fact(N):\n    if N == 0:\n        return 1\n    res = 1\n    for i in range(2, N+1):\n        res = res * i\n    return res\n\ndef NCR(N, R):\n     return (fact(N)\/(fact(R)*fact(N - R)))\n\nprint(\"Please note N must be greater than R\")\nN=int(input(\"Enter the value of N: \"))\nR=int(input(\"Enter the value of R: \"))\nprint(\"The Binomial coefficient of {0}C{1} = {2} \".format(N,R,NCR(N,R)))<\/code><\/pre>\n\n\n\n<p><strong><u>Sample Output:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Please note N must be greater than R\nEnter the value of N: 7\nEnter the value of R: 3\nThe Binomial coefficient of 7C3 = 35.0 <\/code><\/pre>\n\n\n\n<p><strong><u>Laboratory Program 3: <\/u>&nbsp;<\/strong>Read N numbers from the console and create a list. Develop a program to print mean, variance and standard deviation with suitable messages.<\/p>\n\n\n\n<p><strong><u>Source Code:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import math\nlist_num = list()\nN = int(input((\"Enter the value of N: \")))\nprint(\"Enter {0} numbers\".format(N))\nfor i in range(1,N+1):\n    num = int(input())\n    list_num.append(num)\nprint(\"The entered list of numbers is : \",list_num)\n\n# Finding sum of numbers in list\nsum = 0\nfor num in list_num:\n    sum = sum + num\nprint(\"The sum of numbers in the list is : \",sum)\n\n# To find the mean of numbers in list\nmean = sum\/len(list_num)\nprint(\"Mean of numbers in the list is : \",mean)\n\n# To find variance of numbers in list \nsum_var = 0\nfor num in list_num:\n    sum_var = sum_var + (num -mean)**2\nvariance = sum_var\/N\nprint(\"Variance of numbers in the list is : \",variance)\n\n# To find the standard deviation of numbers in list\nstd = math.sqrt(variance)\nprint(\"The standard deviation of numbers in list is : \",std)<\/code><\/pre>\n\n\n\n<p><strong><u>Sample Output:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the value of N: 10\nEnter 10 numbers\n2\n3\n7\n4\n6\n5\n3\n8\n9\n7\nThe entered list of numbers is :  &#91;2, 3, 7, 4, 6, 5, 3, 8, 9, 7]\nThe sum of numbers in the list is :  54\nMean of numbers in the list is :  5.4\nVariance of numbers in the list is :  5.040000000000001\nThe standard deviation of numbers in list is :  2.244994432064365<\/code><\/pre>\n\n\n\n<p><strong><u>Laboratory Program 4: <\/u>&nbsp;<\/strong>Read a multi-digit number (as chars) from the console. Develop a program to print the frequency of each digit with suitable message.<\/p>\n\n\n\n<p><strong><u>Source Code:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>number=int(input(\"Enter any Multidigit Number : \"))\nprint(\"Digit\\tFrequency\")\nfor i in range(0,10):\n    count=0;\n    temp=number;\n    while temp&gt;0:\n        digit=temp%10\n        if digit==i:\n            count=count+1\n        temp=temp\/\/10;\n    if count&gt;0:\n        print(i,\"\\t\",count)<\/code><\/pre>\n\n\n\n<p><strong><u>Sample Output:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter any Multidigit Number : 223356789\nDigit\tFrequency\n2 \t 2\n3 \t 2\n5 \t 1\n6 \t 1\n7 \t 1\n8 \t 1\n9 \t 1<\/code><\/pre>\n\n\n\n<p><strong><u>Laboratory Program 5: <\/u>&nbsp;<\/strong>Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use dictionary with distinct words and their frequency of occurrences. Sort the dictionary in the reverse order of<br>frequency and display dictionary slice of first 10 items]<\/p>\n\n\n\n<p>.<\/p>\n\n\n\n<p><strong><u>Source Code:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from collections import OrderedDict\nimport numpy as np\nimport itertools\ntext = open(\"spamXY.txt\")\n\ndef sort_dict_by_value(d, reverse = False):\n    return dict(sorted(d.items(), key = lambda x: x&#91;1], reverse = reverse))\n\n# Create an empty dictionary\nd = dict()\n  \n# Loop through each line of the file\nfor line in text:\n    # Remove the leading spaces and newline character\n    line = line.strip()\n  \n    # Convert the characters in line to\n    # lowercase to avoid case mismatch\n    line = line.lower()\n  \n    # Split the line into words\n    words = line.split(\" \")\n    \n    # To eliminate  delimiters.\n    for word in words:\n        words.remove(word)\n        word = word.replace(\".\",\"\")\n        word = word.replace(\",\",\"\")\n        word = word.replace(\":\",\"\")\n        word = word.replace(\";\",\"\")\n        word = word.replace(\"!\",\"\")\n        word = word.replace(\"*\",\"\")\n        words.append(word)                    \n  \n    # Iterate over each word in line\n    for word in words:\n        # Check if the word is already in dictionary\n        if word in d:\n            # Increment count of word by 1\n            d&#91;word] = d&#91;word] + 1\n        else:\n            # Add the word to dictionary with count 1\n            d&#91;word] = 1\nprint(\"\\n Sorted dictionary elements by frequency &#91;Descending order]:\\n\")\nd1 =sort_dict_by_value(d, True)\nprint(d1)\nprint(\"\\n\")\n\nN= int(input(\"Enter the number of top frequency words to be displayed: \\n\"))\nprint(\"\\nThe {0} most frequently appearing words are : \\n\".format(N))\nout = dict(itertools.islice(d1.items(),N))\nfor i in out:\n    print(i)<\/code><\/pre>\n\n\n\n<p>Input file <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"259\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-1-1024x259.png\" alt=\"\" class=\"wp-image-26590\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-1-1024x259.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-1-300x76.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-1-768x194.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-1-1130x286.png 1130w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-1-760x192.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-1-600x152.png 600w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-1.png 1233w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong><u>Sample Output:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sorted dictionary elements by frequency &#91;Descending order]:\n\n{'quantum': 6, 'computer': 5, 'and': 5, 'a': 4, 'of': 3, 'the': 3, 'is': 2, 'physical': 2, 'classical': 2, 'could': 2, 'in': 2, 'exploits': 1, 'mechanical': 1, 'phenomena': 1, 'that': 1, 'small': 1, 'exhibits': 1, 'particles': 1, 'waves,': 1, 'scales': 1, 'properties': 1, 'matter': 1, 'at': 1, 'both': 1, 'leverages': 1, 'behavior': 1, 'specialized': 1, 'this': 1, 'hardware': 1, 'using': 1, 'computing': 1, 'physics': 1, 'explain': 1, 'operation': 1, 'these': 1, 'devices,': 1, 'cannot': 1, 'some': 1, 'exponentially': 1, 'than': 1, 'calculations': 1, 'any': 1, 'perform': 1, 'scalable': 1, 'faster': 1, 'modern': 1, 'particular': 1, 'break': 1, 'large-scale': 1, 'encryption': 1, 'physicists': 1, 'performing': 1, 'simulations;': 1, 'schemes': 1, 'widely-used': 1, 'aid': 1, 'state': 1, 'largely': 1, 'however': 1, 'still': 1, 'impractical': 1, 'art': 1, 'current': 1, 'experimental': 1}\n\nEnter the number of top frequency words to be displayed: \n10\n\nThe 10 most frequently appearing words are : \n\nquantum\ncomputer\nand\na\nof\nthe\nis\nphysical\nclassical\ncould<\/code><\/pre>\n\n\n\n<p><strong><u>Laboratory Program 6: <\/u>&nbsp;<\/strong>Develop a program to sort the contents of a text file and write the sorted contents into a separate text file. [Hint: Use string methods strip(), len(), list methods sort(), append(), and file methods open(),<br>readlines(), and write()].<\/p>\n\n\n\n<p><strong><u>Source Code:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>infile = open(\"spamXY.txt\", \"r\")\nwords = &#91;]\nfor line in infile:\n    line = line.strip()\n    line = line.lower()\n    temp = line.split()\n    for word in temp:\n        temp.remove(word)\n        word = word.replace(\".\",\"\")\n        word = word.replace(\",\",\"\")\n        word = word.replace(\":\",\"\")\n        word = word.replace(\";\",\"\")\n        word = word.replace(\"!\",\"\")\n        word = word.replace(\"*\",\"\")\n        word = word.replace(\" \",\"\")\n        temp.append(word)\n    for i in temp:\n        words.append(i)\ninfile.close()\nwords.sort()\nprint(\"Sorted words : \")\nprint(words)\n# writing the sorted words into result.txt\noutfile = open(\"result.txt\", \"w\")\nfor i in words:\n    outfile.write(i)\n    outfile.write(\"\\n\")\noutfile.close()<\/code><\/pre>\n\n\n\n<p><strong><u>Sample Output:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sorted words : \n&#91;'a', 'a', 'a', 'a', 'aid', 'and', 'and', 'and', 'and', 'and', 'any', 'art', 'at', 'behavior', 'both', 'break', 'calculations', 'cannot', 'classical', 'classical', 'computer', 'computer', 'computer', 'computer', 'computer', 'computing', 'could', 'could', 'current', 'devices,', 'encryption', 'exhibits', 'experimental', 'explain', 'exploits', 'exponentially', 'faster', 'hardware', 'however', 'impractical', 'in', 'in', 'is', 'is', 'large-scale', 'largely', 'leverages', 'matter', 'mechanical', 'modern', 'of', 'of', 'of', 'operation', 'particles', 'particular', 'perform', 'performing', 'phenomena', 'physical', 'physical', 'physicists', 'physics', 'properties', 'quantum', 'quantum', 'quantum', 'quantum', 'quantum', 'quantum', 'scalable', 'scales', 'schemes', 'simulations', 'small', 'some', 'specialized', 'state', 'still', 'than', 'that', 'the', 'the', 'the', 'these', 'this', 'using', 'waves', 'widely-used']<\/code><\/pre>\n\n\n\n<p>Output file :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-4.png\" alt=\"\" class=\"wp-image-26600\" style=\"width:392px;height:324px\" width=\"392\" height=\"324\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-4.png 708w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-4-300x248.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-4-600x497.png 600w\" sizes=\"auto, (max-width: 392px) 100vw, 392px\" \/><\/figure>\n\n\n\n<p><strong><u>Laboratory Program 7: <\/u>&nbsp;<\/strong>Develop a program to backing Up a given Folder (Folder in a current working directory) into a ZIP File by using relevant modules and suitable methods.<\/p>\n\n\n\n<p><strong><u>Source Code:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import zipfile, os\nfolder = input(\"Enter the folder name in the current working directory : \")\nfolder = os.path.abspath(folder) # make sure folder is absolute\nnumber = 1\nwhile True:\n    zipFilename = os.path.basename(folder) + '_' + str(number) + '.zip'\n    if not os.path.exists(zipFilename):\n        break\n    number = number + 1\n# Create the zip file.\nprint('Creating %s...' % (zipFilename))\nbackupZip = zipfile.ZipFile(zipFilename, 'w')\n# Walk the entire folder tree and compress the files in each folder.\nfor foldername, subfolders, filenames in os.walk(folder):\n    print('Adding files in %s...' % (foldername))\n    # Add the current folder to the ZIP file.\n    backupZip.write(foldername)\n    # Add all the files in this folder to the ZIP file.\n    for filename in filenames:\n        if filename.startswith(os.path.basename(folder) + '_') and filename.endswith('.zip'):\n            continue # don't backup the backup ZIP files\n        backupZip.write(os.path.join(foldername, filename))\nbackupZip.close()\nprint('Done.')<\/code><\/pre>\n\n\n\n<p><strong><u>Sample Output:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the folder name in the current working directory : cats\nCreating cats_1.zip...\nAdding files in C:\\Users\\thyagu\\18CS55\\cats...\nDone.<\/code><\/pre>\n\n\n\n<p><strong>Compressed file in a given directory&nbsp;<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"314\" height=\"258\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-5.png\" alt=\"\" class=\"wp-image-26603\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-5.png 314w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-5-300x246.png 300w\" sizes=\"auto, (max-width: 314px) 100vw, 314px\" \/><\/figure>\n\n\n\n<p><strong><u>Laboratory Program 8: <\/u>&nbsp;<\/strong>Write a function named DivExp which takes TWO parameters a, b and returns a value c (c=a\/b). Write suitable assertion for a&gt;0 in function DivExp and raise an exception for when b=0. Develop a suitable<br>program which reads two values from the console and calls a function DivExp.<\/p>\n\n\n\n<p><strong><u>Source Code:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def DivExp(a,b):\n    assert a&gt;0,\"a must be &gt; 0\"\n    if b == 0:  \n        raise ZeroDivisionError;  \n    else: c = a\/b\n    return c\n\na = int(input(\"Enter the value of a :\"))  \nb = int(input(\"Enter the value of b :\"))\nr = DivExp(a,b)\nprint(\"The value of {0}\/{1} = {2}\".format(a,b,r))<\/code><\/pre>\n\n\n\n<p><strong><u>Sample Output<\/u>1 :<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"544\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-6-1024x544.png\" alt=\"\" class=\"wp-image-26607\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-6-1024x544.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-6-300x159.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-6-768x408.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-6-1130x601.png 1130w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-6-760x404.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-6-600x319.png 600w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-6.png 1185w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the value of a :3\nEnter the value of b :2\nThe value of 3\/2 = 1.5<\/code><\/pre>\n\n\n\n<p>Sample Output 2 :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-7-1024x544.png\" alt=\"\" class=\"wp-image-26608\" style=\"width:516px;height:274px\" width=\"516\" height=\"274\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-7-1024x544.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-7-300x159.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-7-768x408.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-7-1130x601.png 1130w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-7-760x404.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-7-600x319.png 600w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-7.png 1185w\" sizes=\"auto, (max-width: 516px) 100vw, 516px\" \/><\/figure>\n\n\n\n<p><strong>Sample Output3: <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-8-1024x498.png\" alt=\"\" class=\"wp-image-26610\" style=\"width:639px;height:310px\" width=\"639\" height=\"310\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-8-1024x498.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-8-300x146.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-8-768x374.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-8-1130x550.png 1130w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-8-760x370.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-8-600x292.png 600w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/05\/image-8.png 1254w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<p><strong><u>Laboratory Program 9: <\/u>&nbsp;<\/strong>Define a function which takes TWO objects representing complex numbers and returns new complex number with a addition of two complex numbers. Define a suitable class \u2018Complex\u2019 to represent the<br>complex number. Develop a program to read N (N &gt;=2) complex numbers and to compute the addition<br>of N complex numbers<\/p>\n\n\n\n<p><strong><u>Source Code:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Function to add two complex numbers \ndef addComp(C1, C2): \n    # creating temporary variable\n    temp=Complex(0, 0)\n    # adding real part of complex numbers\n    temp.real = C1.real + C2.real;\n    # adding Imaginary part of complex numbers\n    temp.imaginary = C1.imaginary + C2.imaginary;\n    # returning the sum\n    return temp;\n\n# Class to represent a Complex Number\nclass Complex:\n    def __init__(self, tempReal, tempImaginary):  \n        self.real = tempReal;\n        self.imaginary = tempImaginary;\n\n# variable csum for Storing the sum of complex numbers\ncsum = Complex(0, 0) # inital value of csum set to 0,0\nn = int(input(\"Enter the value of n : \"))\nfor i in range(1,n+1):\n    realPart = int(input(\"Enter the Real Part of complex number {0}:\".format(i)))\n    imgPart  = int(input(\"Enter the Imaginary Part of complex number {0}:\".format(i)))  \n    c = Complex(realPart,imgPart)\n    # calling addComp() method\n    csum = addComp(csum,c);\n    # printing the sum\nprint(\"Sum of {0} complex numbers is :\".format(n))\nprint(csum.real, \"+i*\"+ str(csum.imaginary))<\/code><\/pre>\n\n\n\n<p><strong><u>Sample Output:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the value of n : 3\nEnter the Real Part of complex number 1:2\nEnter the Imaginary Part of complex number 1:3\nEnter the Real Part of complex number 2:4\nEnter the Imaginary Part of complex number 2:5\nEnter the Real Part of complex number 3:6\nEnter the Imaginary Part of complex number 3:7\nSum of 3 complex numbers is :\n12 +i*15<\/code><\/pre>\n\n\n\n<p><strong><u>Laboratory Program 10: <\/u>\u00a0<\/strong> Develop a program that uses class Student which prompts the user to enter marks in three subjects and calculates total marks, percentage and displays the score card details. [Hint: Use list to store the marks<br>in three subjects and total marks. Use <strong>init<\/strong>() method to initialize name, USN and the lists to store<br>marks and total, Use getMarks() method to read marks into the list, and display() method to display the<br>score card details.]<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Source Code <\/strong>: <strong>Approach1 (Single Object at a time)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student:\r\n    def __init__(self, name, usn, max_marks):\r\n        self.name = name\r\n        self.usn = usn\r\n        self.max_marks = max_marks\r\n        self.marks = &#91;]\r\n\r\n    def getMarks(self, m1, m2, m3):\r\n        self.marks.extend(&#91;m1, m2, m3])\r\n\r\n    def displayData(self):\r\n        print(\"Name is:\", self.name)\r\n        print(\"USN is:\", self.usn)\r\n        print(\"Marks are:\", self.marks)\r\n        print(\"Total Marks is:\", self.total())\r\n        print(\"Average Marks is:\", self.average())\r\n        print(\"Percentage Marks is:\", self.percentage())\r\n        print(\"\\n\")\r\n\r\n    def total(self):\r\n        return sum(self.marks)\r\n\r\n    def average(self):\r\n        return sum(self.marks) \/ len(self.marks)\r\n\r\n    def percentage(self):\r\n        return (sum(self.marks) \/ (self.max_marks * len(self.marks))) * 100\r\n\r\nwhile True:\r\n    name = input(\"Enter the name (or 'exit' to quit): \")\r\n    \r\n    if name.lower() == 'exit':\r\n        break\r\n    \r\n    usn = int(input(\"Enter the usn: \"))\r\n    max_marks = int(input(\"Enter the max_marks (25\/50\/100\/Any max): \"))\r\n    m1 = int(input(\"Enter the marks in the first subject out of {}: \".format(max_marks)))\r\n    m2 = int(input(\"Enter the marks in the second subject out of {}: \".format(max_marks)))\r\n    m3 = int(input(\"Enter the marks in the third subject out of {}: \".format(max_marks)))\r\n\r\n    student = Student(name, usn, max_marks)\r\n    student.getMarks(m1, m2, m3)\r\n    student.displayData()<\/code><\/pre>\n\n\n\n<p><strong><u>Sample Output:<\/u><\/strong> <strong>for Approach1<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the name (or 'exit' to quit): Rahul\r\nEnter the usn: 123\r\nEnter the max_marks (25\/50\/100\/Any max): 25\r\nEnter the marks in the first subject out of 25: 23\r\nEnter the marks in the second subject out of 25: 22\r\nEnter the marks in the third subject out of 25: 24\r\nName is: Rahul\r\nUSN is: 123\r\nMarks are: &#91;23, 22, 24]\r\nTotal Marks is: 69\r\nAverage Marks is: 23.0\r\nPercentage Marks is: 92.0\r\n\r\n\r\nEnter the name (or 'exit' to quit): Rohan\r\nEnter the usn: 124\r\nEnter the max_marks (25\/50\/100\/Any max): 25\r\nEnter the marks in the first subject out of 25: 24\r\nEnter the marks in the second subject out of 25: 12\r\nEnter the marks in the third subject out of 25: 12\r\nName is: Rohan\r\nUSN is: 124\r\nMarks are: &#91;24, 12, 12]\r\nTotal Marks is: 48\r\nAverage Marks is: 16.0\r\nPercentage Marks is: 64.0\r\n\r\n\r\nEnter the name (or 'exit' to quit): exit\n\n<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Approach2 : For Multiple Objects at a Time<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student:\r\n    def __init__(self, name, usn, max_marks):\r\n        self.name = name\r\n        self.usn = usn\r\n        self.max_marks = max_marks\r\n        self.marks = &#91;]\r\n\r\n    def getMarks(self, m1, m2, m3):\r\n        self.marks.extend(&#91;m1, m2, m3])\r\n\r\n    def displayData(self):\r\n        print(\"Name is:\", self.name)\r\n        print(\"USN is:\", self.usn)\r\n        print(\"Marks are:\", self.marks)\r\n        print(\"Total Marks is:\", self.total())\r\n        print(\"Average Marks is:\", self.average())\r\n        print(\"Percentage Marks is:\", self.percentage())\r\n\r\n    def total(self):\r\n        return sum(self.marks)\r\n\r\n    def average(self):\r\n        return sum(self.marks) \/ len(self.marks)\r\n\r\n    def percentage(self):\r\n        return (sum(self.marks) \/ (self.max_marks * len(self.marks))) * 100\r\n\r\n# Create a list to store student objects\r\nstudents = &#91;]\r\n\r\n# Input the number of students\r\nnum_students = int(input(\"Enter the number of Students: \"))\r\n\r\n# Input details for each student\r\nfor _ in range(num_students):\r\n    name = input(\"Enter the name: \")\r\n    usn = int(input(\"Enter the usn: \"))\r\n    max_marks = int(input(\"Enter the max_marks (25\/50\/100\/Any max): \"))\r\n    m1 = int(input(\"Enter the marks in the first subject out of {}: \".format(max_marks)))\r\n    m2 = int(input(\"Enter the marks in the second subject out of {}: \".format(max_marks)))\r\n    m3 = int(input(\"Enter the marks in the third subject out of {}: \".format(max_marks)))\r\n\r\n    # Create a student object and add it to the list\r\n    student = Student(name, usn, max_marks)\r\n    student.getMarks(m1, m2, m3)\r\n    students.append(student)\r\n\r\n# Display details for each student\r\nfor student in students:\r\n    student.displayData()\r\n    print(\"\\n\")<\/code><\/pre>\n\n\n\n<p>Sample Output :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the number of Students: 2\r\nEnter the name: Pavan\r\nEnter the usn: 123\r\nEnter the max_marks (25\/50\/100\/Any max): 25\r\nEnter the marks in the first subject out of 25: 12\r\nEnter the marks in the second subject out of 25: 12\r\nEnter the marks in the third subject out of 25: 13\r\nEnter the name: Rohan\r\nEnter the usn: 124\r\nEnter the max_marks (25\/50\/100\/Any max): 25\r\nEnter the marks in the first subject out of 25: 13\r\nEnter the marks in the second subject out of 25: 12\r\nEnter the marks in the third subject out of 25: 14\r\nName is: Pavan\r\nUSN is: 123\r\nMarks are: &#91;12, 12, 13]\r\nTotal Marks is: 37\r\nAverage Marks is: 12.333333333333334\r\nPercentage Marks is: 49.333333333333336\r\n\r\n\r\nName is: Rohan\r\nUSN is: 124\r\nMarks are: &#91;13, 12, 14]\r\nTotal Marks is: 39\r\nAverage Marks is: 13.0\r\nPercentage Marks is: 52.0<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dr. Thyagaraju G S&nbsp;and &nbsp;Ms.Palguni G T Laboratory Program 1A: &nbsp;Develop a program to read the student details like Name, USN, and Marks in three subjects. Display the student details,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-26572","page","type-page","status-publish","hentry"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/26572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/comments?post=26572"}],"version-history":[{"count":32,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/26572\/revisions"}],"predecessor-version":[{"id":27955,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/26572\/revisions\/27955"}],"wp:attachment":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/media?parent=26572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}