Saturday, 3 February 2018

iv)List :
      List is the best of the data types in python3.
      It can hold anything(Literally anything).
      Syntax :
             There are two ways you can declare an empty list:
                          list() or []

            >>> type(list())
    <class 'list'>
    >>> type([])
    <class 'list'>
>>> [1,2,"string", """ multiline \n comment """, 12.564]
[1, 2, 'string', ' multiline \n comment ', 12.564]

>>> a = [2, 5, 45.74, "python"]
>>> a[0]
2
>>> a[3]
'Python'
Python has a special provision for accessing elements from last in case the length of list is unknown.
You can access the last element by using negative indexing.
List[-n] returns nth element counting reverse , starting from the last in the List  .
>>> a[-1]
'Python'
>>> a[-3]
5
Checking length(number of elements) of list :
    len(list) is the method used to find.
        >>> len(a)
4
 
Slicing in list :
    Slicing is a process of selection of elements by using indexes.
    List[a:b] will return list of elements in List from index a to b
        Excluding element at bth position. b>=a
    If a/b parameters are not provided then python takes extreme
        Position.
        For example :
>>> a[2:4]
[45.74, 'python']
>>> a[2:1]
[]
>>> a[3:]
['python']
>>> a[:3]
[2, 5, 45.74]
>>> a[:]
[2, 5, 45.74, 'python']
Slicing with negative index :
>>> a[:-1]
[2, 5, 45.74]
>>> a[:-2]
[2, 5]
a[-x ] is same as a[len(a) - x ]
>>> a[:-1]
[2, 5, 45.74]
>>> a[:-2]
[2, 5]

Reversing a list :
>>> a[::1]
[2, 5, 45.74, 'python']
    reversed(x) can be used to reverse a list.
    Rerversed function returns a reverseiterator . So, it has
        to be type casted to list.
>>> list (reversed(a))
['python', 45.74, 5, 2]

Counting occurrences of certain element in a list.
Syntax : list.count(element_to_find_frequency_of)
Example :
>>> [1, 1, 2, 1, 2, 5, 8 ,6, ""].count(1)
3
Sorting a list :
    sort attribute can be used to sort a list.
    To use sort attribute, list cannot have any other type other than
        that of int and float.
    sort attribute does not return anything but performs sorting on
        the list.
>>> a = [1, 2, 5, 4, 3]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5]

Pop attribute of list :
Syntax : list.pop(index)
             Where, index is index of element you want to remove
             from the list.
This attribute returns the deleted element.
    [1, 3.23, 4, 5]
>>> a.pop(1)
3.23
>>> a
[1, 4, 5]
Copying a list :
    Suppose a=[1,2,3] is a list.
    b = a will give reference of a to b, not copying the list.
    Now, what does that mean??
    That means, when you will modify elements of a, elements of
        b will also be affected and vice versa.

    For copying the elements of a list to another, copy attribute is
        Used.
    b = a.copy()
    Now, elements in b will have elements same as a but
       modifying a or b won’t affect the other list.

Append attribute :
Append takes exactly one argument as an input that will
include the argument passed inside the attribute in the list.
>>> a
[1, 45, 3, 4, 5]
>>> a.append([1,2,3])
>>> a
[1, 45, 3, 4, 5, [1, 2, 3]]

Here, i append a list [1, 2, 3] to the list a.

Removing all the elements :
clear attribute is used.
        >>> a.clear()
>>> a
[]

List can be merged with another list using extend attribute of the list
list.extend(list1)
>>> a = [1, 2, 3, 5]
>>> a.extend([4, 6])
>>> a
[1, 2, 3, 5, 4, 6]
This is one of the way you can add a list to another list.
Another way is :
list = list + list1
>>> a = a + [9, 7, 8]
>>> a
[1, 2, 3, 5, 4, 6, 9, 7, 8]
These were some of the  attributes and functionalities you can do using list. 
More properties we will see in future posts.

Before starting with our next data Type, Let’s first know about variables in python:
Variables :
    These are any names which can be assigned any data type:
Python has no restrictions on use of variables, We don’t need
any explicit declaration of type of variable. Python assigns
data type of the variable by the data it holds.
Confused, correct?? Let’s take an example:
    >>> a = 3
Is a variable declaration where, a is  variable name and 3 is an
int data type assigned to it.
>>> type(a)
<class 'int'>
You can overwrite same  variable to hold value of same or different data type.
    >>> a = 32
>>> a = 45
>>> a
45
>>> a = "string"
>>> a
'string'
>>> type(a)
<class 'str'>

Using variable to access the data it is storing.
>>> a = 23
>>> b = 3
>>> a+b
26
>>> a**b
12167
One of the best things what python provides is simultaneous
assignment of variables.
For example :
>>> a,b = 12,"string"
>>> a
12
>>> b
'string'

>>> a, b, c = 2,1,    9
>>> a+b+c
12
iii)String:
     
      String can be defined in three ways :
      using single quotes, double quotes, triple single quotes or Double single
      quotes.

      "String_name" or 'string_name'  are the basic way you can define a string.
      A special provision for writing a multiline string is provided by python
      using three quotes:
      For example : """ I am a multiline comment
                                     Which can be extended for any number of lines
                                              .............
                                              .............
                                      String ends here """
      Or, instead of  """ comment """",  '''  comment  '''    can be used.
       Note : Combination  of quotes cannot be used. Either of the quotes 
type has to be used.

       By default, the string quotes used by python is ' '.
          >>> "python3"
          'python3'
          >>> type(""" I am a
          ... multiline comment
          ... """)
          <class 'str'>

          >>> "hello "+"world"
          'hello world'
      Here, what interpreter has done is not addition of strings but
concatenation of strings. 
Concatenation is appending of one string at the last of another
      string.

      String repetition:
          >>> " I am repeating"*3
          ' I am repeating I am repeating I am repeating'

Memoization : Memoization is a technique for remembering the results that incur huge costs to running time to the algorithm. Memoizati...