Posts

Regular Expressions

                  Regular Expressions Regular expressions is the sequence of charcaters which are used to search for a pattern in a string. In python we have re module which can be used to perform regular expressions task easily. We need to import this module named re like this -  import re To find the any pattern in the given string we can use search() function which will give us the object and in that object contains the information about that searched patern. It will contain whether the patten has been found or not if yes then it contains the span of that pattern i.e. starting and ending index. Remember that it is case sensitive function.  print(re.search("pattern", "searching pattern in text") ) Output: <re.Match object; span=(10, 17), match='pattern'> Now we can use this object take out relevent information like start() function can be used to find the starting index in string where pattern has been found and end()...

NumPy

                    Numpy in Python NumPy means Numeric Python. It is a package in Python which deals with numeric computation and processing of arrays. NumPy makes easy for us to do the numeric computations. Numpy plays a important role in Machine Learning. There are many inbuilt functions in Numpy, you just need to know when and how to use them. Numpy deals with different ranges of int and float data types. Now lets see how to use NumPy and do the mathematical computations. Before using NumPy we have to import it. Generally we use alias while importing the library but it is only optional. Creating Arrays: import numpy as np a1 = np.array([2, 1, 3, 4])    # single dimension array print(a1) Output: [2  1  3  4] a2 = np.array([[1, 2, 1], [2, 1, 2], [1, 2, 3]])   # multi-dimension array print(a2) Output: [[1  2  1]                      ...

OOPs - Inheritance

               Inheritance in Python In OOPs when one class extends or inherit the properties of another class then it is called as Inheritance. The class from which we inherit the property is called Parent class and another one is called as child class. By property we means methods and data.  To inherit parent into child we pass the parent class as a parameter into the child class. Lets see how -  class Animal:          def walking():                    print("Animal walking") class Dog(Animal):          def speak():                    print("Barking") d = Dog() d.speak()      # Barking d.walking()    # Animal walking As we can see that we have not defined the walking function in Dog class but we can still use it because we inherited it from Animal class....

Python OOPs

  Object Oriented Programming in Python       Python is objected oriented programming. We can use classes and objects in Python. Class can be usderstood as a type of data structure which describes the properties and attributed of the object. Class is the blueprint of object. Object can be understood as the real world entity which has its own behaviour and state and instance of the class. Principles of OOP - Inheritance : In this concept we inherit the properties of one class to another. The class from which we inherit the property is called as parent class and other is child class. By inherit we means that we can use the property of one class to another. No need to write the code again. Polymorphism : Polymorphism means many forms. In this concept we means that we can have multiple functions of same name and can be differentiate on the basis of parameters. Encapsulation : In this concept, we encapsulate the methods and data together in a single unit. In class we...

Exception Handling in Python

                 Exception Handling Exceptions can be defined as the error which causes the interuption in the flow of program and program do not terminate normally. Exceptions are the run time errors but like any other advanced programming languages Python can also handle the exceptions. There can be different types of exeptions like - Arithmetic Exception Assertion Exception Import Exception Memory Exception Name Exception OS Exception etc... These main exceptions have also many types of exception subclasses. How to handle exceptions ? try-except :  In try block we write the code which is to be executed or attempted. If we have any code which can generate the exceptions then we place it to the try block. except block contains the code which is to be executed if there is any exception in the try block. E.g.    try:           a = 5/0      except Exception as e:     ...

Functions in Python

                       Functions A function is a code block which is used to do the specific task whenever it is called. Suppose you need to do the same task many times in a program, what will you do? Well, you can make a code block which performs that partiular task and you name that block and whenever you need to perform that you can call that code block called as functions. Functions improve code reusability. Functions can be pre-defined or user defined. Pre-defined are those which are already coded in prpgramming language. You just have to use them. User-defined are those, which the programmers define to simplify the task they needed. There are many functions in Python which are pre-defned, print function that we use to print the statement is also pre-defined. To make a function is Python we use def keyword. Syntax :   def fun():              statements Functions can also r...

Loops in Python

                            Loops While programming sometimes there are conditions where we need to execute a block of code to execute several times and loops makes this task easy for us. Every programming language supports loops. Generally there are three types of loops i.e. for, while and do-while. But in Python we have only two types of loops while and for. In loops we have a condition and block of code. We execute the code till the condition satisfies. Loops can be used in netsed forms also. While loop :  In while loop we use the while keyword and code block runs till the condition satisfies. syntax - while condition:                  statements E.g. i = 0      while i < 10:         print(i, end = " ")         i += 1          Output : 0 1 2 3 4 5 6 7 8 9 In ab...