{"id":18334,"date":"2022-09-06T17:54:52","date_gmt":"2022-09-06T12:24:52","guid":{"rendered":"https:\/\/copyassignment.com\/?p=18334"},"modified":"2022-12-02T17:03:37","modified_gmt":"2022-12-02T11:33:37","slug":"python-average-function-hackerrank-solution","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/python-average-function-hackerrank-solution\/","title":{"rendered":"Python Average Function Hackerrank Solution"},"content":{"rendered":"\n<p>We have a function called avg that takes in a variable number of integer inputs. We have to complete it such that it returns the average of all the input integers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def avg(*nums):\n   pass<\/code><\/pre>\n\n\n\n<p>This is the function that we need to complete.<\/p>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>The *args is a special syntax in python used to define a variable number of inputs. Having a * before the name of the argument means it is a variable input. All the inputs are available in this argument as a list<\/p>\n\n\n\n<p>avg(1,2,3,4) means that the nums will be [1,2,3,4]. we can use this to calculate the average.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code for Python Average Function Hackerrank Solution<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>def avg(*nums):\n    sum = 0\n    length = 0\n\n    for n in nums:\n        sum += n\n        length += 1\n\n    return sum\/length\n\nprint(avg(1,2,3,4,5))<\/code><\/pre>\n\n\n\n<h3 class=\"has-medium-font-size wp-block-heading\">Output:<\/h3>\n\n\n\n<p>3.0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Single Line Code<\/h2>\n\n\n\n<p>We can also write the code in a single line using python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def avg(*nums):\n    return sum(nums)\/len(nums)\n\nprint(avg(1,2,3,4,5))<\/code><\/pre>\n\n\n\n<h3 class=\"has-medium-font-size wp-block-heading\">Output:<\/h3>\n\n\n\n<p>3.0<\/p>\n\n\n\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-9886351916045880\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-9886351916045880\"\n     data-ad-slot=\"7933252109\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<div style=\"text-align:center\" class=\"wp-block-atomic-blocks-ab-button ab-block-button\"><a href=\"https:\/\/copyassignment.com\/top-100-python-projects-with-source-code\/\" class=\"ab-button ab-button-shape-rounded ab-button-size-medium\" style=\"color:#ffffff;background-color:#3373dc\">Best 100+ Python Projects with source code<\/a><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Also Read:<\/strong><\/p>\n\n\n<ul class=\"wp-block-latest-posts__list is-grid columns-3 wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/download-1000-projects-all-b-tech-programming-notes-job-resume-interview-guide-and-more-get-your-ultimate-programming-bundle\/\">Download 1000+ Projects, All B.Tech &#038; Programming Notes, Job, Resume &#038; Interview Guide, and More &#8211; Get Your Ultimate Programming Bundle!<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-8-solution-in-python-dictionaries-and-maps\/\">HackerRank Day 8 Solution in Python: Dictionaries and Maps<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-7-solution-in-python-arrays\/\">HackerRank Day 7 Solution in Python: Arrays<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-6-solution-in-python-lets-review\/\">HackerRank Day 6 Solution in Python: Let&#8217;s review<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-5-solution-in-python-loops\/\">HackerRank Day 5 Solution in Python: Loops<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-4-solution-in-python\/\">HackerRank Day 4 Solution in Python: Class vs Instance<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-3-solution-in-python\/\">HackerRank Day 3 Solution in Python: Intro to Conditional Statements<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-2-solution-in-python-operators\/\">HackerRank Day 2 Solution in Python: Operators<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-1-solution-in-python-data-types\/\">HackerRank Day 1 Solution in Python: Data Types<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-0-solution-in-python-hello-world\/\">HackerRank Day 0 Solution in Python: Hello World<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-29-solution-in-python-bitwise-and\/\">HackerRank Day 29 Solution in Python: Bitwise AND<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-28-solution-in-python-regex-patterns-and-intro-to-databases\/\">HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-27-solution-in-python-testing\/\">HackerRank Day 27 Solution in Python: Testing<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-26-solution-in-python-nested-logic\/\">HackerRank Day 26 Solution in Python: Nested Logic<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-25-solution-in-python-running-time-and-complexity\/\">HackerRank Day 25 Solution in Python: Running Time and Complexity<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-24-solution-in-python-more-linked-lists\/\">HackerRank Day 24 Solution in Python: More Linked Lists<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-23-solution-in-python-bst-level-order-traversal\/\">HackerRank Day 23 Solution in Python: BST Level Order Traversal<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-22-solution-in-python-binary-search-trees\/\">HackerRank Day 22 Solution in Python: Binary Search Trees<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/find-peak-element-leetcode-162\/\">Find Peak Element LeetCode 162<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-20-solution-in-python-sorting\/\">HackerRank Day 20 Solution in Python: Sorting<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-19-solution-in-python-interfaces\/\">HackerRank Day 19 Solution in Python: Interfaces<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-18-solution-in-python-queues-and-stacks\/\">HackerRank Day 18 Solution in Python: Queues and Stacks<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-17-solution-in-python-more-exceptions\/\">HackerRank Day 17 Solution in Python: More Exceptions<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-16-solution-exceptions-string-to-integer\/\">HackerRank Day 16 Solution: Exceptions &#8211; String to Integer<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/explained-allocate-minimum-number-of-pages\/\">Explained: Allocate minimum number of pages<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-15-solution-in-python-linked-list\/\">HackerRank Day 15 Solution in Python: Linked List<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/search-a-2d-matrix-leetcode-74\/\">Search a 2D matrix: leetcode 74<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/maximum-subarray-sum-kadanes-algorithm\/\">Maximum Subarray Sum: Kadane&#8217;s Algorithm<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-13-solution-in-python-abstract-classes\/\">HackerRank Day 13 Solution in Python: Abstract Classes<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-14-solution-in-python-scope\/\">HackerRank Day 14 Solution in Python: Scope<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>We have a function called avg that takes in a variable number of integer inputs. We have to complete it such that it returns the&#8230;<\/p>\n","protected":false},"author":62,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,1306],"tags":[],"class_list":["post-18334","post","type-post","status-publish","format-standard","hentry","category-allcategorites","category-competitive-programming","wpcat-22-id","wpcat-1306-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/18334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=18334"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/18334\/revisions"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=18334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=18334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=18334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}