Python-Introduction

Python Introduction

Python is a pure object oriented language created by guido van rossum. It was released in February 1991. Python is a open source language, you can even add new features to the python source code. It is a beginers language anyone can start coding with python. As it is a interpreter oriented language it executes line by line and interrupts execution at the fisrt error occurence. Python latest version is 3.7

Why Python is so powerful?

  1. Open Source
  2. Broad range of libraries
  3. Portable
  4. Extention
  5. high-level
  6. object oriented language
  7. scalable
  8. interpreted

Application of Python?

  1. Web Application
  2. Scientific and Numeric Computing
  3. Image Processing
  4. Data Science
  5. I, Machine Learning, Deep Learning
  6. Robotics
  7. Game Desigining
  8. I.S

Is python Procedural/Object Oriented language?

            It supports both procedural oriented programming as well as object-oriented programming. As python is developed from different languages, it takes functional oriented features from C language, object-oriented features from C++, scripting from Perl & shell script, Modular programming from module-3 and syntax from C, ABC languages.

             In python, the most common tasks are grouped into functions. Python also consists of a number of predefined functions. So it is considered a procedural oriented programming language.

 Eg:- type(),len(), max(),print()…

 

            On the other hand, everything in python is a class or an object of that class, inheriting the class properties. So python is a object-oriented language.

Eg:- >>> a=10

        >>>type(a)

       <class ‘int’>

      Variables and Datatypes

            Python is a dynamically typed/ type-inferred languages. It means, there is no need  to declare the datatype of a variable before it is used.The variables can be assigned directly with any value. Python interpreter recognises the datatype of the variable based on the value assigned to it. Their is no limit/range for any datatype in python. To know the datatype of a variable, we can use the type () function. 

Eg:-

       >>> s=’supernatural’

      >>> type(s)

      <class ‘str’>

Rules to declare variable/identifiers:

            The names given for function, class and modules is called identifieres. The following rules must follow to declare variable/identifiers are:

  1. It should start with A-Z or a-z or uderscore(_) and can followed by 0-9
  2. Keyword should not be used
  3. Special characters, punctuation marks should not be used

