Python Technical Paper (Arrays and Strings)
Published: 2023-02-16 07:10:50

## Paper by Tushar Singh (susst94@gmail.com)

### Introduction:

As programmers, we always here this fact that everything in python is an "object" including and not limited to functions as well. This very fact becomes the backbone and the basis for how the python libraries and definitions (in-built and user-defined) are stored. This idea is what will be demonstrated with the help of various examples. Another thing to note is the concept of dealing with mutable and immutable objects and any operations surrounding those objects. List objects are said to be "mutable" and strings are treated as "immutable". This distinction has lot of implications in the usage of these types of data storage elements. Following section of this paper will dive into the basic idea, setup, nuances along with demonstrations for both objects.

### Mutable / Immutable:

Before going into the set of tools that python can provide to programmers for working with lists and string data types, it is very important to understand how referencing works in python.

Consider the following code snippet and how it functions.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nthhzbxbm3w6uam7cxcx.png)

We often say that strings and int data types are immutable and a nice way to understand the difference between the two is with the help of the "id" function. This function returns the memory location wherein our data is being stored. When we declare a variable by the name "x" and assign it an integer value, the python interpreter allocates space in memory and stores the starting value 35. Now suppose we were to change the value stored in the variable x to something different say 36, what will happen? Well as you can see with the help of "id", we notice that python allocated a separate memory in space for storing the value 36 in the variable named "x". We can also say that Python decided to create a new object and store the value 36 and assigned the name "x" to the object.

Another angle of looking at immutability is with the help of the next example with the string value "Test". We declare 2 objects named 'a' and 'b' and assign the value "Test". Upon checking the id for both objects, we see that both have the same id.

Consider this snippet now and notice the "id".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5b6nwrq2ypxcvn22p12.png)

Here we are declaring the same list, like with strings, and checking their id to see if anything is different. And of course it is different. This difference in "id" even when passing the same list to both object variables is the basis of all the differences in terms of mutability and immutability and extends to method executions between strings and lists.

## List Methods

We shall now see some of the more commonly used list methods that programmers like to use on a consistent basis. The general syntax skeleton for all list methods follows the format:

```
list_name.method_name(arguments)
```

* append()
  
  This method is used to add elements to the end sequence of the list. Refer to the image to understand how append works.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uf7ggogbyasm46yur7z2.png)

* insert()

    This method operates like append, except the user has the freedom to add value based on which index he wishes to insert the value in.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sx2i8eixohje0s7x17lj.png)

Here we are inserting the element 6 in our list at index value 2.

* pop()

    This method is used to return and remove elements from a list based on what index argument is provided. Stress on the word return here, since in some cases the user might want to retain what element it was that removed during the pop operation.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zexdmottsrgzf8yx8pbz.png)

Notice how pop function alters its behavior based on when an index argument is provided or not.

* reverse()

    This method is used to reverse the order of the elements in a list.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12hl55hvurserue7yr91.png)

Notice how the id through all the operations remains the same since the operations are being performed on the same object.

* sort()

    This method is used for performing in-place(within the object memory location) sorting of elements of a list based on the argument list provided.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jsh101kppyl03lw64k68.png)

The argument list allows the user to either revere sort the list or set other criteria for sorting with the help of the keyword "key".

* index()

    This method is used for detecting and returning the index of first occurrence of the first argument in the method syntax notation with the starting search position as argument 2 if provided or from index = 0 if not provided.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f6eqhbrz08qkf3ngzacd.png)

Notice how in the first case upon providing the start position as index 5 for search element 3 in the list, we get a ValueError stating that element 3 is not present in the list after index 5.

* count()
  
    This method is used for finding the total count of an occurrence of an element in a list based on the argument provided.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2srjatrguk9d2wmmeg5i.png)


## String Methods:

We shall now see some of the more commonly used string methods that programmers like to use on a consistent basis.

* startswith()

    This method is used to return if a string starts with a certain specific string sequence which is provided as argument to test along with starting position and end position if present. The default value for starting position is 0.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ve4y0aq1vwidv8sxv7eh.png)

* endswith()

    This method is used to return if a string ends with a certain specific string sequence which is provided as argument to test along with starting position and end position if present. The default value for starting position is last position of the string.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzhexjoe2222pe2uke4p.png)

* count()

    This method is used to return the total count of a character in a string which is provided as argument to test along with starting position and end position if present. The default value for starting position is 0.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzobim8nwytt0kdvx1jk.png)

* index()

    This method is used to return the index of first occurrence of a character in a string which is provided as argument to test along with starting position and end position if present. The default value for starting position is 0.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/429wijz1ci1zutlvsc35.png)

* isalnum()
  
    This method is used to return presence of alphanumeric characters in a string.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pr0a2v04dilloclbizrd.png)

* isdigit()

    This method is used to return presence of only numeric characters in the complete string.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fo3qiegnrmanzyn2xxw.png)

* isupper()

    This method is used to return presence of upper-case characters in a string.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/293b9vvd20a8r9kx7hw6.png)

* islower()

    This method is used to return presence of lower-case characters in a string.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2eu78p8cs9vy8rhmj16t.png)

* split()

    This method is used to return an iterable data based on the delimiter argument provided. The default delimiter is space (' ').


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shqilsj9l0lub5hz66v5.png)

## Conclusion:
I hope that by the end of this, you not only get well acquainted with how the methods function but also understand why they are structured the way they are based on if you are dealing with a mutable or immutable data type.