Constructor concepts in python with examples

By // No comments:

 A Constructor is a special type of method which is used to initialize the object of the class.In c++ and java ,the constructor is the same name as its class.But in python,it is different .Hence we use __init__() as a special method to simulates the constructor of the class.This special method is called when the object of the class is created.

  • The __init__() method accepts the self keyword as a first argument which is used to access the methods or attributes of the class.
  • We can pass the any number of arguments at the time of object creation according to definition of __init__() method.
  • The constructors are mainly used to initialize the class attributes.
  • In python,every class must have a constructor even it may be a default constructor.
3

Classes and Objects Concepts in Python

By // No comments:
Python classes and objects

Python is an object oriented programming language .Every things in python treated as an object.python supports structured as well as object oriented programming language paradigms.

  • A class contains data and  methods that can access or manipulate this data.Thus a class is a collection of data and methods.A class is a blueprint (prototype) of an object. Thus a class lets us bundle data and functionality together.
  • A user-defined blueprint/prototype for an object is known as class.example : House is an object where blueprint/prototype/model of house is known as a Class.
  • A class is generic in nature,whereas an object is specific in nature.
  • We can create many objects from one class.We can easily access the class member with the help of these objects.
  • An object is also an instance of a class The process of creating this object  is called instantiation
  • We can  also create our own classes.These are often called user defined data types but we generally uses ready-made classes in python.
3

Arrays Concept in Python With Examples

By // No comments:
Python Arrays 
  • An array is a collection of elements that are stored in contiguous memory locations.
  • An array holds fixed number of elements of the same data types.
  • An array can store multiple elements of the same data types.
  • An array is used to store multiple values in single variable.
e.g.
arr_val1="horse"
arr_val2="lion"
arr_val3="man"
  • An array in python can be handled a module named array
e.g.
import array as aar

Representation of array:-
An array can be declared in different ways and different languages.Here We are going to declared the array in python language. 
  • Python array starts with 0 index.
  • Python array's elements can be accessed via its index.
  • The length of an array = total number of index+1.
3

Modules and Package Concepts in Python

By // No comments:

 Today, we will learn about most important concepts of modules in python.

Modules in Python:-

  • A modules is a .py files containing definitions and statements. So all .py files that we created for our python programs are modules.
  • A python modules can be defined a python program file which contains .py files including python variable , class and functions etc.
  • A python modules provides us the flexibility to organize the code in a logical way.
e.g. Create a modules named as modules.py
def show ():
    print("Welcome to MNT LAB")
#calling the show() function
show()
Output:-
Welcome to MNT LAB
>>> 
The Main Modules:-
  • When we execute a program its module name is __main__.
  • This name is available in the variable __name__.
  • This technique is executed only when we run program directly and not executed when imported as a module.
  • When we run any python codes ,python interpreter start interpreting the codes inside it. At that time it sets some implicit variable values like math, random etc. ,one of them is __name__ whose value set as __main__.
  • If the program source file imported as a module , At that situation interpreter sets the __name__ value to module name. Then this main  method will not work.
3

Lambda and Recursive functions in python

By // No comments:

Lambda function in python:

  • Normal functions have their names and they are defined using the def keyword.An anonymous functions do not have names and they are defined using the lambda keyword.
Syntax:-
lambda arguments:expression
  • An anonymous function defined using lambda can take any number of arguments but can return only one value.
  • Lambda functions can be used wherever function objects are required usually.It is used as an argument to other functions.
  • An anonymous function contains a small piece of code.
3

Function concepts in python Part 2

By // No comments:

We have already explained some basic features of functions in our previous lectures.Before learn this part ,you have to learn function concepts in python part 1.

Types of Arguments:-

There are four types of arguments in python functions as given below:-

  1. Required arguments or Positional arguments.
  2. Keyword arguments
  3. Default arguments
  4. Variable Length arguments
    1.) Required arguments or Positional arguments
Required arguments must be passed in correct positional order.For example,if a function want an int, float and string to be passed it,then the call to this function should look like:
func(10,5.12,'MNT LAB')                #Correct function call
func(5.12,10,'MNT LAB')                #Incorrect function call gives error

In required arguments number of arguments passed must be match with number of arguments received.
3

Function Concepts in Python part 1

By // No comments:

What are functions:-

  • Python function is a block of code that performs a specific task.
  • Python function allows us to divide a large program into the basic building block.
  • Python function helps to break the program code  into smaller part.It is used to avoid the repetition of the code.
Types of functions

There are two types of the functions in python.

  1. Built-in Functions
  2. User defined functions
 1.) Built-in functions:- The Built-in functions are those functions which are predefined in python.I have already explained more built-in functions in our previous lectures.e.g. len(),min(),max(),range() ,print() etc.
2.) User defined functions:-The user defined functions are those functions which are defined by user for any specific tasks in python.e.g. show(),display(),calculate() etc.
Advantage of functions:-
There are two main advantage of functions as given below:-
  • Functions help us to divide our program into multiple tasks.For each task we can define a separate functions.This makes the code modular.
  • Functions provide us a reuse mechanism.
3
Powered by Blogger.