Social Media

Image Slider

7/30/2019

Instance Hidden Variables

Instance variables are those variables which depends upon the object.
They are not class specific, they are actually object specific.
We can also hide any instance variable.
Script: Write an script in Python which demonstrate how we can access hidden instance variable inside or outside the class.
class A:
    def __init__(self):
        self.__x=10
        self.__y=20
    def printresult(self):
        print("The value of y is",self._A__y) #Accessing inside the class
       

a1=A()
print(a1._A__x) #accessing outside the class
a1.printresult()


Output:
10
The value of y is 20


7/29/2019

Static Hidden Variables

There is no keyword like public, protected, private in Python. However you can make any hidden variable in Python.
Hidden variable can be static or instance.
You can't access hidden static variables outside the class in the same way as before.
Script1: Write an script which shows the demonstration of static hidden variable.
class A:
    x=6 #static variable
    __y=5 # static hidden variable

print(A.x) # 6 will be printed 
print(A.__y) #error comes here

Script2: Write an script which describe how to access static hidden variables.
Static hidden variables can be access inside the class using static method as given below:
class A:
    x=6 #static variable
    __y=5 # static hidden variable
    @staticmethod
    def showhiddenvar():
        print("The value of hidden variable y is",A.__y)

print(A.x)
A.showhiddenvar()

Output:
6
The value of hidden variable y is 5

Note* Static hidden variables can also be accessed  outside the class by changing the name during accessing them. Actually what Python is doing when we are making any variable hidden is, it is just changing its name. So we can access the hidden variable if we know what is the way of changing the name of variables of Python.
Example if i write__x=3
then we can access this static hidden variable outside the class as ClassName.__ClassName__x 




7/28/2019

Command Line Arguments

Command line arguments are those arguments which are given by user at a screen, when we run the python script.
You cannot directly run these kinds of programs using your IDE normally.
You will have to import argv which is defined in sys package.
argv is a kind of list in which first element is the name of the file in which you have written the script.
You can give any number of arguments at command line. They will start going into list one by one.
You have to run these kind of programs using command prompts in windows or in linux it will be terminal where you are going to learn these kinds of scripts.
A simple script to multiply two numbers is given below.
Script: Write an script in Python to multiply two numbers which are given by user at prompt screen.

7/25/2019

Polymorphism

Polymorphism comprises of two words poly+morphs.
Poly means many and morphs means forms.
If a single entity has different behaviour in different conditions then this condition is called polymorphism.
Polmorphism can be implemented using two ways.
  1. Function overloading
  2. Function overriding
Function Overloading and overriding:
Function overloading basically means functions with same names but different arguments.
But function overloading is not possible in Python language. In Python, if we are declaring more than one function with the same name, then the function that we declare currently will override the previous declared function. So function overloading is not possible in Python.
Below script will give you error due to function overriding.
Example of function overloading

class A:
   def f1(self):
      print("This is the function of class A")

class B(A):

   def f1(self,a):
      print("This is the function of class B")

a1=A()

a1.f1()
b1=B()
b1.f1()

Output:
This is the function of class A
Traceback (most recent call last):
  File "C:\Users\hp\Desktop\p1.py", line 12, in <module>
    b1.f1()
TypeError: f1() missing 1 required positional argument: 'a'

I think you got why the error is coming. 

Example of function overriding

class person:
   def __init__(self,name):
      self.name=name
   def viewProperties(self):
      print("You are in general category")
      print("Your name is ",self.name)

class student(person):

   def __init__(self,name):
      super().__init__(name)
   def viewProperties(self):
      print("Your category is student")
      print("Your name is",self.name)

p1=person("Aniket")

p1.viewProperties()
s1=student("Mayank")

s1.viewProperties()

Output:
You are in general category
Your name is  Aniket
Your category is student
Your name is Mayank

Note: In other object oriented languages like JAVA and C++, function overloading is possible.


7/16/2019

Multiple Inheritance

Multiple Inheritance denotes that a class has more than one parent classes.
In Java language multiple inheritance is not possible.
We can take advantage of multiple inheritance in Python language.
The Syntax for multiple inheritance can be as:
class A:
   ....
   ....
class B:
   ...
   ...
class C(A,B):
   ...
   ...
Script: Write an script in Python to demonstrate the use of Multiple Inheritance.
class A:
   def __init__(self,a):
      self.a=a
   def showA(self):
      print("the value of a=",self.a)

class B:

   def __init__(self,b):
      self.b=b
   def showB(self):
      print("the value of b=",self.b)

class C(A,B):

   def __init__(self,a,b,c):
      A.__init__(self,a)
      B.__init__(self,b)
      self.c=c
   def showC(self):
      A.showA(self)
      B.showB(self)
      print("the value of c=",self.c)

obj=C(2,3,4)

obj.showC()
print(obj.__dict__)


