0% found this document useful (0 votes)
33 views17 pages

1st Unit Notes

Uploaded by

rohith96kum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views17 pages

1st Unit Notes

Uploaded by

rohith96kum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 17

1.what is class?

a class is a user-defined blueprint of prototype from which objects are created. It allows
you to define a custom data structure by combining attributes[variables] and methods[functions]
into a single entity.

Syntax:

class classname:

2. what is object?

An object is an instance of a class. A class is like a bluprint while an instance is a copy of


the class with actual values.

Syntax:

obj = classname()

print(obj.atrr)

3. Define self_method :

* In python, self is a reference to the instance of the class in which is defined.

* Every instance method in Python needs to have self as its first parameter.

* Each method of the class uses self to access.

4. Define _ _init_ _() method:

* In python, _ _init_ _() method is a special method also called as constructor. It is


automatically invoked when a new instance of a class in created.

* It is used to initialize the attributes of the class with value paused to it.

* the _ _init_ _ method is similor to constructor in C++ and JAVA.

5. What is Inheritance ?
Inheritance is a fundamental concept in object-orianted programming that allows to
create a hiearcy of classes that shares a set of properties and method by deriving a class from
another class.

Syntax:
class parentclass:

// code block

class childclass(parent class):

// code block

6. What is Polymorphism ?

* In Python, polymorphism refers to the ability to use a single interface or function name
to represent different types of objects or data.

* It allows object of different classes to be treated as object of a common super class.

* It prometes flexibility and reusability, making it easier to write generic code.

7. What is Encapsulation ?

* Encapsulation is one of the fundamental concepts in object-oriented programming


(OOP). It describes the idea of wrapping data and the methods that work on data within one unit.

* This puts restrictions on accessing variables and methods and exposing only what is
necessary.

8. What is Assertion ?

* assertion is the boolean expression that checks if the statement is True or False.

* If the statement is true then it does nothing and continues the execution, but if the
statement is False then it stops the execution of the program and throws an error.
9. What is Generators ?
* generator is a function or expression that will process a given iterable one object at a
time, on demand.

* A generator will return a generator object, which can be cast as a list or some other
data structure as appropriate.

10. What is Iterators ?


* An iterator in Python is an object that contains a countable number of elements that
can be iterated upon.

* Iterators are objects that allow you to traverse through all the elements of a collection
and return one element at a time.
5 Marks

1.Explain about types(Private,public,protected)?

MEMBER TYPES:

Most programming languages has three forms of access modifiers which are

used to restrict access to the variables and methods of the class.


Python uses ‘_’ symbol to determine the access control for a specific data member or a member

function of a class.

A Class in Python has three types of access modifiers:

· Public Access Modifier

· Protected Access Modifier

· Private Access Modifier

Public Access Modifier

The members of a class that are declared public are easily accessible fromany part of the program

Example for public access modifier in class:

Class Student:

def _init_(self, name, age):

self.stuName = name

self.stuAge = age

def displayAge(self): # public member function

print("Age: ", self.Age) # accessing public data member

obj = Student("SMITH", 20) # creating object of the class

print("Name: ", obj.Name) # accessing public data member

obj.displayAge() # calling public member function of the class

OUTPUT:

Name: SMITH

Age: 20

Protected Access Modifier:

· The members of a class that are declared protected are only accessible to a

class derived from it.

· Data members of a class are declared protected by adding a

single underscore ‘_’ symbol before the data member of that class.
Example for protected access Modifier

class Student:

# protected data members

_name = None

_roll = None

_branch = None

# constructor

def _init_(self, name, roll, branch):

self._name = name

self._roll = roll

self._branch = branch

# protected member function

def _displayRollAndBranch(self):

# accessing protected data members

print("Roll: ", self._roll)

print("Branch: ", self._branch)

# derived class

class College(Student):

# constructor

def _init_(self, name, roll, branch):

Student._init_(self, name, roll, branch)

# public member function

def displayDetails(self):

# accessing protected data members of super class

print("Name: ", self._name)

# accessing protected member functions


self._displayRollAndBranch()

