DEV Community

Tushar Singh
Tushar Singh

Posted on

Python Technical Paper (Arrays and Strings)

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

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

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)
Enter fullscreen mode Exit fullscreen mode
  • 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

  • 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

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

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

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

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

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

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

  • 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

  • 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

  • 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

  • isalnum()

    This method is used to return presence of alphanumeric characters in a string.

Image description

  • isdigit()

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

Image description

  • isupper()

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

Image description

  • islower()

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

Image description

  • split()

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

Image description

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.

Top comments (0)