Output:
the value of a= 2
the value of b= 3
the value of c= 4
{'a': 2, 'b': 3, 'c': 4}



7/15/2019

Inheritance

Inheritance is basically one of the most important feature of Object Oriented Programming.
Inheritance means acquiring the properties of a class in another class.
The parent class is called Base class while child class is called Derived class.
By using inheritance concept in our script we can reuse the code.
Inheritance saves a lot of time of programmer as writing code again and again is time consuming.
There can be many types of Inheritance. For example

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance
Script: Write an script in which class B inherit all the properties of class A.

class A:
   def __init__(self, a):
      self.a=a
   def showA(self):
      print("the value of a is "+str(self.a))
      
class B(A):
   def __init__(self,a,b):
      super().__init__(a)
      self.b=b
   def showB(self):
      print("The value of b is "+str(self.b))

obj1=B(2,3)
obj1.showA()
obj1.showB()

Output:
the value of a is 2
The value of b is 3
The following points needs to be remember in the above program.
  1. Child class constructor does not call parent class constructor implicitly in Python language.
  2. If we make object of child class and we call a function then first it will find out the function in the child class, if the function matches in the child class, then it will not search that function in the parent class.
  3. If the function is not found in the child class it will search that in the parent class.

7/14/2019

Projects in Python Language

Practice Problems

Will be updated soon...
7/12/2019

Class Method

There is a special type of method that Python language offers, this is called class method.
This function is neither static nor instance.
Notation is compulsory while we are going to declare this kind of function.
This function can also be used to create static variables.
Class method contains the class reference implicitly which means one argument is necessary while we are going to create class method.

Script1: Write an script which demonstrate the usage of class method in Python language.

class Demo:
    @classmethod
    def fun(cls):
        cls.a=10
        Demo.b=20
        
Demo.fun()
print(Demo.a)
print(Demo.b)

Output:
10
20


Script2: Write an script to print all the static variables exist in the class.

class Demo:
    x=80
    @classmethod
    def fun(cls):
        cls.a=10
        Demo.b=20
        
Demo.fun()
print(Demo.__dict__)

Instance and Static Variables

Instance Variables:
Instance variable are those variables which depends upon the creation of the object.
We can create instance variable by using any of the way you like.
  1. Using __init__() method
  2. Using a function defined in the class
  3. Outside the class
Script: Write an script to demonstrate the creation of instance variables using all three ways above.

class Test:
    def __init__(self):
        self.a=50
        print("Instance Variable a is created using init function ")
    def createinstncvar(self):
        self.b=10
        print("Instance Variable b is created using instance function")

t1=Test()
t1.createinstncvar()
t1.c=30
print("Instance variable c is created outside the class")
print("a=",t1.a," b=",t1.b," c=",t1.c)

Output:
Instance Variable a is created using init function 
Instance Variable b is created using instance function
Instance variable c is created outside the class
a= 50  b= 10  c= 30

**Here self contains current object reference.
Creation of instance variable using __init__() function is more popular and widely used way.


Static Variables:
If you have the background of Java language then for static variables there is static keyword present to create them.
Static variables are not object specific, these are class oriented.
They can be shared by any number of objects of that class in which static variable is defined.
Static variables can be created using
  1. __init__() method
  2. instance method
  3. static method
Script:Write an script which shows the creation of static variables using above three ways.

class Demo:
    a=10
    def __init__(self):
        Demo.b=20
    def fun1(self):
        Demo.c=30
    @staticmethod  # This notation is optional but advisable
    def fun2():
        Demo.d=40

print("Static variable a=",Demo.a," is created in the body of the class")
d1=Demo()
print("Static variable  b=",Demo.b," is created using __init__() function")
d1.fun1()
print("Static variable c=",Demo.c," is created using instance function")
Demo.fun2()
print("Static variable d=",Demo.d," is created using static function")
    
Output:
Static variable a= 10  is created in the body of the class
Static variable  b= 20  is created using __init__() function
Static variable c= 30  is created using instance function
Static variable d= 40  is created using static function

Types of Variables in Python Language (Local and Global)

There are mainly four types of variables that python language provide to us.
These are as follows:
  1. Local variable
  2. Global variable
  3. Instance variable
  4. Static variable
Local Variables:
Local variables are those variables which are defined inside a function. 
Their scope is limited to that function in which they are defined.
They can't be accessible from outside the function.

Global Variables:
Global variables are those variables which are defined outside the function as well as outside the class.
These types of variables can be accessible from anywhere means outside or inside the function or class.
Script: Demonstration of Local and Global Variable

y=7
def fun():
    x=5
    print("x is a local variable")
    print("y is a global variable")
fun()
print("y is a global variable")
    
Output:
x is a local variable
y is a global variable
y is a global variable

From the above script we can conclude that local variables are only accessible inside the function in which they are defined while on other side global variables can be accessible from anywhere in the script.