Arrays in Python

walden systems, geeks corner, programming, languages, Python, scope, local scope, global scope, hoisting, functions, variables, developer, scripting, decrypt, aes256, encrypt, pycrypto, pip, tuple, dictionary, list, file, read, write, readline
Python is a programming language that lets you work quickly and integrate systems more effectively.

An array is a data structure that contains a group of elements. Usually, the elements are all of the same data type, such as an integer or string. In some programming languages such as Python and PHP, arrays can hold any value type. In other languages such as Java, the data type has to be declared when declaring the array. Another twist is that Python doesn't natively support the traditional definition of an array and instead, uses lists. In PHP, arrays are treated as ordered maps. An ordered map is a key value pair .

In Python, we don't have to declare the type of data held in the array since it is a loosely typed language. This holds true for PHP also. In Python and PHP, we can hold any combination of value types in the array. In strongly typed languages such as Java, the value data type must be defined when declaring an array. Another difference between how Python treats arrays and how PHP treats arrays are that in PHP, you can define the index ( or key ). In PHP keys can be either string or integer types. In PHP, if a key is not provided, it will give an ascending integer as the keys to the element. Below are the differences in how arrays are declared in each language.

Python

    array1 = ["a","b","c"]
    array2 = ["Mary", 2, "lamb"]


PHP
    $array1 = array(
        1 => "a",
        2 => "b",
    ) ;

    $array2 = array["Mary",2,"lamb");


Java
    int myArray[] = new int[2] ;
    myArray[0] = 1 ;
    myArray[1] = 2 ;


As we can see from above, in Java, the data type and size of the array must be declared. In PHP and Python, we don't have to declare the size nor the data type, in fact, we can have the elements hold any data type we desire. Also note that in Java, an array must be instanced, as we can see by the "new" keyword, this is not necessary in PHP or Python. When it comes to accessing elements in the array, PHP, Python and Java have their starting index with "0." In PHP, since arrays are ordered maps, we can have the index start at another number or a key such as "foo."

Python

1    myArray = ["a","b","c"]
2    myArray2 = ["Mary", 2, "lamb"]
3
4    # foo1 will hold "a"
5    foo1 = myArray[0]
6
7    # foo 2 will hold 2
5    foo2 = myArray2[1]


PHP
1    $array1 = array(
2        1 => "a",
3        2 => "b",
4    ) ;
5
6    $array2 = array["Mary",2,"lamb");
7
9   $array3 = array(
10       "name" => "John",
11       "age" => 36,
12   ) ;
13
14   # foo1 will hold "Mary"
15   $foo1 = $array2[0] ;
16
17   # foo2 will hold "a"
18   $foo2 = $array1[1];
19
20   # foo3 will hold 36
21   $foo3 = $array3["age"] ;

In Python and PHP, we can add and remove elements to an array. In Java, since the size of the array is set, in order to add to an array, we have to create a new, larger array and vice versa to remove items from an array. In Python, we have several different methods to remove items from an array: remove(), pop() and clear(). PHP also has several methods to remove items from an array such as array_splice() and array_pop(). Since the size of the array is fixed in Java, there is no way to decrease or increase the size of the array.

Python

1    myArray = ["a","b","c"]
2    myArray2 = ["Mary", 2, "lamb"]
3
4    # myArray will hold ["b","c"]
5    myArray.pop(0)
6
7    # myArray2 will hold ["Mary",2]
8    myArray2.remove("lamb")
9
10   #myArray2 will be empty
11   myArray2.clear()


PHP
1    $array1 = array(
2        1 => "a",
3        2 => "b",
4    ) ;
5
6    $array2 = array["Mary",2,"lamb");
7
9    $array3 = array(
10       "name" => "John",
11       "age" => 36,
12       "address" => "any town",
13   ) ;
14
15   # array1 will hold ["a"]
16   array_pop($array1) ;
17
18   # array2 will hold ["Mary","lamb"]
19   unset($array2[1]);
20
21   # array will hold ["John","any town"
22   array_splice($array3, 1, 1);

If we think about arrays as a collection of objects, it makes things easier to when migrating to a different language. Even though some languages such as PHP and Python doesn't natively support arrays, we can still use the concept of arrays in these languages if we think of arrays as just a collection of objects. The implementations between languages may not be the same we can get the same things done in different languages.