# creating objects of the derived class

obj = College("SMITH", 17066, "COMPUTER SCIENCE")

# calling public member functions of the class

obj.displayDetails()

OUTPUT:

Name: SMITH

Roll: 17066

Branch: COMPUTER SCIENCE

Private Access Modifier:

· The members of a class that are declared private are accessible within the

class only, private access modifier is the most secure access modifier.

· Data members of a class are declared private by adding a double underscore ‘’

symbol before the data member of that class.

Program to illustrate private access modifier in a class

class Student:

# private members

__name = None

__roll = None

__branch = None

# constructor

def _init_(self, name, roll, branch):

self.__name = name

self.__roll = roll

self.__branch = branch

# private member function


def __displayDetails(self):

# accessing private data members

print("Name: ", self.__name)

print("Roll: ", self.__roll)

print("Branch: ", self.__branch)

# public member function

def accessPrivateFunction(self):

accessing private member function

self.__displayDetails()

# creating object

obj = Student("SMITH", 17066, "Computer Science")

# calling public member function of the class

obj.accessPrivateFunction()

OUTPUT:

Name: SMITH

Roll: 17066

Branch: COMPUTER SCIENCE

2.Explain about Decorators?

· Decorators are a very powerful and useful tool in Python since it allows

programmers to modify the behaviour of a function or class.

· The decorators are used to modify the behaviour of function or class.

· In Decorators, functions are taken as the argument into another

function and then called inside the wrapper function.

Syntax:

@gfg_decorator

def hello_decorator():
print("Gfg")

'''Above code is equivalent to -

def hello_decorator():

print("Gfg")

hello_decorator = gfg_decorator(hello_decorator)'''

Example:

import time

import math

# decorator to calculate duration

# taken by any function.

def calculate_time(func)

# added arguments inside the inner1,

# if function takes any arguments,

# can be added like this.

def inner1(*args, **kwargs):

# storing time before function execution

begin = time.time()

func(*args, **kwargs)

# storing time after function execution

end = time.time()

print("Total time taken in : ", func.__name__, end - begin)

return inner1

# this can be added to any function present,

# in this case to calculate a factorial

@calculate_time

def factorial(num):
# sleep 2 seconds because it takes very less time

# so that you can see the actual difference

time.sleep(2)

print(math.factorial(num))

# calling the function.

factorial(10)

Output:

3628800

Total time taken in : factorial 2.0061802864074707

10 Marks

1.Method overloading and Method overrinding?

Method Overloading:

Two or more methods have the same name but different numbers of parameters or different types of
parameters, or both. These methods are called overloaded methods and this is called method overloading.

Example for overloading:@dispatch(int, int)

def product(first, second):

result = first*second

print(result)

# passing two parameters

@dispatch(int, int, int)

def product(first, second, third):

result = first * second * third

print(result)

# you can also pass data type of any value as per requirement

@dispatch(float, float, float)

def product(first, second, third):

result = first * second * third


print(result)

# calling product method with 2 arguments

product(2, 3) # this will give output of 6

product(2, 3, 2) # this will give output of 12

# calling product method with 3 arguments but all float

product(2.2, 3.4, 2.3) # this will give output of 17.985999999999997

Output:

12

17.985999999999997

In Backend, Dispatcher creates an object which stores different

implementation and on runtime.

Method Overriding:

Method overriding is an ability of any object-oriented programming language that allows a subclass or
child class to provide a specific implementation of a method that is already provided by one of its super-
classes or parent classes. When a method in a subclass has the same name, the same parameters or
signature, and same return type(or sub-type) as a method in its super-class, then the method in the
subclass is said to override the method in the super-class.

Example:
class Parent():

# Constructor

def _init_(self):

self.value = "Inside Parent"

# Parent's show method

def show(self):

print(self.value)

class Child(Parent): # Defining child class

def _init_(self): # Constructor

self.value = "Inside Child"

# Child's show method

def show(self):

print(self.value)

obj1 = Parent() # Driver's code

obj2 = Child()

obj1.show()

obj2.show()

Output:

