Data Structures In Python

Data Structures In Python

Data structures are containers that organize and group data types together in different ways.

LIST : A list is one of the most common and basic data structure in python, which is said to be data type for mutable ordered sequence of element.

example;

days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
print(days)

A list can contain any mix and match of the data types.

list_of_random_things = [1, 2, 3, 'a string' True]

Slices & Dice with List: When using slicing it is important to remember that the lower index is inclusive and upper is inclusive.

Therefore,

list_of_random_things = [1, 2, 3, 'a string' True]
list_of_random_things = [1:2]

output

[2, 3]

will only return different 2 3 in a list. Notice this is still different than just indexing a single element because you get a list back with this indexing. The colon tells us to go from starting value on the left of the colon up to but not including the element on the right. If per adventure you know that you want to start at the beginning of the list, you can also let out this value.

>>> list_of_random_things = [1, 2, 3, 'a string' True]
>>> list_of_random_things [ :2]

[1, 2, 3]

USEFUL FUNCTION IN LIST

  1. len() returns how many elements are in a list.
  2. max() returns the greatest element of the list. How the greatest element is determined depends onwhat type objects are in the list. The maximum element in a list of numbers is the largest number. The maximum elements in a list of strings is element that would occur last if the list were sorted alphabetically. This works because the the max function is defined in terms of the greater than comparison operator. The max function is undefined for lists that contain elements from different, incomparable types.
  3. min() returns the smallest element in a list. min is the opposite of max, which returns the largest element in a list.
  4. sorted() returns a copy of a list in order from smallest to largest, leaving the list unchanged.

TURPLES

A tuple is another useful container. It's a data type for immutable ordered sequences of elements. They are often used to store related pieces of information.

Example

location = (13.4125, 103.866667)
print("Latitude:", location[0])
print("Longitude:", location[1])

Tuples are similar to lists in that they store an ordered collection of objects which can be accessed by their indices. Unlike lists, however, tuples are immutable - you can't add and remove items from tuples, or sort them in place.

Tuples can also be used to assign multiple variables in a compact way.

dimensions = 52, 40, 100
length, width, height = dimensions
print("The dimensions are {} x {} x {}".format(length, width, height))

The parentheses are optional when defining tuples, and programmers frequently omit them if parentheses don't clarify the code.

In the second line, three variables are assigned from the content of the tuple dimensions. This is called tuple unpacking. You can use tuple unpacking to assign the information from a tuple into multiple variables without having to access them one by one and make multiple assignment statements.

If we won't need to use dimensions directly, we could shorten those two lines of code into a single line that assigns three variables in one go!

length, width, height = 52, 40, 100
print("The dimensions are {} x {} x {}".format(length, width, height))