{"id":4829,"date":"2020-09-15T13:46:38","date_gmt":"2020-09-15T08:16:38","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4829"},"modified":"2020-09-15T13:46:40","modified_gmt":"2020-09-15T08:16:40","slug":"python-range-function","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-range-function\/","title":{"rendered":"Python range() Function"},"content":{"rendered":"\n<p>Do you remember we used the Python range function while learning a loop? Let me remind you of an example.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-subtle-light-gray-background-color has-background\"><tbody><tr><td>for i in range(5):<br>print(i)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/hWEPPZf2ShGuVRT8KSiRCc0IBoAgd1KefKa2UvKR9Fxj2JtLHE74dsGJMUNGmSNz3ne9nC4DTzH2oUrK7PsjM9shhmKs0VW-abUrXNdwzupHafe8JRgP31jMDO3TmKJ0jrjB8UvUlkUfl_U4-A\" alt=\"python while loop\" title=\"\"><\/figure>\n\n\n\n<p>The range function is used to create a list of numbers on the basis of a number given by the user. The user gives a starting and ending number. If the user only mentions one number as in the above example then 0 is considered as the starting number and the number given by the user is considered as the ending number.<\/p>\n\n\n\n<p>There is a third input that a user can mention while calling the range function named \u201cstep\u201d. The step is the increment number. By default, the step is 1. Let\u2019s take a look at examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Start, Stop and Step in Range<\/h2>\n\n\n\n<p>In the table of two, the starting number is 2 ending number is 20 and the step size is 2.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/AyEnnYEcCyQTXLoCPEZXB1KKhOd1Qf7KqU_K46xxr3OT_vVsaXoJ-rJnVTtz5hRs72OCgr98Q9pLwEUE2gY8yByiptFnRGt-TaCYlH-P5ihueVbQTllCPKWk026b74mk8-JRC-IaQLZmviYXOw\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>Let write a <a href=\"https:\/\/www.h2kinfosys.com\/blog\/python-for-while-loops\/\">python for loop<\/a> to print this table.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-subtle-light-gray-background-color has-background\"><tbody><tr><td>for i in range(2,21,2):<br>print(i)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following is the output.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/2yO3dxTfqHL8c2yl-NWxR5PtkS8WOMPOQ93Njwb77ylnKC6iAZhOgY182NQRuzxfEUys6c_Hu0URcYEpNrO74ycwy1_PndFqSOS9_Uo12-WeyBuWZCpQ47j4SiWzpDR0DDlvtzZGKfPvWSNklg\" alt=\"python range\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Reverse Range<\/h2>\n\n\n\n<p>We can also the range function to create the numbers in reverse order.<\/p>\n\n\n\n<p>Let\u2019s take a look at an example.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-subtle-light-gray-background-color has-background\"><tbody><tr><td>for i in range(10,2,-2):<br>print(i)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/7tkU0ryF1aD_QtGFFMBUpSgh7NcNIpXOg_DHh5puCighYdoC-clZavsM81DPpNsVCpgNsigpBtq5E9kd233GHBTMWOqzaK-k6oDnCcM4k3LsBB-LDYUtYFMEJfAOkpQbrSRKzVW9y1DeT6tgPQ\" alt=\"Reverse Range\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">floating numbers in range() function<\/h2>\n\n\n\n<p>Python Range function does not accept the float numbers. If you try to use a float number then you will <a href=\"https:\/\/docs.python.org\/3\/library\/exceptions.html\" rel=\"nofollow noopener\" target=\"_blank\">receive a TypeError<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/nYhkQP4E99dZJQna_7a2xNvOXsA_Re2VusFu51wnYI467d1mxuaaU0bUz5k5_XE8MsbD67ceq3UvgxLF04LTsaapi4npqVnuo5JZEq3QsWBom0EASCpFiEyn2Nwzc37aCORJKXQHs19_Dtfemw\" alt=\"Range function \" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Creating List using the Range function&nbsp;<\/h2>\n\n\n\n<p>Suppose you need a list of numbers from 1 to 10 or the result of two\u2019s table stored in the list.<\/p>\n\n\n\n<p>Let\u2019s take a look at how to create it.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-subtle-light-gray-background-color has-background\"><tbody><tr><td>num = list(range(1,11))<br>print(num)<br>two_table = list(range(2,21,2))<br>print(two_table)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/fvXl9mIuy9vIyjWt7A_r7TGthu93mj6JRSkYYAcwXolBnsRp6GQSv1Y3apNGYiJTQuBN4BHmd_5YhVCkDA-F2RydYL1upBeVdYGEyjgeUVc1qwN2a2V31dAbjYVOKuQQ80m6_vbZlu94fNeJpA\" alt=\"\" title=\"\"><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Do you remember we used the Python range function while learning a loop? Let me remind you of an example. for i in range(5):print(i) The range function is used to create a list of numbers on the basis of a number given by the user. The user gives a starting and ending number. If the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4851,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[1354,1352,1353],"class_list":["post-4829","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-floating-numbers","tag-python-range-function","tag-reverse-range"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4829","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4829"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4829\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/4851"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}