{"id":39220,"date":"2025-11-08T09:51:26","date_gmt":"2025-11-08T17:51:26","guid":{"rendered":"https:\/\/cloudinary.com\/blog\/?p=39220"},"modified":"2025-11-08T09:51:27","modified_gmt":"2025-11-08T17:51:27","slug":"how-to-use-the-range-function-in-python-for-loops","status":"publish","type":"post","link":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/","title":{"rendered":"How to use the range() function in Python for loops"},"content":{"rendered":"\n<p>If you read enough Python threads, you will see the same question appear whenever looping and indexing come up: what exactly does range do, how are its arguments interpreted, and when should you use it versus iterating directly over items?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Question:<\/h2>\n\n\n\n<p><em>Hi all, I am a bit confused about how to use the range() function in Python for loops. I want to understand: how start, stop, and step work, how to loop backward, how to iterate in fixed-size batches, when to use range with enumerate, and what common pitfalls to avoid. Examples would be great.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Answer:<\/h2>\n\n\n\n<p>The range type in Python is a memory efficient, immutable sequence of integers commonly used in for loops. It doesn\u2019t allocate a list of numbers; it generates values on demand, which makes it ideal for large loops.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Range() syntax and behavior<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>range(stop):<\/strong> starts at 0, increments by 1, ends before stop<\/li>\n\n\n\n<li><strong>range(start, stop):<\/strong> starts at start, increments by 1, ends before stop<\/li>\n\n\n\n<li><strong>range(start, stop, step):<\/strong> starts at start, increments by step, ends before reaching stop<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-comment\"># 0, 1, 2, 3, 4<\/span>\n<span class=\"hljs-keyword\">for<\/span> i in range(<span class=\"hljs-number\">5<\/span>):\n\u00a0 \u00a0 <span class=\"hljs-keyword\">print<\/span>(i)\n\n<span class=\"hljs-comment\"># 1, 2, 3, 4, 5<\/span>\n<span class=\"hljs-keyword\">for<\/span> i in range(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">6<\/span>):\n\u00a0 \u00a0 <span class=\"hljs-keyword\">print<\/span>(i)\n\n<span class=\"hljs-comment\"># 0, 2, 4, 6, 8<\/span>\n<span class=\"hljs-keyword\">for<\/span> i in range(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>):\n\u00a0 \u00a0 <span class=\"hljs-keyword\">print<\/span>(i)\n\n<span class=\"hljs-comment\"># 10, 9, 8, 7, 6<\/span>\n<span class=\"hljs-keyword\">for<\/span> i in range(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">-1<\/span>):\n\u00a0 \u00a0 <span class=\"hljs-keyword\">print<\/span>(i)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Common patterns you will use a lot<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Index-based iteration when you need positions:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">items = &#91;<span class=\"hljs-string\">\"a\"<\/span>, <span class=\"hljs-string\">\"b\"<\/span>, <span class=\"hljs-string\">\"c\"<\/span>]\n<span class=\"hljs-keyword\">for<\/span> i in range(len(items)):\n\u00a0 \u00a0 <span class=\"hljs-keyword\">print<\/span>(i, items&#91;i])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Prefer enumerate when you just need index and value:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-keyword\">for<\/span> i, item in enumerate(items):\n\u00a0 \u00a0 <span class=\"hljs-keyword\">print<\/span>(i, item)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Fixed-size batching:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">data = <span class=\"hljs-keyword\">list<\/span>(range(<span class=\"hljs-number\">23<\/span>))\nbatch_size = <span class=\"hljs-number\">5<\/span>\n<span class=\"hljs-keyword\">for<\/span> start in range(<span class=\"hljs-number\">0<\/span>, len(data), batch_size):\n\u00a0 \u00a0 batch = data&#91;start:start + batch_size]\n\u00a0 \u00a0 <span class=\"hljs-keyword\">print<\/span>(batch)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Loop a specific number of times without caring about the counter:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-keyword\">for<\/span> _ in range(<span class=\"hljs-number\">3<\/span>):\n\u00a0 \u00a0 <span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">\"retrying...\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Reverse iteration:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-keyword\">for<\/span> i in reversed(range(<span class=\"hljs-number\">5<\/span>)):\u00a0 <span class=\"hljs-comment\"># 4, 3, 2, 1, 0<\/span>\n\u00a0 \u00a0 <span class=\"hljs-keyword\">print<\/span>(i)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Pitfalls to avoid<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Off-by-one errors: <\/strong>Remember that <code>stop<\/code> is exclusive. If you want 1 to 10 inclusive, use range(1, 11).<\/li>\n\n\n\n<li><strong>Zero or misplaced step:<\/strong> <code>step<\/code> cannot be 0. Also, ensure <code>step<\/code> matches the direction of start to stop.<\/li>\n\n\n\n<li><strong>Unnecessary list conversion:<\/strong> <code>list(range(...))<\/code> materializes the whole sequence. Only do it if you truly need a list.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Performance and features<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Memory friendly: range stores only start, stop, step, not all integers.<\/li>\n\n\n\n<li>Supports membership and length in O(1):<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">r = range(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">1000000<\/span>, <span class=\"hljs-number\">3<\/span>)\n<span class=\"hljs-keyword\">print<\/span>(len(r)) \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># fast<\/span>\n<span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-number\">10<\/span> in r)\u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># fast arithmetic check<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Using range in real tasks<\/h3>\n\n\n\n<p>Iterating over files, frames, or image assets is a common use case. For example, you might export a sequence of thumbnails at fixed intervals or process a dataset in batches.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">from pathlib import Path\nfrom PIL import Image\n\nsrc_dir = Path(<span class=\"hljs-string\">\"input\"<\/span>)\ndst_dir = Path(<span class=\"hljs-string\">\"output\"<\/span>)\ndst_dir.mkdir(exist_ok=<span class=\"hljs-keyword\">True<\/span>)\n\nimages = sorted(src_dir.glob(<span class=\"hljs-string\">\"*.jpg\"<\/span>))\n<span class=\"hljs-keyword\">for<\/span> i in range(<span class=\"hljs-number\">0<\/span>, len(images), <span class=\"hljs-number\">10<\/span>):\u00a0 <span class=\"hljs-comment\"># process every 10th image<\/span>\n\u00a0 \u00a0 img_path = images&#91;i]\n\u00a0 \u00a0 with Image.open(img_path) <span class=\"hljs-keyword\">as<\/span> im:\n\u00a0 \u00a0 \u00a0 \u00a0 thumb = im.resize((<span class=\"hljs-number\">320<\/span>, <span class=\"hljs-number\">320<\/span>))\n\u00a0 \u00a0 \u00a0 \u00a0 thumb.save(dst_dir \/ f<span class=\"hljs-string\">\"thumb_{i:04d}.jpg\"<\/span>, quality=<span class=\"hljs-number\">85<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When choosing formats inside such loops, review format tradeoffs like <a href=\"https:\/\/cloudinary.com\/guides\/image-formats\/jpeg-vs-png\">JPEG vs PNG<\/a> to balance quality and size.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Doing the same with Cloudinary workflows<\/h3>\n\n\n\n<p>If you want to automate uploads and delivery at scale, you can pair range with a cloud media platform. For example, you can iterate deterministically over files, upload them programmatically, and output consistent public IDs for reliable downstream delivery. Cloudinary also offers a collection of handy <a href=\"https:\/\/cloudinary.com\/tools\">online media tools<\/a> if you need quick format conversions or basic edits before uploading.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-comment\"># pip install cloudinary<\/span>\nimport cloudinary\nimport cloudinary.uploader\nfrom pathlib import Path\n\ncloudinary.config(\n    cloud_name=<span class=\"hljs-string\">\"YOUR_CLOUD_NAME\"<\/span>,\n    api_key=<span class=\"hljs-string\">\"YOUR_API_KEY\"<\/span>,\n    api_secret=<span class=\"hljs-string\">\"YOUR_API_SECRET\"<\/span>\n)\n\nsrc_dir = Path(<span class=\"hljs-string\">\"output\"<\/span>)  <span class=\"hljs-comment\"># from previous step<\/span>\nimgs = sorted(src_dir.glob(<span class=\"hljs-string\">\"thumb_*.jpg\"<\/span>))\n\n<span class=\"hljs-keyword\">for<\/span> i in range(len(imgs)):\n    path = str(imgs&#91;i])\n    public_id = f<span class=\"hljs-string\">\"project\/thumb_{i:04d}\"<\/span>\n    cloudinary.uploader.upload(\n        path,\n        public_id=public_id,\n        overwrite=<span class=\"hljs-keyword\">True<\/span>,\n        folder=<span class=\"hljs-string\">\"project\"<\/span>\n    )\n    <span class=\"hljs-comment\"># Later, deliver:<\/span>\n    <span class=\"hljs-comment\"># https:\/\/res.cloudinary.com\/YOUR_CLOUD_NAME\/image\/upload\/w_320,c_fill\/project\/thumb_0000.jpg<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For more Python-centric processing ideas that integrate nicely with remote delivery, the resources above on saving and analyzing images will help you structure robust loops and pipelines.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TL;DR<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>range(start, stop, step)<\/code> yields integers up to but not including stop.<\/li>\n\n\n\n<li>Use <code>enumerate<\/code> for index plus value, and range for batching or when you truly need indices.<\/li>\n\n\n\n<li>Try not to convert <code>range<\/code> to a list unless required; it is memory efficient by design.<\/li>\n\n\n\n<li>Validate <code>step<\/code> and watch for off-by-one mistakes.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Learn More<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/cloudinary.com\/tools\/jpg-to-webp\">JPG to WebP<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/tools\/png-to-webp\">PNG to WebP<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/tools\/image-to-jpg\">Image to JPG<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/background-remover\">Background Remover<\/a><\/li>\n<\/ul>\n\n\n\n<p>Ready to streamline how you process and deliver images and videos in Python-driven projects? <a href=\"https:\/\/cloudinary.com\/users\/register_free\">Create your free Cloudinary account<\/a> and start building efficient, scalable media workflows today.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you read enough Python threads, you will see the same question appear whenever looping and indexing come up: what exactly does range do, how are its arguments interpreted, and when should you use it versus iterating directly over items? Question: Hi all, I am a bit confused about how to use the range() function [&hellip;]<\/p>\n","protected":false},"author":88,"featured_media":39221,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_cloudinary_featured_overwrite":false,"footnotes":""},"categories":[1],"tags":[423],"class_list":["post-39220","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-questions"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to use the range() function in Python for loops<\/title>\n<meta name=\"description\" content=\"If you read enough Python threads, you will see the same question appear whenever looping and indexing come up: what exactly does range do, how are its\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use the range() function in Python for loops\" \/>\n<meta property=\"og:description\" content=\"If you read enough Python threads, you will see the same question appear whenever looping and indexing come up: what exactly does range do, how are its\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudinary Blog\" \/>\n<meta property=\"article:published_time\" content=\"2025-11-08T17:51:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-08T17:51:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762623823\/how_to_use_the_range_function_in_python_for_loops_featured_image\/how_to_use_the_range_function_in_python_for_loops_featured_image.jpg?_i=AA\" \/>\n\t<meta property=\"og:image:width\" content=\"2000\" \/>\n\t<meta property=\"og:image:height\" content=\"1100\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"damjanantevski\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"NewsArticle\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/\"},\"author\":{\"name\":\"damjanantevski\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e\"},\"headline\":\"How to use the range() function in Python for loops\",\"datePublished\":\"2025-11-08T17:51:26+00:00\",\"dateModified\":\"2025-11-08T17:51:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/\"},\"wordCount\":519,\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762623823\/how_to_use_the_range_function_in_python_for_loops_featured_image\/how_to_use_the_range_function_in_python_for_loops_featured_image.jpg?_i=AA\",\"keywords\":[\"Questions\"],\"inLanguage\":\"en-US\",\"copyrightYear\":\"2025\",\"copyrightHolder\":{\"@id\":\"https:\/\/cloudinary.com\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/\",\"url\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/\",\"name\":\"How to use the range() function in Python for loops\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762623823\/how_to_use_the_range_function_in_python_for_loops_featured_image\/how_to_use_the_range_function_in_python_for_loops_featured_image.jpg?_i=AA\",\"datePublished\":\"2025-11-08T17:51:26+00:00\",\"dateModified\":\"2025-11-08T17:51:27+00:00\",\"description\":\"If you read enough Python threads, you will see the same question appear whenever looping and indexing come up: what exactly does range do, how are its\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#primaryimage\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762623823\/how_to_use_the_range_function_in_python_for_loops_featured_image\/how_to_use_the_range_function_in_python_for_loops_featured_image.jpg?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762623823\/how_to_use_the_range_function_in_python_for_loops_featured_image\/how_to_use_the_range_function_in_python_for_loops_featured_image.jpg?_i=AA\",\"width\":2000,\"height\":1100},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudinary.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use the range() function in Python for loops\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"name\":\"Cloudinary Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudinary.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\",\"name\":\"Cloudinary Blog\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"width\":312,\"height\":60,\"caption\":\"Cloudinary Blog\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e\",\"name\":\"damjanantevski\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g\",\"caption\":\"damjanantevski\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to use the range() function in Python for loops","description":"If you read enough Python threads, you will see the same question appear whenever looping and indexing come up: what exactly does range do, how are its","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:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/","og_locale":"en_US","og_type":"article","og_title":"How to use the range() function in Python for loops","og_description":"If you read enough Python threads, you will see the same question appear whenever looping and indexing come up: what exactly does range do, how are its","og_url":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/","og_site_name":"Cloudinary Blog","article_published_time":"2025-11-08T17:51:26+00:00","article_modified_time":"2025-11-08T17:51:27+00:00","og_image":[{"width":2000,"height":1100,"url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762623823\/how_to_use_the_range_function_in_python_for_loops_featured_image\/how_to_use_the_range_function_in_python_for_loops_featured_image.jpg?_i=AA","type":"image\/jpeg"}],"author":"damjanantevski","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#article","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/"},"author":{"name":"damjanantevski","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e"},"headline":"How to use the range() function in Python for loops","datePublished":"2025-11-08T17:51:26+00:00","dateModified":"2025-11-08T17:51:27+00:00","mainEntityOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/"},"wordCount":519,"publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762623823\/how_to_use_the_range_function_in_python_for_loops_featured_image\/how_to_use_the_range_function_in_python_for_loops_featured_image.jpg?_i=AA","keywords":["Questions"],"inLanguage":"en-US","copyrightYear":"2025","copyrightHolder":{"@id":"https:\/\/cloudinary.com\/#organization"}},{"@type":"WebPage","@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/","url":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/","name":"How to use the range() function in Python for loops","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#primaryimage"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762623823\/how_to_use_the_range_function_in_python_for_loops_featured_image\/how_to_use_the_range_function_in_python_for_loops_featured_image.jpg?_i=AA","datePublished":"2025-11-08T17:51:26+00:00","dateModified":"2025-11-08T17:51:27+00:00","description":"If you read enough Python threads, you will see the same question appear whenever looping and indexing come up: what exactly does range do, how are its","breadcrumb":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#primaryimage","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762623823\/how_to_use_the_range_function_in_python_for_loops_featured_image\/how_to_use_the_range_function_in_python_for_loops_featured_image.jpg?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762623823\/how_to_use_the_range_function_in_python_for_loops_featured_image\/how_to_use_the_range_function_in_python_for_loops_featured_image.jpg?_i=AA","width":2000,"height":1100},{"@type":"BreadcrumbList","@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-use-the-range-function-in-python-for-loops\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudinary.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to use the range() function in Python for loops"}]},{"@type":"WebSite","@id":"https:\/\/cloudinary.com\/blog\/#website","url":"https:\/\/cloudinary.com\/blog\/","name":"Cloudinary Blog","description":"","publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudinary.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cloudinary.com\/blog\/#organization","name":"Cloudinary Blog","url":"https:\/\/cloudinary.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","width":312,"height":60,"caption":"Cloudinary Blog"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e","name":"damjanantevski","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g","caption":"damjanantevski"}}]}},"jetpack_featured_media_url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762623823\/how_to_use_the_range_function_in_python_for_loops_featured_image\/how_to_use_the_range_function_in_python_for_loops_featured_image.jpg?_i=AA","_links":{"self":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/39220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/users\/88"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/comments?post=39220"}],"version-history":[{"count":1,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/39220\/revisions"}],"predecessor-version":[{"id":39222,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/39220\/revisions\/39222"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media\/39221"}],"wp:attachment":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media?parent=39220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/categories?post=39220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/tags?post=39220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}