Python Constructor !!

Python Constructor !!




In this article we will discuss about constructors and types of constructor in python.

Constructor


Constructor is a special function which name is used to initialize the instance member of the class.

Constructor is execute when object of the class is created.

Types Of Constructor


  • Parameterized Constructor
  • Non-parameterized Constructor

Create Constructor


Constructor created in python with _init_ method.

We can give any number of arguments in constructor.


Non Parameterized Constructor


If there is no arguments in constructor then it is non perameterized constructor.

When instance of class created  constructor invoke automatically.

Program :


Output :

This is non parameterized constructor
Hello Nibkarma

Parameterized Constructor


If there is arguments in constructor then it is perameterized constructor.

When we pass number of arguments in the constructor then constructor invoke automatically.
Program :


Output :


This is parameterized constructor
Hello Nibkarma

In this article we discuss about Constructor and types of constructor in brief.For more interesting articles keep join us!!

Basic Programs Of All Python Methods


1) Program :


Python program to print Hello World.

# This prints Hello World on the output screen
print('NibKarma')

Output :


Hello NibKarma

List Programs

2) Program :



def string_length(str):
    count = 0
    for char in str:
        count += 1
    return count
print(string_length('NibKarma'))


Output : 8

Python Dictionary

3) Program :


def char_frequency(str):
    dict = {}
    for n in str:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('NibKarma'))

Output :


{'a': 2, 'N': 1, 'i': 1, 'K': 1, 'r': 1, 'm': 1}

Python Tuple 

4) Program :

tuple=('a','b','c','d','e')
print(tuple)
del tuple[1]
print(tuple)

Output :


('a', 'b', 'c', 'd', 'e')

5) Program :



a = "Hello world"
print(a)
print(a[0])
print(a[1])
print(a[2])

x = "Hello"
y = "Khush"
print(x+y)

s = "Hello Khush"
print(s[0:2])
print(s[2:7])
print(s[4:])

Output :


Hello world
H
e
l
HelloKhush
He
llo K
o Khush

Python Pass

6) Program :


l=[1,2,3,4,5]
for i in l:
    if i==3:
        pass
        print ("Pass execute")
        print("Hello khush")
    print(i)

Output :


1
2
Pass execute
Hello khush
3
4
5

Python List

7) Program :



a1=["khush","jinu","ashu","shwetu"]
print(a1)
a3=['a','b','c']
print(a3)
a5=[1,2,3]
print(a5)
a6=[1,'khush']
print(a6)


a=['a','b','c','d','e']
print(a)
a[2]=3
print(a)
a[1:3]=1,2
print(a)
a[0:]=1,2,3,4,5
print(a)
del a[1]
print(a)

n=["khush","nishu","priti"]
print(n)
n.append("jinu")
print(n)
n.insert(2,"siya")
print(n)

n=["khush","nishu","priti"]
print(n)
n.remove("nishu")
print(n)
n.remove(n[0])
print(n)

n=[1,2,3,4,5]
print(n)
print("length is "+str(len(n)))

n=[1,2,3,4,5]
for i in n:
    print(i)

a=["khush","keri","ketan","khushi"]
print(a)
print("after sorting :")
a.sort()
print(a)

a=["khush","keri","ketan","khushi"]
print(a)
print("after reverse :")
a.reverse()
print(a)

a=["khush","keri","ketan","khushi"]
print(a)
n=max(a)
print(n)
n=min(a)
print(n)

print("maximum and minimum in numbers")

a=[1,2,3,4,5]
print(a)
n=max(a)
print(n)
n=min(a)
print(n)

a=[1,2,3,4,5]
print(a)
n=max(a)
print(n)
n=min(a)
print(n)
a.clear()
print(a)


Output :


