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 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...

Python | Data Structure or Data Types (String)

Data Type Of Python I n the last session we discus about what is the data type of python. And we see List data type. In this session we s ee the next data type String.     2) String- String is a ordered collection of immutable data type. String are iterable and hashable. Strings are always declared in single or double quotes.  Single line string:- S = "Python" Multi line string:- S = """ Line 1             Line 2             Line 3 """ Iterable:- S= "Program" for i in S:       print(i) Output:-  P  r  o  g  r  a  m Membership:-  print( 'p' in S) => True print( 'P' not in S) => False Methods of string:- capitalize() :-  It return the capitalize version of the string. More specifically it make the first character have uppercase and rest lowercase. For example:- S = "hello World" print(S.capitalize()) => "Hello world" count(substring, start,en...

Python | Data Structure Or Data Type (Tuple)

Data Type Of Python  In this session we will see last data type of python which is Tuple. 5) Tuple:- Tuple is a ordered collection of immutable datatype, tuples are quite similar to lists but difference is list are mutable where as tuple are immutable. Empty tuple:-  t = () or tuple() Tuple with single element:- t = (10,) Tuple with multiple element:- t1 = ( 10, 20, 30, 'Hello' ) Nested tuple:-  t1 = ( 10, 20, 30, 'Hello' ) p = t2, 30, 40, 50 print(p) => ((10, 20, 30, 'Hello'), 30, 40, 50) Methods of Tuple:-   count(value):- It return the count of value if the value is present in the tuple else it return 0. For example:- t = (10, 20, 30, 'Hello') t.count(10) => 1  t.count(50) => 0 index(value,start,end):- It return the index number if the value is present in the tuple, otherwise it raised value error. For example:- t = (10, 20, 30, 'Hello') t.index(10) => 0 It return the index number of value 10. Prev Topic   Next Topic