{"id":12053,"date":"2021-11-18T05:38:17","date_gmt":"2021-11-18T05:38:17","guid":{"rendered":"https:\/\/linuxways.net\/?p=12053"},"modified":"2024-03-25T02:10:45","modified_gmt":"2024-03-25T02:10:45","slug":"python-range-function","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/python-range-function\/","title":{"rendered":"Python range() function"},"content":{"rendered":"<h2><a id=\"post-12053-_fhgsdklwxutl\"><\/a>Introduction<\/h2>\n<p>The range() function in Python prints out a string of numbers according to the start value, end value, and jump we entered. In Python2 version, it is called xrange() function, it wasn&#8217;t until Python3 version that it was named range() function.<\/p>\n<p>And we will show you how to use the range() function in Python as you go through it below. Hope you understand.<\/p>\n<h2><a id=\"post-12053-_uc44d3juu7hf\"><\/a>Example<\/h2>\n<p><strong>Example 1<\/strong>: Number sequence from 1 to 4<\/p>\n<pre>\r\nx = range(1, 5)\r\n\r\nfor i in x:\r\n\r\nprint(i)\r\n<\/pre>\n<p>Output:<\/p>\n<pre>\r\n<strong>1<\/strong>\r\n\r\n<strong>2<\/strong>\r\n\r\n<strong>3<\/strong>\r\n\r\n<strong>4<\/strong>\r\n<\/pre>\n<p><strong>Example 2<\/strong>: The sequence of numbers from 3 to 12 step is 3<\/p>\n<pre>\r\nx = range(3, 15, 3)\r\n\r\nfor i in x:\r\n\r\nprint(i)\r\n<\/pre>\n<p>Output:<\/p>\n<pre>\r\n<strong>3<\/strong>\r\n\r\n<strong>6<\/strong>\r\n\r\n<strong>9<\/strong>\r\n\r\n<strong>12<\/strong>\r\n<\/pre>\n<h2>Definition<\/h2>\n<p>The range() function in Python prints out a string of numbers. By default, it will start at 0, step is 1 and stop at the specified number.<\/p>\n<p>We can specify starting point and step.<\/p>\n<h2>The syntax<\/h2>\n<pre>\r\nrange(start, stop, step)\r\n<\/pre>\n<p><strong>Parameter Values<\/strong>:<\/p>\n<p>start: the starting point is an integer. By default, it is 0 (optional).<\/p>\n<p>stop: the stopping point is an integer (required).<\/p>\n<p>step: the step of the sequence of numbers is an integer. By default, it is 1 (optional).<\/p>\n<h2>More examples<\/h2>\n<p><strong>Example 1<\/strong>:<\/p>\n<pre>\r\nrange() combined with the list() function\r\n\r\n# Number range from 0 to 9\r\n\r\nx = list(range(10))\r\n\r\nprint(x)\r\n\r\n# Number range from 1 to 9\r\n\r\ny = list(range(1,10))\r\n\r\nprint(y)\r\n<\/pre>\n<p>Output:<\/p>\n<pre>\r\n<strong>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<\/strong>\r\n\r\n<strong>[1, 2, 3, 4, 5, 6, 7, 8, 9]<\/strong>\r\n<\/pre>\n<p><strong>Example 2<\/strong>: Sequence of even numbers from 2 to 12<\/p>\n<pre>\r\nx = list(range(2,14,2))\r\n\r\nprint(x)\r\n<\/pre>\n<p>Output:<\/p>\n<pre>\r\n<strong>[2, 4, 6, 8, 10, 12]<\/strong>\r\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>Above is the tutorial about how to use the range() function in Python through examples.<\/p>\n<p>Thanks for reading!<\/p>","protected":false},"excerpt":{"rendered":"<p>Introduction The range() function in Python prints out a string of numbers according to the start value, end value, and jump we entered. In Python2 version, it is&hellip;<\/p>","protected":false},"author":110,"featured_media":12064,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[10],"class_list":["post-12053","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-python"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/12053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=12053"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/12053\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/12064"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=12053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=12053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=12053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}