['khush', 'jinu', 'ashu', 'shwetu']
['a', 'b', 'c']
[1, 2, 3]
[1, 'khush']
['a', 'b', 'c', 'd', 'e']
['a', 'b', 3, 'd', 'e']
['a', 1, 2, 'd', 'e']
[1, 2, 3, 4, 5]
[1, 3, 4, 5]
['khush', 'nishu', 'priti']
['khush', 'nishu', 'priti', 'jinu']
['khush', 'nishu', 'siya', 'priti', 'jinu']
['khush', 'nishu', 'priti']
['khush', 'priti']
['priti']
[1, 2, 3, 4, 5]
length is 5
1
2
3
4
5
['khush', 'keri', 'ketan', 'khushi']
after sorting :
['keri', 'ketan', 'khush', 'khushi']
['khush', 'keri', 'ketan', 'khushi']
after reverse :
['khushi', 'ketan', 'keri', 'khush']
['khush', 'keri', 'ketan', 'khushi']
khushi
keri
maximum and minimum in numbers
[1, 2, 3, 4, 5]
5
1
[1, 2, 3, 4, 5]
5
1
[]

You May Also Like This Article :


Python Lambda

8) Program :


a = 5
b = 2

sum = a+b
print("sum is : "+str(sum))

a = 5
b = 2

sub = a-b
print("substraction is : "+str(sub))

a = 5
b = 2

mul = a*b
print("multiplication is : "+str(mul))

a = 5
b = 2

div = a/b
print("divison is : "+str(div))

a = 5
b = 2

mod = a%b
print("modulo is : "+str(mod))

Output :


sum is : 7
substraction is : 3
multiplication is : 10
divison is : 2.5
modulo is : 1

Python Break

9) Program :


a='khush'
print("without break statement")
for i in a:
    print(i,end='')
print()

print ("with break statement")
for i in a :
    if i=='u' :
        break
    print(i)

for i in range (1,11):
    if i==6:
        break
    print(i)

for i in range (1,11):
    if i==6:
        continue
    print(i)

Output :


without break statement
khush
with break statement
k
h
1
2
3
4
5
1
2
3
4
5
7
8
9
10

Python Class

10) Program :


class Student:   
    def __init__(self, name): 
        print("This is parametrized constructor") 
        self.name = name 
    def show(self): 
        print("Hello",self.name) 
student = Student("Nibkarma") 
student.show()

Output :


This is parametrized constructor
Hello Nibkarma

11) Program :


def func():
    print("def called")
func()

def func(name):
    print("hi "+ name)
func('khush')

def value(a):
    print("a is "+str(a))
value(10)

def func(name):
    print("name is :"+name)
s=input("enter name:")
func(s)

def func(name):
    msg=print("hi "+name)
    return msg;
name=input("enter name :")
func(name)

Output :


def called
hi khush
a is 10
enter name:khush
name is :khush
enter name :khush
hi khush

Python Data-types

12) Program :



x=2
y=3.4
z=False

print(x)
print(y)
print(z)


x = 2
y = 3
sum = x+y
print(sum)

a=int(input("Enter a:"))
b=int(input("Enter b:"))
print("\n")

sum = a+b
sub = a-b
mul = a*b
div = a/b
mod = a%b

print(sum)
print(sub)
print(mul)
print(div)
print(mod)

Output :


2
3.4
False
5
Enter a: 3
Enter b:4


7
-1
12
0.75
3

Simple Python Programs :


1) Program :


Python program to print Hello World.

# This prints Hello World on the output screen
print('Hello World')

Output :


Hello World

2) Program : 


For Getting input in Python by user.

str = input("Enter any string: ")
print(str)

Output :


Enter any string: NibKarma
NibKarma

3) Program : 


Python program to check if a number is positive or negative.

number = int(input("Enter number: "))

# checking the number
if number < 0: print("The entered number is negative.") elif number > 0:
    print("The entered number is positive.")
elif number == 0:
    print("Number is zero.")
else:
    print("The input is not a number")

Output :


Enter Number: 200
The entered number is positive.

4) Program :


Python program to check leap year.

year = int(input("Enter Year: "))

# Leap Year Check
if year % 4 == 0 and year % 100 != 0:
    print(year, "is a Leap Year")
elif year % 100 == 0:
    print(year, "is not a Leap Year")
elif year % 400 ==0:
    print(year, "is a Leap Year")
else:
    print(year, "is not a Leap Year")

Output :


