filmov
tv
Python enumerate - a super helpful function to know

Показать описание
Let me elaborate on why I love Python enumerate! It adds a counter to your for loop without you having to explicitly update the count/index each time. Which can save you a lot of time - and prevent a lot of silly coding errors!
Basically, a “for loop” is a coding thing where you tell the computer to go through some list of things and do something each time, such as printing out the thing. And with enumerate it will keep count as it goes. You can access that count by doing something like this:
for i, thing in things:
“i” will be the index and each time you go through, it will be updated
Like most things in Python, it will start at 0 by default (so i=0 for the first thing in the list). But you can specify the start value (just put “, start = “ to get it to start wherever you want.
I think it’s easiest to explain with examples, so hope this quick vid helps. I will also upload the code file if you want to look at the syntax and play around.
I’m definitely not a Python expert, but this is one of those fundamental Python functions that anyone who only tangentially uses Python should know. And for some reason it took me a while to find out about it. So I thought I’d introduce you if you weren’t familiar with it.
If you want more information, RealPython has a nice guide:
It’s a built-in Python function so you don’t have to import anything. Just enjoy!
Basically, a “for loop” is a coding thing where you tell the computer to go through some list of things and do something each time, such as printing out the thing. And with enumerate it will keep count as it goes. You can access that count by doing something like this:
for i, thing in things:
“i” will be the index and each time you go through, it will be updated
Like most things in Python, it will start at 0 by default (so i=0 for the first thing in the list). But you can specify the start value (just put “, start = “ to get it to start wherever you want.
I think it’s easiest to explain with examples, so hope this quick vid helps. I will also upload the code file if you want to look at the syntax and play around.
I’m definitely not a Python expert, but this is one of those fundamental Python functions that anyone who only tangentially uses Python should know. And for some reason it took me a while to find out about it. So I thought I’d introduce you if you weren’t familiar with it.
If you want more information, RealPython has a nice guide:
It’s a built-in Python function so you don’t have to import anything. Just enjoy!
Комментарии