+ 1

Is this an array or list?

When you have a variable like : Leds = [4,17,22,11] Is this an array or list?

7th Dec 2018, 8:07 PM
Sahan Edirisooriya
Sahan Edirisooriya - avatar
9 odpowiedzi
+ 13
By default, Python does not support arrays (as they are understood in other languages) using lists instead. Python's numpy module introduces arrays treated like matrices (or tensors in general). You can do linear algebraic operations in them as if they were matrices, indeed.
7th Dec 2018, 8:23 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
Good luck with the Pi, by the way ;)
8th Dec 2018, 7:40 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
It's a list in python.
7th Dec 2018, 8:20 PM
Arushi Singhania
Arushi Singhania - avatar
+ 4
Basically in this case, you have a range consisting of numbers 0,1,2,3,4 and 5. It's not an array but here i is acting as the index number for accessing values from list leds.
7th Dec 2018, 9:00 PM
Arushi Singhania
Arushi Singhania - avatar
+ 2
thanks
9th Dec 2018, 10:07 AM
Sahan Edirisooriya
Sahan Edirisooriya - avatar
0
thanks guys
7th Dec 2018, 8:34 PM
Sahan Edirisooriya
Sahan Edirisooriya - avatar
0
and how do i explain ; for i in range(0,6) GpIo.output(leds[i], false)
7th Dec 2018, 8:35 PM
Sahan Edirisooriya
Sahan Edirisooriya - avatar
0
the [ i] is related to an array?
7th Dec 2018, 8:35 PM
Sahan Edirisooriya
Sahan Edirisooriya - avatar
0
[i] in leds[i] is not an array or similar, it is an index, many types of lists use indexing: strings, lists, tuples, dictionaries. By more complex answer each type of object has a class. To make an object indexable, you need to define atleast 2 functions called magic methods, __init__ and __index__. __init__ is ran when you use the class name as function and it is used to create objects. __index__ is ran when you use the index [i]. When you use leds[i] you pass 2 arguments to the __index__ method, the list object leds and the index i. This also allows programmer to make new list types. You often have 2 ways to create any builtin list types in python. Such as lists have: [x, y, z] list([x, y, z]) Because there was already created a list in list(), it is not best way to make lists, but it can be used to convert other types to lists. Even those that can't be indexed.
27th Dec 2018, 7:05 PM
Seb TheS
Seb TheS - avatar