Enter Year: 2016
2016 is a Leap Year

5) Program :


Python program to check whether the input character is an alphabet.

ch = input("Enter a character: ")
if((ch>='a' and ch<= 'z') or (ch>='A' and ch<='Z')):
    print(ch, "is an Alphabet")
else:
    print(ch, "is not an Alphabet")

Output :


Enter a character : N
N is an alphabet

6) Program :


Python program to check vowel or consonant.

ch = input("Enter a character: ")

if(ch=='A' or ch=='a' or ch=='E' or ch =='e' or ch=='I'
 or ch=='i' or ch=='O' or ch=='o' or ch=='U' or ch=='u'):
    print(ch, "is a Vowel")
else:
    print(ch, "is a Consonant")

Output :


Enter a character : I
I is a Vowel

Things You Should Know About Python Before Learning Python :


Python is an interpreted, high-level, general-purpose programming language. Developed by Guido van Rossum and first released in 1991,

The Reasons  For Why Writing Software Applications in Python


1) Readable and Maintainable Code
2) Multiple Programming Paradigms
3) Compatible with Major Platforms and Systems
4) Robust Standard Library
5) Many Open Source Frameworks and Tools
6) Simplify Complex Software Development
7) Adopt Test Driven Development

The Python Applications :


1) Web and Internet Development
2) Applications of Python Programming in Desktop GUI
3)  Science and Numeric Applications
4) Software Development Application
5) Python Applications in Education
6) Python Applications in Business
7) Database Access
8) Network Programming
9) Games and 3D Graphics
10) Console-based Applications
11) Audio – or Video- based Applications
12) Applications for Images
13) Enterprise Applications
14) 3D CAD Applications
15) Computer Vision
16) Machine Learning
17) Robotics
18) Web Scraping
19) Scripting
20) Artificial Intelligence
21) Data Analysis

You can also learn :


You Also Can Search For Other Python Keywords Like :



Python , Python For Loop  , Python Loops  , Python Tutor  , Python Tutorial  , Python Dictionary  , Python Learning  , For Python List  , Python List  , Python Anaconda  , Python Download  , Python Range  , Python Regex  , Python Else If  , Python If Else  , Python Programming  , Python Snake  , Python Class  , Python Set  , Python In Function  , Python Function  , Python 3  , Python Pandas  , Python Random  , Python Array  , Python Requests  , Python Tuple  , Python Enumerate  , Python Write To File  , Python IDE  , Python Logger

 Python String  , Python While Loop  , Python Map  , Python Install  , Python Read File  , Python Code  , Python Flask  , Python Json  , Python Operator  , Python Or Operator  , Python Zip  , Python Compiler Online  , Python Online Compiler  , Python Online  , Python Replace  , Python Join  , Python Unit Testing  , Python Unittest  , Python Machine Learning  , Python With Machine Learning  , Python Certification  , Why Python For Machine Learning  , Python Interview Questions  , Python gui  , Python Reverse String  , Python Compiler

The Python Regular Expression  , Python Queue  , Python 2.7  , Python Yield  , Python Web Scraping  , Python IDE Online  , Python Django  , Python Scripting  , Python Numpy  , Python Multithreading  , For Python  , Python Or  , Python Generator  , Python For Windows  , Python 3.7  , With Python  , Python Documentation  , Python Version  , Python Beginners  , Python For Beginners  , Python Language  , Python List Functions  , Python Projects  , Python Data Structures  , Python Mysql  , Python Library  , Python And  , Python w3schools  , Python Modules  , Python Hello World  , Python Data Types  , Python 3.6  , Python Interpreter

Python Keyerror  , Python Ord  , Python Meaning  , Python Ordereddict  , Python Xrange  , Pythonanywhere  , Python Range Function  , Python For Data Analysis  , Python With Data sScience  , Python For Data Science  , Python Basics  , Python Interpreter Online  , Python Online interpreter  , Python Cheat Sheet  , Python Cheat Sheet  , Python vs Java  , Python Syntax  , Python Xml Parser  , Python Xml Parsing  , Python Join List  , Python Editor