Constructors Examples in Python

Definition : constructor is a special member function which is meant for initializing value of variables and objects. It is a special because constructor name will be  same as __init__ Types of constructors :     There are two different types of constructors available. Default constructor Parameterized constructor Default constructor : …

Super Example in Python

Definition :      Super is a keyword which is meant for invoking base class components from the base class. It will look from the variables from the base class. Program : class Base: def geta(self): return 10 def getb(self): return 20 class Derived(Base): def geta(self): return 100 def sum(self): print(super().geta()+self.geta()+self.getb()) …

Class Examples In Python

Definition : Class is a collection of variables and methods into a single unit. Program 1 : class Student(): def display(self): print(“Simple Student”) s=Student() s.display() #Student().display() Program 2 : class Student(): def display(self,a,b): print(“sum”,(a+b)) s=Student() s.display(10,20) #Student().display(10,20) All the functions of a class will have one parameter at least that …

Set Examples in Python

Definition : Set is a function which is meant for eliminating duplicate values available in a list or tuple. Program 1 : Data=[‘a’,’b’,’c’,’e’,’f’,’g’,’a’,’b’] Data1=[‘a’,’b’,’c’,’e’,’f’] Print(data) Print(set(data)) Print(set(data).difference(set(data1)) Print(set(data).intersection(set(data1))) Print(set(data).union(set(data1))) Program 2 : Data=[‘a’,’b’,’c’,’e’,’f’,’g’,’a’,’b’] Data1=set(data) Data1.add(“h”) Print(data1) data1.add(“I”) Print(data1) data1.add(“j”) Print(data1) data1.remove(“h”) Print(data1)

Dictionary Examples in Python

Definition : Dictionary is a data representation which is meant for holding the data in linear manner. Dictionary is basically use for representing key and value representations in python program. Program 1 : Student={‘name’ : ‘test’,’age’ : 22, ‘fee’ : 17.6}           Print(student) Print(student[‘age], student[‘name’]) If we want name=test age=22 …

Tuple Example Program in Python

Definition :      this is a linear data structure which is meant for holding the data in linear manner. Tuple will not allow any element modification during the program. Tuple is not allow to store the values Program : a=(‘a’,’b’,’c’,’d’,’e’) b=(1,2,3,4,5,6,7,8) c=”h”,”e”,”l”,”l”,”o” Print(a) Print(b) Print(c)

List Examples in Python

Definition : List is a linear data structure which is meant for holding the data in linear manner. Program 1 :   a=[99,2,3,4,5,6,7,8,’a’,’b’,’c’,’d’,’e’] Print(a[1]) Print(a[-1]) Print(a[-2]) Program 2 : A=[‘a’,’b’,’c’,’d’,’e’,1,2,3,4,5,6,7,8] B=[1,2,3,4,5,6,7,8] Print(a) Print(b*3) Print(b[4]) Print(len(b)) Program 3 : b=[1,2,3,4,5,6,7,8] b[5]=10 Print(b) b.insert(6,99) print(b) b.remove(10) print(b) program 4 : b=[1,2,3,4,5,6,7,8] for …