Inside Parent

Inside Child

Calling the Parent’s method within the overridden method

Parent class methods can also be called within the overridden methods. This

can generally be achieved by two ways.

1.Using Classname: Parent’s class methods can be called by using the

Parent classname.method inside the overridden method.

Example:

Python program to demonstrate calling the parent's class method inside the overridden method

class Parent():

def show(self):
print("Inside Parent")

class Child(Parent):

def show(self):

# Calling the parent's class

# method

Parent.show(self)

print("Inside Child")

# Driver's code

obj = Child()

obj.show()

Output:

Inside Parent

Inside Child

2.Using Super():

Python super() function provides us the facility to refer to the parent class

explicitly. It is basically useful where we have to call superclass functions. It

returns the proxy object that allows us to refer parent class by „super‟.

Python program to demonstrate calling the parent's class method inside the overridden method

using super()

class Parent():

def show(self):

print("Inside Parent")

class Child(Parent):

def show(self):

# Calling the parent's class

# method

super().show()

print("Inside Child")

# Driver's code
obj = Child()

obj.show()

Output:

Inside Parent

Inside Child

2.Explain About Inheritance?

One of the core concepts in object-oriented programming (OOP) languages is inheritance. It is a


mechanism that allows you to create a hierarchy of classes that share a set of properties and methods by
deriving a class from another class. Inheritance is the capability of one class to derive or inherit the
properties from another class.

Syntax :

Class BaseClass:

{Body}

Class DerivedClass(BaseClass):

{Body}

Different types of python inheritance:

· single inhertance

· multilevel inheritance

· multiple inheritance

· hierarchical inheritance

Single Inheritance:

When a child class inherits from only one parent class, it is called single inheritance. We saw an example
above.

Example:

class parent:

def display(self):

print("parent")

class child(parent):

def show(self):

print("child")
c1=child()

c1.show()

c1.display()

OUTPUT:

child

Parent

Multiple inheritances:

When a child class inherits from multiple parent classes, it is called multiple inheritances.

Example:

class Father:

def fdisplay(self):

print("Father")

class Mother:

def mdisplay(self):

print("Mother")

class child(Father,Mother):

def cdisplay(self):

print("child")

c1=child()

c1.cdisplay()

c1.mdisplay()

c1.fdisplay()

Output:

child

Mother

Father

Multilevel inheritance:

When we have a child and grandchild relationship. This means that a child class will inherit from its
parent class, which in turn is inheriting from its parent class.
Example:

class grandparent:

def display(self):

print("grand parent class")

class parent(grandparent):

def show(self):

print("parent class")

class child(parent):

def show(self):

print("child")

c1=child()

c1.display()

c1.show()

c1.display()

OUTPUT:

grand parent class

child

grand parent class

Hierarchical inheritance:

More than one derived class can be created from a single base.

Example:

class parent:

def func1(self):

print(“TYPE OF THE ANIMAL.”)

class Domestic(Animals):

def func2(self):

print(“IT IS A DOMESTIC ANIMAL.”)

class Wild(Animals):

def func3(self):
print(“IT IS A WILD ANIMAL”)

Obj1=Domestic()

Obj2=Wild()

Obj1.func1()

Obj1.func2()

Obj2.func1()

Obj2.func3()

Output:

TYPE OF THE ANIMAL.

IT IS A DOMESTIC ANIMAL.

TYPE OF THE ANIMAL.

IT IS A WILD ANIMAL.

3.Explain polmorphism?

The word polymorphism means having many forms. In programming,

polymorphism means the same function name (but different signatures) being

used for different types. The key difference is the data types and number of

arguments used in function.

Example of inbuilt polymorphic function

# Python program to demonstrate in-built polymorphic functions

# len() being used for a string

print(len("geeks"))

# len() being used for a list

print(len([10, 20, 30]))

Output

Examples of user-defined polymorphic functions:

# A simple Python function to demonstrate Polymorphism

def add(x, y, z = 0):


return x + y+z

# Driver code

print(add(2, 3))

print(add(2, 3, 4))

Output

You might also like