Eg:-

            >>>_a=500 (#valid)

            >>> a  b=500(#invalid)

            >>> 1f=500(#invalid)

            >>>a6gf=500(#valid)

Datatypes:

            In python their are six major datatypes 

  1. Number
  2. String
  3. List
  4. Tuple
  5. Set
  6. Dictionary

Every datatype is a class in python, It contains data and methods. When a variable is initialised with a value the datatype of the variable is automatically detected  and the variable becomes an object of that particular class. It can access the data and method of that class.

These datatypes are divided into two different categories

  1. immutable datatypes
  2. mutable datatypes

Immutable Datatypes:

            Data cannot be modified for immutable datatypes. Numbers,Strings and  Tuples are immutable datatypes

Mutable Datatypes:

            Data can be modified for mutable datatypes. Lists,Sets, and Dictionaries will come under these categories

List

           list is a sequence datatype in python. It is mutable datatype. List is declared by enclosing the elements in sqaure brackets []  and seperated by commas(,). It will support heterogeneous data.

 

>>> L=[1,2,5,7,10]

>>>type(L)

<class ‘list’>

>>>L=[ 5, ‘python’, 4.5, 4+5j, [1,3,5,8]]

List Sclicing:

            The clicing operator [] is used to access the elements of the list using the index of the list. The index of the list start with the zero and ends with len(list)-1.  len()  function returns length of the list.  

>>>L[1]

‘python’

>>>L[4] [1,3,5,8]

            The elements in the list can also be accessed by using the reversed indexing. Reverse indexing start with -1 from the ending of the list to -len(list)

>>>L[-1] [1,3,5,8]

>>>L[-3]

4.5

Range Slicing:

            The range slicing operator [:] is used to access a sublist from the given list [start: end].

The returned sublist starts with the index position start to the index position-1.

 >>> L[0:3] [5, ‘python’, 4.5]

>>>L[-3 : -1] [4.5, (4+5j)]

If starting index postion is not assigned then it will return from the starting of the list to the specified end position.

>>> L[:2]

 [5, ‘python’]

>>>L[-3] [5, ‘python’]

If the ending position is not specified then the list will be returned from the given starting position to the end of the list.

>>>L[2:] [5, ‘python’]

The list L contains string and list as elements the are sequence datatypes. We can access those individual elements also.

>>> L[1][ : -3]

‘pyt’

>>> L[-1][-1]

8

>>>L[-1][-2:] [5, 8]

*Note: List will take three parameters as input one is start, stop and step(optional).

>>> L[ : : -1] [[1, 3, 5, 8], (4+5j), 4.5, ‘python’, 5]

>>> L[0 : 3: -1] []

>>> L[ 3 : 0 : -1] [(4+5j), 4.5, ‘python’]

>>> L[-1 : -5 : -1] [[1, 3, 5, 8], (4+5j), 4.5, ‘python’]

>>>

List Methods:

            By default list class contains some methods we can access these methods by creating list variable. This variable acts as list class object / instance. To access list methods we need call using.

 object_name.method_name()

>>> L=[ 5, ‘python’, 4.5, 4+5j, [1,3,5,8]]  #L is list class object

  1. Append Method:

            append method will add the elements/object to the existing list at last position.

>>> L1=[2,35,6]

>>> L.append(L1) # L1 is other list object

>>> L

[5, ‘python’, 4.5, (4+5j), [1, 3, 5, 8], [2, 35, 6]]

>>> L.append(‘india’) # directly we can add elements in this way

>>> L

[5, ‘python’, 4.5, (4+5j), [1, 3, 5, 8], [2, 35, 6], ‘india’]

>>> 

2, Copy Method:

            copy method will copy the element of one list to other list but their reference will be different.

>>> L3=L.copy()

>>> L

[5, ‘python’, 4.5, (4+5j), [1, 3, 5, 8], [2, 35, 6], ‘india’]

>>> L3

[5, ‘python’, 4.5, (4+5j), [1, 3, 5, 8], [2, 35, 6], ‘india’]

>>> L.append(‘Jhon’) #if we add new element to list L it will not change in L3 as their reference is differ

>>> L

[5, ‘python’, 4.5, (4+5j), [1, 3, 5, 8], [2, 35, 6], ‘india’, ‘Jhon’]

>>> L3

[5, ‘python’, 4.5, (4+5j), [1, 3, 5, 8], [2, 35, 6], ‘india’]

*To check which memory location is pointing by a variable using  id(variable).

>>> id(L)

140081661061832

>>> id(L3)

140081632587016

>>> L2=L #We can directly assign  in this format but it will point to one reference

>>> id(L)

140081661061832

>>> id(L2)

140081661061832

*If any changes performed on list L it will reflect on list L2 also.

>>> L.append(100000)

>>> L

[5, ‘python’, 4.5, (4+5j), [1, 3, 5, 8], [2, 35, 6], ‘india’, ‘Jhon’, 100000]

>>> L2

[5, ‘python’, 4.5, (4+5j), [1, 3, 5, 8], [2, 35, 6], ‘india’, ‘Jhon’, 100000]

>>>

  1. Count Method:

            if will count how many time particular element is exist in list L

>>> L.count(5)

1

  1. Clear Method:

            clear method will clears the entire list elements

>>>L.clear()

[]

 Extend Method:

            It will extends list by appending elements from the iterable.

>>> L=[1,3,4]

>>> L1=[5,6,1]

>>> L.extend(L1)

>>> L

[1, 3, 4, 5, 6, 1]

      6.Index Method:

            Index method will returns index of the first occurence of  particular element. If element not found in list it will rise an exception.

>>> L.index( 1 )  # element 1 is present in two index postions i.e 0,5

0

>>> L.index(10)         # element 10 is not present in list L

Traceback (most recent call last):

  File “<pyshell#4>”, line 1, in <module>

    L.index(10)

ValueError: 10 is not in list

  1. Insert Method:

            This method will take two parameters, one is index position and other is element. If index is not in range it will place element at the end of the list.

>>> L

[1, 3, 4, 5, 6, 1]

>>> L.insert( 2, 55)

>>> L

[1, 3, 55, 4, 5, 6, 1]

>>> L.insert( 100, 55)

>>> L

[1, 3, 55, 4, 5, 6, 1, 55]

>>> L.insert( -4, 55)

>>> L

[1, 3, 55, 4, 55, 5, 6, 1, 55]

 

  1. Pop Method:

            pop method removes and returns the last item from the list. It takes an optional parameter which is index position of the element and removes from the list.

>>> L.pop()

55

>>> L

[1, 3, 55, 4, 55, 5, 6, 1]

>>> L.pop(2)

55

>>> L

[1, 3, 4, 55, 5, 6, 1]

>>> L.pop(99)  # index 99 doesn’t exists list L

Traceback (most recent call last):

  File “<pyshell#23>”, line 1, in <module>

    L.pop(99)

IndexError: pop index out of range

>>>

9.R emove Method:

            this method take ean lement from the list as an argument and removes the first occurence of that element from the list. It raises an exception if the element is not present in the list.

>>> L

[1, 3, 4, 55, 5, 6, 1]

>>> L.remove( 1 )

>>> L

[3, 4, 55, 5, 6, 1]

>>>

  1. Reverse Method: This method reverses the elements of the list in the place.

>>> L.reverse()

>>> L

[1, 6, 5, 55, 4, 3]

>>>  

  1. Sort Method:

            This method sorts the elements of the list in ascending order if all the elements belong to the same datatype. If elements are of different datatypes, it raises an exception.

>>> L

[1, 6, 5, 55, 4, 3]

>>> L.sort()

>>> L

[1, 3, 4, 5, 6, 55]

>>> L2=[ ‘a’ ,1, 4, 5, 44.0]

>>> L2.sort()

Traceback (most recent call last):

  File “<pyshell#32>”, line 1, in <module>

    L2.sort()

TypeError: unorderable types: int() < str()

 

            It take’s two optional parameters, one is key and other one is reverese. Key parameter takes a function and sorts elements in the list based on that function eg:- len,min,max.

            Reverse parameter takes boolean values True and False, By default this parameter is set to False so elements are sorted in ascending order. If parameter is set to True the elements are sorted in descending order.

 

 >>> L3=[ ‘python’ , ‘java’ , ‘C’ , ‘pascal’ , ‘CPP’ , ‘programming’ ]

>>> L3.sort()

>>> L3

[ ‘C’ , ‘CPP’, ‘java’ , ‘pascal’ , ‘programming’ , ‘python’ ]

>>> L3.sort(reverse=True)

>>> L3

[ ‘python’ , ‘programming’ , ‘pascal’ ,  ‘java’ , ‘CPP’ , ‘C’ ]

>>> L3.sort( key=len, reverse=True)

>>> L3

[ ‘programming’ ,  ‘python’ ,  ‘pascal’,  ‘java’ , ‘CPP’ , ‘C’]

>>>