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
4 odpowiedzi
+ 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.
+ 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.
0
'range' returns a list of int (the iterator)
, and 'for' will get the 'int' value of the list from the iterator.