0

"For" doubt in python

I'm currently taking a course in python and i ran into this code: def has_33(nums): for i in range(0,len(nums)-1): if nums[i] == 3 and nums[i+1] == 3: return True return False my doubt is why in this specific case the interator "i" is able to be used as an integer, without giving back am error, since inside the if they are not using '3' but instead they are using 3 and they are not converting it using an built statement int() anywhere. Thanks in advance

31st Oct 2019, 8:36 PM
Hugo Bustamante
Hugo Bustamante - avatar
3 Respuestas
+ 2
This function will work correctly if it receives a list of numbers (or digits) for example: has_33([1, 3, 3, 7]) if you give a single number or a string, it won't work.
31st Oct 2019, 8:49 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Tibor, thanks for pointing that, i hadn't thought about it, but that do explains the reason of it functionning with an integer in this case.
31st Oct 2019, 8:59 PM
Hugo Bustamante
Hugo Bustamante - avatar
0
'range' returns a list of int (the iterator) , and 'for' will get the 'int' value of the list from the iterator.
1st Nov 2019, 1:35 AM
o.gak
o.gak - avatar