{"id":13193,"date":"2020-04-23T01:30:42","date_gmt":"2020-04-23T08:30:42","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=13193"},"modified":"2023-12-01T02:44:04","modified_gmt":"2023-12-01T10:44:04","slug":"python-median","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-median\/","title":{"rendered":"Python Median() &#8211; A Python Statistics Tutorial"},"content":{"rendered":"\n<p><strong>Python median(): <\/strong><br><em>With the Python statistics module, you can find the median, or middle value, of a data set. The Python median() function allows you to calculate the median of any data set without first sorting the list.<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>When you\u2019re working with numbers in Python, you may want to calculate the median of a <a href=\"https:\/\/careerkarma.com\/blog\/python-list-methods\/\">list of numbers<\/a>. For instance, let&#8217;s say you\u2019re building a program to get insights about student ages in a fourth-grade class. You may want to calculate the median age of the students.<\/p>\n\n\n\n<p>That\u2019s where the <code>statistics.median()<\/code> function comes in. The <code>statistics.median()<\/code> function is part of Python\u2019s statistics module and can find the median of a list of values.<\/p>\n\n\n\n<p>This tutorial will discuss how to use the <code>statistics.median()<\/code> method. We will also walk through an example of <code>statistics.median()<\/code> in action and will break down, line-by-line, how the method works.<\/p>\n\n\n\n<p>This tutorial will discuss how to use the <code>statistics.median()<\/code> method. We will also walk through an example of <code>statistics.median()<\/code> in action and break down how the method works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Median<\/h2>\n\n\n\n<p>In statistics, the median is the <em>middle<\/em> value in a sorted list of numbers. For example, for a data set with the numbers 9, 3, 6, 1, and 4, the median value is 4.&nbsp;<\/p>\n\n\n\n<p>When analyzing and describing a data set, you often use median with <a href=\"https:\/\/careerkarma.com\/blog\/python-average\/\">mean<\/a>, standard deviation, and other statistical calculations.<\/p>\n\n\n\n<p>In Python, the <code>statistics.median()<\/code> function is used to calculate the median value of a data set.<\/p>\n\n\n\n<p><code>statistics.median()<\/code> is part of the statistics <a href=\"https:\/\/careerkarma.com\/blog\/what-is-a-python-module\/\">Python module<\/a>. It includes a number of functions for statistical analysis.  First, import the statistics module with this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import statistics<\/pre><\/div>\n\n\n\n<p>Now we can use the <code>median()<\/code> function. Here\u2019s the syntax for the <code>statistics.median()<\/code> method:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block syntax-highlight-text\"><pre>import statistics\nstatistics.median(list_name)<\/pre><\/div>\n\n\n\n<p>The <code>median()<\/code> method takes in one parameter: the data list.<\/p>\n\n\n\n<p>When you call the <code>median()<\/code> method, it will order a list of values and find its middle value. If the number of data points is odd, the middle data point will be returned. If the number is even, the median is the midpoint between the two middle values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Median Example<\/h2>\n\n\n\n<p>Let\u2019s walk through an example. Say we are building a program that to calculate all student ages in a fourth-grade class to learn about their age distribution. We want to use <code>median()<\/code> to find out the median age of the class.<\/p>\n\n\n\n<p>We could use this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import statistics\nstudent_ages = [9, 10, 9, 10, 9, 9, 10, 11, 11, 10, 9, 9]\nmedian_age = statistics.median(student_ages)\nprint(median_age)<\/pre><\/div>\n\n\n\n<p>Our program returns:<\/p>\n\n\n\n<p><code>9.5<\/code><\/p>\n\n\n\n<p>This is the median student age.<\/p>\n\n\n\n<p>Let\u2019s break down our code. On the first line, we import the statistics module to access the <code>median()<\/code> method in our code. Then, we define a list. The list is called <code>student_ages<\/code><em>.<\/em> It stores the ages of all the students.<\/p>\n\n\n\n<p>Next, we use <code>median()<\/code> and pass the <code>student_ages<\/code> variable to calculate the median age. We assign the returned value to the variable <code>median_age<\/code>. Then, we use <code>print()<\/code> to print the value of <code>median_age<\/code> to the console.&nbsp;<\/p>\n\n\n\n<p>When working with <code>median()<\/code>, the list you use should have at least one value. If you pass an empty list through the <code>median()<\/code> method, you get this error:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>StatisticsError: no median for empty data<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Median of a Tuple<\/h2>\n\n\n\n<p>In addition, you can use the median method to find the median of a <a href=\"https:\/\/careerkarma.com\/blog\/python-tuples\/\">Python tuple<\/a>. A tuple is a data type that is ordered and unchangeable. This means that it is useful when you want to store similar data that will not change over time. Tuples are declared as a list of comma-separated values enclosed within curly brackets.<\/p>\n\n\n\n<p>Say that our data set of student ages were stored as a tuple instead of a list. If we wanted to calculate the median age of the students in the fourth-grade class, we could do so using the <code>median()<\/code> method. Here\u2019s the code we would use to calculate the median of our tuple:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import statistics\nstudent_ages = (9, 10, 9, 10, 9, 9, 10, 11, 11, 10, 9, 9)\nprint(statistics.median(student_ages))<\/pre><\/div>\n\n\n\n<p>Our code returns: <code>9.5<\/code>.  <\/p>\n\n\n\n<p>The median value is the same, but instead of our data being stored in a list, it is stored in a tuple. This is evident because our data is enclosed within round brackets (<code>()<\/code>) instead of square brackets (<code>[]<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>You can use the <code>statistics.median()<\/code> method to calculate the median value of a data set. Using examples, this tutorial discussed how to use the <code>statistics.median()<\/code> method in Python to calculate the middle value of a list and a tuple.<\/p>\n\n\n\n<p>You now have the knowledge you need to start using the <code>statistics.median()<\/code> method like a professional Python developer!<\/p>\n","protected":false},"excerpt":{"rendered":"Python median(): With the Python statistics module, you can find the median, or middle value, of a data set. The Python median() function allows you to calculate the median of any data set without first sorting the list. When you\u2019re working with numbers in Python, you may want to calculate the median of a list&hellip;","protected":false},"author":240,"featured_media":15016,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-13193","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Median() - A Python Statistics Tutorial %<\/title>\n<meta name=\"description\" content=\"Coders use the statistics.median() method to calculate the median of a list or a tuple in Python. Learn how to use Python median() here!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-median\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Median() - A Python Statistics Tutorial\" \/>\n<meta property=\"og:description\" content=\"Coders use the statistics.median() method to calculate the median of a list or a tuple in Python. Learn how to use Python median() here!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-median\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-23T08:30:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T10:44:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/python-median.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1680\" \/>\n\t<meta property=\"og:image:height\" content=\"945\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-median\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-median\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python Median() &#8211; A Python Statistics Tutorial\",\"datePublished\":\"2020-04-23T08:30:42+00:00\",\"dateModified\":\"2023-12-01T10:44:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-median\/\"},\"wordCount\":700,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-median\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/python-median.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-median\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-median\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-median\/\",\"name\":\"Python Median() - A Python Statistics Tutorial %\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-median\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-median\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/python-median.jpg\",\"datePublished\":\"2020-04-23T08:30:42+00:00\",\"dateModified\":\"2023-12-01T10:44:04+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"Coders use the statistics.median() method to calculate the median of a list or a tuple in Python. Learn how to use Python median() here!\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-median\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-median\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-median\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/python-median.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/python-median.jpg\",\"width\":1680,\"height\":945,\"caption\":\"Python Median()\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-median\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Median() &#8211; A Python Statistics Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Median() - A Python Statistics Tutorial %","description":"Coders use the statistics.median() method to calculate the median of a list or a tuple in Python. Learn how to use Python median() here!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/python-median\/","og_locale":"en_US","og_type":"article","og_title":"Python Median() - A Python Statistics Tutorial","og_description":"Coders use the statistics.median() method to calculate the median of a list or a tuple in Python. Learn how to use Python median() here!","og_url":"https:\/\/careerkarma.com\/blog\/python-median\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-04-23T08:30:42+00:00","article_modified_time":"2023-12-01T10:44:04+00:00","og_image":[{"width":1680,"height":945,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/python-median.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-median\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-median\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python Median() &#8211; A Python Statistics Tutorial","datePublished":"2020-04-23T08:30:42+00:00","dateModified":"2023-12-01T10:44:04+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-median\/"},"wordCount":700,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-median\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/python-median.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-median\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-median\/","url":"https:\/\/careerkarma.com\/blog\/python-median\/","name":"Python Median() - A Python Statistics Tutorial %","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-median\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-median\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/python-median.jpg","datePublished":"2020-04-23T08:30:42+00:00","dateModified":"2023-12-01T10:44:04+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"Coders use the statistics.median() method to calculate the median of a list or a tuple in Python. Learn how to use Python median() here!","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-median\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-median\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-median\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/python-median.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/python-median.jpg","width":1680,"height":945,"caption":"Python Median()"},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-median\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python Median() &#8211; A Python Statistics Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/13193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=13193"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/13193\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/15016"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=13193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=13193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=13193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}