Day 13 - Data Types in Python - Part 1

Day 13 - Data Types in Python - Part 1

As we have seen various data types in previous blog. We will look in details about Sequenced Data Type.

LISTS

Lists are an ordered and mutable collection of items separated by comma and enclosed within square brackets [ ].

They can store multiple items in a single variable.

We can add different data types to our single list.

Accessing List Items:

There are 2 ways, we can access the list:

  1. Positive Indexing: Starts with [0] index from first.

  2. Negative Indexing: Starts with [-1] index from last.

    However, we can convert the negative index to the positive index by subtracting the given index with the length of the list given.

    In the below screenshot, you can see that:
    Length of list, len(colors)= 5

    index we are taking = -1
    So to change negarive index to positive index -> len(list)-index
    print(colors[len(colors)-1]) -> print(colors[5-1]) -> print(colors[4]) -> Purple

List Methods

There are various list methods available:

  • append: to add elements at the end of the list.

  • sort: sorts the list in ascending order.

    You can also sort the list in descending order.

  • reverse: reverse the order of the list.

  • index: returns the index of the first occurrence of the list item.

  • count: returns the count of the number of items with the given value.

  • insert: inserts an item at the given index.

    Syntax: - insert(index, item)

  • extend: adds an entire list or other collection of data type(set, tuple, dict) to the existing list, at the end.

  • copy: returns the copy of the list.

    In the above snapshot, while using m=l.copy() , "l" didn't get change because "m" is the copy of "l" whereas using m=l , "l" got changed because "m" is the reference of "l".

  • concatenate: concatenate two lists to join them.

TUPLES

Tuples are an ordered and immutable collection of items separated by comma and enclosed within parentheses ().

Like List, We can add different data types to the single tuple.

Accessing Tuple Items

Indexing in tuple is same as the list. So, you can refer to the list indexing in the above section.

Just in case of list there is square brackets[ ] and in tuple there will be parentheses( ).

Tuple Methods

There are no direct methods to manipulate tuples.

As tuples are immutable, hence if we want to add, remove or change tuple items, then first we must convert that tuple to a list.
Then perform operation to the list and convert it back to the tuple.

countries = ("Spain", "India", "Germany", "Italy")
temp = list(countries) # converting to the list
temp.append("Russia") # adding an item
temp.pop(2) # removing an item
temp[2] = "Finland" # changing an item
countries = tuple(temp) # converting it back to tuple
print(countries)

o/p: ('Spain', 'India', 'Finland', 'Russia')

We can directly concatenate two tuples without converting them to list.

CONCLUSION

In Python, lists and tuples are both ordered collections that store elements. However, there are key differences between them:

List:

  1. Mutable: Lists can be modified after creation. Elements can be added, removed, or changed.

  2. Syntax: Defined using square brackets [].

  3. Use cases: Suitable for situations where the collection needs to be modified, such as adding or removing elements.

Tuple:

  1. Immutable: Tuples, once created, cannot be modified. Elements cannot be added, removed, or changed.

  2. Syntax: Defined using parentheses ().

  3. Use cases: Useful when a collection of elements should remain constant throughout the program, providing a form of data integrity.

In summary, choose a list when you need a mutable collection, and a tuple when you want an immutable collection.

👆The information presented above is based on my interpretation. Suggestions are always welcome.😊

~Smriti Sharma✌