Skip to main content

Python | Data Structures or Data Types (List)

Data Type of Python


In Python there are two types of data types which are Mutable and Immutable. Mutable data type contains List and Dictionary data types. And Immutable data type contains String,Tuple and Sets data types.
Now we see what is Mutable and Immutable data type? The main object get changed permanently after applying methods of data type this data type is called as Mutable data type. And the main object will not be changed permanently after applying methods of data type this data type is called as Immutable data types.
Now we see data types one by one!

1) List - List is an ordered collection of mutable datatype. Lists are hashable, iterable. List contain any kind of data. List are declared in square braces[].
  • Empty List- 
    L = [ ]
  • List with multiple elements - 
    L1 = [10, 15.10, 'Python', [10,20] ]
    In the above list you can add any data type as shown above. In list L1 there is Integer, float,String,list data types. To add string data type you must have to declared in single or double quotes.
  • Hashable- 
    print (L1[2]) => 'Python'
    In the above example it returns 3rd element of list. In the above example we entered index number 2 in the square braces. To know more about the example please refer blow table. 

     0
    3
     10
    15.10 
    'Python' 
    [10,20] 
     -4
     -3
     -2
     -1
     
    We will see another example
    print(L1[0]) => 10
    print(L1[0:3]) => [10, 15.10, 'Python' ]
    Now in this example it will print from index number 0 to index number 2. It will return first three elements of list L1.
  • Iterable-
    for i in L1:
           print(i)
    It will return => 10 
                         => 15.10
                         => 'Python'
                         => [10,20]
  • Membership-
    print(10 in L1) => True
    print(20 in L1) => False
    print(20 not in L1) => True
Methods of List:-
  1. append(obj) - It append and object to the end of the list.
    For example:-
    L = [ 1, 2, 3 ]
    L.append(4) => [ 1, 2, 3, 4]
    L.append('Hello') => [ 1, 2, 3, 4, 'Hello' ]

  2. clear():- It removes all elements from list.
    For example:-
    L=[ 1, 2, 3, 4, 'Hello' ]
    L.clear() => []
    When you apply clear method to list only elements are deleted from list memory allocation is not remove.

  3. count(value):- It count number of occurrences of value in a list.
    For example:-
    L=[ 1, 2, 3, 4, 2, 3, 2, 1 ]
    print(L.count(3)) => 2 
    Here the value 3 is occur twice in the list.
    print(L.count(2)) => 3
    Here the value 2 is occur three times in the list. 

  4. index(value,start,stop):- It return first index of given value. It raises value error if the value is not present. The start and stop is for slice notation.
    For example:-
    L=[ 1, 2, 3, 4, 2, 3, 2, 1 ]
    print(L.index(3)) => 2
    In this example the value 3 is repeat twice but index method return the index number of first occurrence.
    print(L.index(3,3)) => 5
    In this example it will find the value 3 from the index number 3 not from the index number 0.
    print(L.index(1,2,5)) => valueError
    In this example the value 1 is not found between index number 2 and index number 5 so it will return value error.

  5. extend(iterable):- It extends the list by appending the elements from iterable. Each elements of an iterable is placed on individual indexes.
    For example:-
    L=[ 1, 2, 3 ]
    L.extend([ 4, 5, 7 ]) => [ 1, 2, 3, 4, 5, 7 ]
    L.extend( 'Hello' ) => [ 1, 2, 3, 4, 5, 7, 'H', 'e', 'l', 'l', 'o' ]

  6. insert(index,obj):- It insert an object at given index. The original value at that index is right shifted.
    For example:-
    L=[ 10, 20, 40, 60 ]
    L.insert( 2, 30 ) => [ 10, 20, 30, 40, 60 ] 
    In the above example we add 30 to the index number two but in original list have value 40 at the index number 2. When we perform insert method the original value of that index is shifted to right side.

  7. pop(index=-1):- It remove and return the item at given index. Default index is last index. It raises index error if index is out of range or list is empty.
    For example:-
    L=[ 10, 20, 30, 40, 50, 60 ]
    print(L.pop()) => 60
    print(L) => [ 10, 20, 30, 40, 50 ]
    print(L.pop(-2)) => 40
    print(L) =>[ 10, 20, 30, 50 ]

  8. remove(value):- It removes the first occurrence of given value. It raises value error if the value is not present.
    For example:-
    L=[ 10, 10, 20, 30, 20, 40, 30 ]
    L.remove(20) => print(L) => [ 10, 10, 30, 20, 40, 30]

  9. reverse():- It reverse a list by in place.
    For example:-
    L = [ 'Hi', 2, 5, 3, 9, 'Bye', 'World', 4, 2, 1 ]
    L.reverse() => [ 1, 2, 4, 'World', 'Bye', 9, 3, 5, 2, 'Hi' ]

  10. sort(key=none, reverse=false):- It sort the list in ascending order default. If we have multiple data types in a list we use key.
    For example:-
    L=[ 'Hi', 2, 5, 3, 9, 'Bye', 'World', 4, 2, 1 ]
    L.sort(key=str, reverse=True) => ['World', 'Hi', 'Bye', 9, 5, 4, 3, 2, 2, 1]

    L1=[ 10, 10, 20, 30, 20, 40, 30 ]
    L1.sort() => [ 10, 10, 20, 20, 30, 30, 40 ]

Comments

Popular posts from this blog

Basic Knowledge of Python part 2

Strings using python - Other than numbers python can also handle strings which can be expressed by several ways. In programming a string is a contiguous sequence of symbols or values such as a character string or digit string. Strings is declared in a '....' (single quotes) or "...." (double quotes).     By using  '\n'  you can insert the string to the new line by using  print()  command. For example:  We can concatenate string by using + operator. Concatenate means we can merge two or more separate strings by using + operator. For example: We see another example of multiple concatenate. Here 3*'py' means the string 'py' repeats 3 times and same for the 'thon' string. Now we another method to concatenate two or more strings. In this method you have write strings next to each other without any operator. For example: In the python string is stored in indexes. The index is start from 0 (zero). In python indexes also be calculated from reve...

Basic Knowledge of Python part 1

Basic Mathematics equations using Python- As we know python is a simple and easy to learn programming language. In this section we learn how to done mathematics equations using python. To perform this we use our basic operators +,-,/,* .  To perform python we have to open python interpreter. In this interpreter you can type simple equation like 5+5, 4/2 etc. For example: The integer number (e.g. 1,2,3....) have type int and the number with fractional part (e.g. 2.5,2.1,4.5...) have type float. Note:-  The division type always returns float. To get only an integer result of division you can use the // operator and to get remainder from division result you can use % operator. In python you can easily calculate power of the number by using ** operator. Like this you can easily calculate mathematics equation. Now we see how to find area of rectangle. Area = length*width is the formula to find area of rectangle. Here '=' sign is used to store value to the variable. You can als...