[017] Python Basics: Range and Enumerate

Photo by Markus Spiske on Unsplash

When looping in Python, one of the most common tools is the range function. Range object is a special type of object in Python that returns an object that produces a sequence of integers from start to stop. Let’s explore it a little bit more about range:

If I wanted to print a range and let’s say one hundred. Here we get something like this which look like a tuple but it’s not and it’s a range object that give us a default of zero to whatever the input we give in which is 100:

or we can just give in value like (0,100) and it will produce the same result:

Besides tuples, lists, sets, dictionaries and strings, we can also iterate over a range. For instance, we will insert something like for number in range and then we print number. we are able to get number from 0–99 (total 100 of items):

Why is this useful? With range, We was able to loop a hundred times. We can also choose to loop a certain number of times which is quite useful:

Another neat trick in Python where the programmer doesn’t need a variable. We can do underscore (_) and it still works fine. This just indicate/show other people that we don’t really care what this variable is and wan to use range so that we can loop over things:

Range also come with third parameter which is step over (by default step over is 1). If I do two, it jumps from zero to 10 (0,2,4,6,8) which is very handy as range creates a special kind of object that can iterate over:

Well, if I insert the step over with a minus value, it won’t work.

Why this won’t work? Let me show you another example. Here, the value will be going in the opposite direction.

In order for us to reverse the value, we must add the minus one to do so. Well, you have a lot of options to play with the range. If we do the reverse 10 to 0, it wont work:

We can also do something interesting with range with list. Here, we loop two times with range(2) and create an iterable object that have 10 items with range(10). Then, convert the list of 10 items into list which you can see in the list 0 to 9. So, this is a quick way to create list that can be use in your journey.

Another useful function that we will able to use just like range. Might not be as common as range as you will see range in a lot of Python Code and it is known as enumerate. Now what does it do? Let’s have a look what does enumerate mean:

We have to give something that the enumerate can iterate over. So, let’s do a string that says hello. Now, enumerate is special as it is going to take an editable object and give you an index counter and the item of that index. Here, if we don’t have enumerate , we would just have gotten hello and we also get the index of item since we use enumerate:

So let’s say we use a list of 1,2,3 and we still able to access the index number of items (1,2,3) as well :

What if we have a tuple, it still work and we get the index number:

Thus, enumerate is useful when you need the index counter of the item that you are looping through. Let’s do a simple exercise, we want to create a script that enumerates a list of numbers. We will use range to create it and we get the index of numbers that we want. The f string is used to provide a concise and convenient way to embed python expressions inside string literals for formatting:

And just to confirm the index of 50 is 50, we can print out all the numbers:

Well, enumerate is not useful as range but you will see it occasionally in code.

Thanks for reading and I write for fun 😄

--

--

--

A Bachelor CS student with major in Network Security (UniSZa). Every stories I shared is based on my personal opinion. Thanks you. Having my Internship now.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Azure Public IP Availability Zone

The Technical Interview Guide to Backtracking

photo down the length of a covered arcade with repeating architectural features

Get random documents from a collection in firebase firestore with firebase cloud functions

Is DevOps Making Life of Data Scientists Better?

Event-Driven Architectures with Kafka and Python

Details of My Personal Automation Infrastructure

Bindless rendering — Templates

Install Wordpress locally (macOS)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Koay Yong Cett

Koay Yong Cett

A Bachelor CS student with major in Network Security (UniSZa). Every stories I shared is based on my personal opinion. Thanks you. Having my Internship now.

More from Medium

Useful functions in Python

How to Write Better Code in Python

Exception Handling | A Beginners Guide:

How to install python 3.9 on Debian 10