{"id":19867,"date":"2021-07-03T10:06:18","date_gmt":"2021-07-03T10:06:18","guid":{"rendered":"https:\/\/www.askpython.com\/?p=19867"},"modified":"2021-07-09T20:02:49","modified_gmt":"2021-07-09T20:02:49","slug":"smallest-number-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/smallest-number-in-python","title":{"rendered":"3 Easy Methods to Find the Smallest Number in Python"},"content":{"rendered":"\n<p>Hello there! This article is for beginners who wish to understand the basic code for finding the smallest number in Python. So let&#8217;s begin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Find the Smallest Number in Python?<\/h2>\n\n\n\n<p>We aim to find the smallest number in Python of all the numbers given in a list.<\/p>\n\n\n\n<p>Say if the list is:  [32, 54, 67, 21]                                                                                                                  <\/p>\n\n\n\n<p>The output should be: 21<\/p>\n\n\n\n<p>In this article, we will understand 3 different methods to do this.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Using Python min()<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/python-min-method\" data-type=\"post\" data-id=\"4482\">Min()<\/a> is a built-in function in python that takes a list as an argument and returns the smallest number in the list. An example is given below-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#declaring a list\nlist1 = &#x5B;-1, 65, 49, 13, -27] \nprint (&quot;list = &quot;, list1)\n\n#finding smallest number\ns_num = min (list1)\nprint (&quot;The smallest number in the given list is &quot;, s_num)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist = &#x5B;-1, 65, 49, 13, -27]\nThe smallest number in the given list is  -27\n<\/pre><\/div>\n\n\n<p>This is one of the simplest methods to find the smallest number. All you need to do is to pass the list to min() as an argument.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Using Python sort()<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python\/list\/python-sort-list\" data-type=\"post\" data-id=\"2393\">Sort()<\/a><span class=\"has-inline-color has-vivid-cyan-blue-color\"> <\/span><span class=\"has-inline-color has-black-color\">is another inbuilt method in python that doesn&#8217;t return the smallest<\/span><span class=\"has-inline-color has-vivid-cyan-blue-color\"> <\/span><span class=\"has-inline-color has-black-color\">number of the list<\/span>. Instead, it sorts the list in ascending order.<\/p>\n\n\n\n<p>So by sorting the list, we can access the first element of the list using indexing and that will be the smallest number in that list. Let&#8217;s see the code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#declaring a list\nlist1 = &#x5B;17, 53, 46, 8, 71]\nprint (&quot;list = &quot;, list1)\n\n#sorting the list\nlist1.sort ()\n\n#printing smallest number\nprint (&quot;The smallest number in the given list is &quot;, list1&#x5B;0])\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist =  &#x5B;17, 53, 46, 8, 71]\nThe smallest number in the given list is 8\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. Using the &#8216;for&#8217; loop <\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nls1 = &#x5B;]\ntotal_ele = int (input (&quot; How many elements you want to enter? &quot;))\n\n#getting list from the user\nfor i in range (total_ele):\n  n =int (input (&quot;Enter a number:&quot;))\n  ls1.append(n)\nprint (ls1)\nmin = ls1&#x5B;0]\n\n#finding smallest number\nfor i in range (len (ls1)):\n  if ls1&#x5B;i] &lt; min:\n    min = ls1&#x5B;i]\nprint (&quot;The smallest element is &quot;, min)\n<\/pre><\/div>\n\n\n<p>In the above code, we are using two <a href=\"https:\/\/www.askpython.com\/python\/python-loops-in-python\" data-type=\"post\" data-id=\"555\"><strong>for<\/strong> loops<\/a>, one for getting the elements of the list from the user and the second one for finding the smallest number from the list.<\/p>\n\n\n\n<p>After getting the elements from the user, we define the first element of the list (at 0 index) as the smallest number (min). Then with the for loop, we compare each element of the list to the <strong>min <\/strong>and if any element is smaller than <strong>min<\/strong>, it becomes the new <strong>min<\/strong>. <\/p>\n\n\n\n<p>This is how we get the smallest number from the user-given list.<\/p>\n\n\n\n<p><strong>The output for the above code is:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nHow many elements you want to enter? 4\nEnter a number: 15\nEnter a number: 47\nEnter a number: 23\nEnter a number: 6\n&#x5B;15, 47, 23, 6]\nThe smallest number is  6\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>So, these were some methods to find the smallest number from the given list in python. Hope you understood this! Feel free to ask questions below, if any. Thank you! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello there! This article is for beginners who wish to understand the basic code for finding the smallest number in Python. So let&#8217;s begin. How to Find the Smallest Number in Python? We aim to find the smallest number in Python of all the numbers given in a list. Say if the list is: [32, [&hellip;]<\/p>\n","protected":false},"author":34,"featured_media":19878,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-19867","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/19867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=19867"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/19867\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/19878"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=19867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=19867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=19867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}