Posts

Showing posts from July, 2020

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

Conditions in Python

                           If....else... Decision making is important part of programming. We come across many situations where we have to choose something during coding like whether it will print success or failure, something like that. Like other programming language, in Python also we have If...else conditions. If true then this else that.  By using If..else we only select the block of code we want to execute. We can also use nested if...else . In nesed if...else we have multiple if..else conditions inside the former conditions which we will see in example. Syntax : if condition:                statements         else:             statements E.g.   x = 10      if x <= 10:           print("x is low.")      else:  ...

Python Dictionary

                            Dictionary A dicitonary is a key-value pair collection which is unordered and can be changed or altered. We can not perform indexing on dictionary like lists but dictionary is still indexed. How? Well, we can use key inplace of index to get the value. We use curly braces { } to represent the dictionary.  E.g. dict1 = {'a':1, 'b':2, 'c':3, 'd':4} The value on left side of colon is called key, and value on right side of colon is called value. Hence key-value pair. We can use any datatype for keys and values. We can also use lists, sets, dictionary,etc. To access the value, we use keys. We can also use get() function for access the value. Just put the key in get() and value is accessed. E.g.   dict1['a']          # output : 1      dict1['c']          # output : 3     dict1.get('b')    ...