Files are basically used to store and write the data.Files are used to permanently store the data in a non volatile memory(pen drive ,hardisk).We were taking the input from the console and writhing it back to the console.It is not enough,if we want to display more data then it is not display on the console because data are temporely stored in RAM (volatile memory).when we off or shut down the computer/pc ,data will be lost.So that here file input/output concepts are more important.The file handling plays an important role when the data needs to be stored permanently into the file.When we use files,we can access the stored information after the program termination because files are non volatile storage.When we want to read ,write and modify...
Exception Handling in python with examples
Python Exception:-An exception is an unusal things in a program that cause the interruption in flow of program.An exception is the run-time errors that are unable to handle the python program.While creating and executing a python script things may go worng at two different stages:-During compilationDuring executionError that occurs during compilation time,are called syntax error.The Error that occurs during execution or run time, are called exceptions or run-time error.Python provides a way to handle the exception so that the program/codes can be executed without any interruption.Python has many built-in exceptions that enable program to run without interruption and give the output of the cod...
Encapsulation In Python with examples
Encapsulation:-Encapsulation is a important concept of object oriented programming language.In encapsulation methodology we can restrict access to methods and variables.To prevent the data access of the class from out side or same class, is known as encapsulation.In python ,we denote private attributes using prefix underscore(single(_) or double(__)).It is used to hide the entire class data from the outside source. Benefits and needs of Encapsulation in python:-Encapsulation concepts not only ensure better data flow but also protects the data from the outside sources.Encapsulation is helpful to achieving well -defined interaction in each applications.Encapsulation helps to secure the python applications.Encapsulation ensures...
Polymorphism concepts in python with example
Polymorphism word is made Greek words poly(many) and morphism(shape/forms).It means we can use same method/function name for different types.We generally uses two types of polymorphism in c++,java ,c# etc.But python support only one polymorphism that is called run-time polymorphism.Types of Polymorphism :-Compile time polymorphism (e.g. method overloading)Run-time polymorphism (e.g. method overriding) 1.) Compile time polymorphism:- It is used for method overloading.It support compile-time binding.Python does not support compile time polymorphism.In this, class ,method and object are bind at compile tim...
Inheritance Concepts in python with example
There are some important points to define inheritance concepts in python.Inheritance is the most important concept of object-oriented programming language.It simulates the real world concepts of inheritance.In inheritance, a new class called derived class can be created to inherit features of an existing class called base class.Base class is also known as super class or parent class.Derived class is also know as sub class or child class.In inheritance,when we create the object of child class.Then this child class object acquires all the properties and behaviors of the parent object. Inheritance concept provide the re-usability of cod...
oops concepts in python with real life example
OOP(object oriented programming) is used to solve the problems with the help of classes and objects.Python is an object oriented programming language like c++,java and c#.We can easily develop the applications using this object oriented approach.An object oriented paradigm is used to design the program using using classes and objects.This oops concepts in python focuses on creating reusable code.There are some major concepts is used in oops(object -oriented programming system) as given below.class objectmethodInheritancePolymorphismEncapsulationAbstraction1.) Class:A class is a blue print of an object.A class is a collection of data and methodsMore Details....
Constructor concepts in python with examples
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 constructo...
Classes and Objects Concepts in Python
Python classes and objectsPython 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...
Arrays Concept in Python With Examples
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 arraye.g.import array as aarRepresentation 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+...
Modules and Package Concepts in Python
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...
Lambda and Recursive functions in python
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:expressionAn 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 cod...
Function concepts in python Part 2
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:-Required arguments or Positional arguments.Keyword argumentsDefault argumentsVariable Length arguments 1.) Required arguments or Positional argumentsRequired 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 callfunc(5.12,10,'MNT LAB') ...
Function Concepts in Python part 1
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 functionsThere are two types of the functions in python.Built-in FunctionsUser 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...
Dictionary concepts in python
What is Dictionary:Dictionary is a collection of key-value pairs.It is used to store the data in a key-value pairs.It is used to store the data in key-value format.Dictionaries are indexed by keys.Dictionary is the data type in python.Dictionaries are mutable.So we can perform add/delete/modify operations on a dictionary.The keys must be a single element in dictionary.The values can be any type such as list,tuple,integer etc.Dictionaries are also known as maps or associative arrays.The keys in a dictionary must must be unique and immutable.So number,strings and tuples can be used as keys.Though key values are unique,different keys may have same valu...
Set Concepts in Python
What are setsA python set is the collection of unordered elements.Hence order of insertion is not same as the order of access. e.g.
set1=set() #create an empty set
set1={10,20,30,40,50}
print(set1)
Output:-{40, 10, 50, 20, 30}>>> A set does not contain duplicate items like list and tuple.e.g.
#set1 contains duplicate items
set1={10,20,20,10,50,60,60,70,80,30,50}
print(set1)
Output:-{70, 10, 80, 50, 20, 60, 30}>>> A set is mutable (changeable),Means we can change the items after created but set items must be immutable as shown in below example...
Tuple concepts in python
What is Tuple:-Tuple is usually a collection of heterogeneous objects enclosed within ()(small bracket).A tuple is the collection of comma (,)separated values enclosed with small brackets.The parentheses (small bracket) are optional in tuple.e.g.
tuple1=(10,20,180,105)
tuple2=('Horse','Lion','Cow')
tuple3=("MNT LAB")
tuple4=("MNT LAB",)
print(type(tuple1))
print(type(tuple2))
print(type(tuple3))
print(type(tuple4))
Output:-<class 'tuple'><class 'tuple'><class 'str'><class 'tuple'>>>> Properties of Tuple:-There are some properties of tuple as given below:-An empty tuple can created as given belo...
List concepts in python part 2
There are some basic list operations in python as given below.Lists are mutable but strings are immutable.e.g.
animals=['cow','horse','lion','tiger','deer']
numbers=[1,2,3,4,5,6,7,8,9,10]
#Replace list items
animals[4]='dog'
numbers[2:5]=[30,40,50]
print(animals)
print(numbers)
#delete items from 7 to 9 in numbers list
numbers[6:9]=[]
print(numbers)
#clear all items in the numbers list
numbers[:]=[]
print(numbers)
Output:-['cow', 'horse', 'lion', 'tiger', 'dog'][1, 2, 30, 40, 50, 6, 7, 8, 9, 10][1,...
List concepts in python part 1
What is ListA List can store the sequence of various types of data.A list is a container data type.Container is a an entity which contains multiple data items.It is also known as a collection.There are some container data types in python.ListTupleSetDictionaryContainer data type are also known as compound data types.Lists are mutable in python.It means we can change the list element after it created.A list can be collection of Items with different data types(int,string,float etc.).A list can contain similar and dissimilar both data types.The items in the list are separated with comma(,) and enclosed with the square brackets [...
How to use break,continue and pass statements in python with examples
There are three most important statements are used in python program as given below:break statementcontinue statementpass statement1.) The break statement:-The break is a keyword in python.It is used to terminate the loop when we use break statement in any python programs.The break statement is used to abort(terminate) the current execution of the loop and the control goes to the next line after the loo...
How to use for loop repetition control in python with examples
For loop is used to iterate over elements of a sequence such as string,tuple,list and dictionary etc.For loops are used for sequential traversal. For loop has two forms:-1.) for var in list: statement 1 statement 2 statement...
Powered by Blogger.