Python file handling

By // No comments:

  • 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 the file ,we  need to open it first.
  • After performing the read ,write and modification in the file,we have to close the file.
Hence a file operation can be done in the following order.
  1. Open a file.
  2. Read or write(operations)
  3. Close the file.
3

Exception Handling in python with examples

By // No comments:

 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:-
  1. During compilation
  2. During execution

  • Error 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 code.
3

Encapsulation In Python with examples

By // No comments:

 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 the data security and avoids the access the data accidentally.
Python allows us to access the method and variables from the outside the class as given below:-
3

Polymorphism concepts in python with example

By // No comments:

  • 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 :-
  1. Compile time polymorphism (e.g. method overloading)
  2. 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 time.
3

Inheritance Concepts in python with example

By // 1 comment:

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 code.
3

oops concepts in python with real life example

By // 1 comment:

  •  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.
  1. class 
  2. object
  3. method
  4. Inheritance
  5. Polymorphism
  6. Encapsulation
  7. Abstraction
1.) Class:
  • A class is a blue print of an object.
  • A class is a collection of data and methods
3

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

Dictionary concepts in python

By // No comments:

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 value.
3

Set Concepts in Python

By // No comments:
What are sets
  • A 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 examples.
3

Tuple concepts in python

By // No comments:
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 below.
3

List concepts in python part 2

By // No comments:
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, 2, 30, 40, 50, 6, 10]
[]
>>> 
  • The Repetition operator is used to repeat  the elements in the list multiple times as given below.
e.g.
list1=[10,20,30,40,50]
list2=list1*2
print(list2)
Output:-
[10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
>>> 
  • The concatenation operator is used to concatenate the one list with other list.
e.g.
list1=[10,20,30,40,50]
list2=['a','b','c','d','e','f']
list3=list1+list2
print(list3)
Output:-
[10, 20, 30, 40, 50, 'a', 'b', 'c', 'd', 'e', 'f']
>>> 
  • The membership operator returns True if a particular element exists in a list otherwise False.
e.g.
list=['a','e','i','o','u']
print('a' in list)
print('o' in list)
print('p' in list)
Output
True
True
False
  • A list can be iterated by using for loop as given below.
e.g.
list=['Ram',20,3.5,'lion','neha']
for i in list:
    print(i)
    #print(i,end=" ")
Output:
Ram
20
3.5
lion
neha
>>> 

There are some basic operations can be performed on a list.
  • Create a list
e.g.
list=['Ram',20,3.5,'lion','neha']
  • Concatenation (addition) of two more lists.
e.g.
list1=[10,20,30,40,50]
list2=[60,70,80,90,100]
list=list1+list2
print(list)
Output:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
>>> 
  • del() function is used to delete the elements in the list.
e.g.
list1=[10,20,30,40,50,60,70,80,90,100]
#delete the 3rd index value from the list
del(list1[3])
print(list1)
#delete the 6th index to last index from the list
del(list1[6:])
print(list1)
#delete the whole list
del(list1[:])
print(list1)
Output:
[10, 20, 30, 50, 60, 70, 80, 90, 100]
[10, 20, 30, 50, 60, 70]
[]
>>> 
  • len() function is used to calculate the number of items in given list.
e.g.
list1=[10,20,30,40,50,60,70,80,90]
a=len(list1)
print("Lenght of given list is = ",a)
Output:
Lenght of given list is =  9
>>> 
  • list() function is used to convert the string to a list.
e.g.

str="AMERICA"
a=list(str)
print("Convert the string value to the list value: ",a)
Output:
Convert the string value to the list value:  ['A', 'M', 'E', 'R', 'I', 'C', 'A']
>>> 
  • max() function is used in python to return the maximum element in the list.
e.g.
str1=['A','M','E','R','I','C','A']
str2=[1,2,3,4,5,6,7,8]
print("Maximum value of list is=",max(str1))
print("Maximum value of list is=",max(str2))
Output:-
Maximum value of list is= R
Maximum value of list is= 8
>>> 
  • min() function is used in python to return the minimum element in the list.
e.g.
str1=['A','M','E','R','I','C','A']
str2=[1,2,3,4,5,6,7,8]
print("Minimum value of list is=",min(str1))
print("Minimum value of list is=",min(str2))
Output:-
Minimum value of list is= A
Minimum value of list is= 1
>>> 
  • sorted() function is used to return the sorted list without any change in the list.
e.g.
str1=['A','M','E','R','I','C','A']
str2=[70,50,60,40,20,80,30,40]
print("Sorted value of the list is=",sorted(str1))
print("Sorted value of the list is=",sorted(str2))
Output:-
Sorted value of the list is= ['A', 'A', 'C', 'E', 'I', 'M', 'R']
Sorted value of the list is= [20, 30, 40, 40, 50, 60, 70, 80]
>>> 
  • sum() function is used to return the sum of all the list values.
e.g.
str1=[70,50,60,40,20,80,30,40]
print("sum of all list value is=",sum(str1))
Output:-
sum of all list value is= 390
>>> 
  • Comparison between two list is possible.Comparison is done item by item till there is mismatch.
e.g.
a=[1,5,10,15,20]
b=[1,5,10,30,3]
if(a<b):
    print("a is less than b")
elif(a==b):
    print("a is equal to b") 
else:
     print("b is less than a")
Output:-
a is less than b
>>> 

List Methods:- 
List methods are accessed using the syntax list.function().
There are important methods used in list operations as given below:-
  • list.append():-This is used to add the new items at the end of the list.This Method takes only one argument.
e.g.
lst=[10,20,30,40,50,60,70]
print(lst)
lst.append(90)
print(lst)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 50, 60, 70, 90]
>>> 
  • list.remove():-This method is used to delete the item from the list.
e.g.
lst=[10,20,30,40,50,60,70]
print(lst)
lst.remove(40)
print(lst)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 50, 60, 70]
>>> 
  • list.pop():-This method is used to remove the last item from the list.
e.g.
list=[10,20,30,40,50,60,70]
print(list)
list.pop()
print(list)
#delete the 4th index value in the list
list.pop(4)     
print(list)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 60]
>>> 
  • list.insert():-This method is used to insert the element at the specified position in the list.This method takes two argument.first argument denote the index value and second argument denote item which you want to add in the given list.
e.g.
list=[10,20,30,40,50,60,70]
print(list)
#insert the item at the 4th index in the list
list.insert(4,122)
print(list)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 122, 50, 60, 70]
>>> 
  • list.reverse():-This is used to reverse the element in the list.
e.g.
list=[2,4,6,8,10,12,14,16]
list.reverse()
print(list)
Output:-
[16, 14, 12, 10, 8, 6, 4, 2]
>>> 
  • list.sort():-This is used to sort the items in the list.
e.g.
list=[70,20.5,40,50,60,16]
list.sort()
print(list)
Output:-
[16, 20.5, 40, 50, 60, 70]
>>>  
  • list.count():-This is used to return the number of times element appears in the list.
e.g.
list=[16,40,40,50,60,70,40,20,40]
a=list.count(40)
print("Above given number present in the list: ",a)
Output:-
Above given number present in the list:  4
>>> 
  • list.index():-This used to find the index of the particular item in the list.
e.g.
list=[16,40,50,60,70,20,40]
a=list.index(70)
print("70 is present in the list at index : ",a)
Output:-
70 is present in the list at index :  4
>>> 
List Varieties:-
There are some list varieties as given below:-
  • It is possible to create new list from another.
e.g.
list1=[16,40,50,60,70,20,40]
list2=list1
print(list1)
print(list2)
Output:-
[16, 40, 50, 60, 70, 20, 40]
[16, 40, 50, 60, 70, 20, 40]
>>> 
Note:-list1 and list2 are pointing the same list.
  • It is possible to create a list of lists.
e.g.
a=[1,3,5,7,9,11,13]
b=[2,4,6,8,10,12,14]
c=[a,b]
print(c[0][4],c[1][6])
#c[0][4]-> 0th list(first list) of 4th index
#c[1][6]-> 1th list(second list) of 6th index
Output:-
9 14
>>>
  • It is possible that a list may be embedded in another list.
e.g.
a=[1,3,5,7,9,11,13]
b=[2,a,4,6,8,10,12,14]
print("After embedded list is: ",b)
Output:-
After embedded list is:  [2, [1, 3, 5, 7, 9, 11, 13], 4, 6, 8, 10, 12, 14]
>>> 
  • It is possible to unpack a list within a list using the * operator.
e.g
a=[1,3,5,7,9,11,13]
b=[2,4,6,*a,8,10,12]
print("After unpacked list is: ",b)
Output:-
After unpacked list is:  [2, 4, 6, 1, 3, 5, 7, 9, 11, 13, 8, 10, 12]
>>> 
Program:1 
How to add items in empty list by user manually.
#declare the empty list
list=[]
num=int(input("Enter the number of elements in the list : "))
for i in range(0,num):
    #The input is taken by user and add to the list
    list.append(input("Enter the value:"))
#print the list
for j in list:
    print(j,end="")
Output:-
Enter the number of elements in the list : 10
Enter the value:1
Enter the value:2
Enter the value:3
Enter the value:4
Enter the value:5
Enter the value:6
Enter the value:7
Enter the value:8
Enter the value:9
Enter the value:10
12345678910
>>> 
Program:2
Write a program to remove the duplicate items of the list.
l1=[10,20,30,40,50,20,20,70,10,20,30]
#declare the empty list
l2=[]
for i in l1:
    if i not in l2:
        l2.append(i)

print("After remove duplicate item, list is= ",l2)
Output:-
After remove duplicate item, list is=  [10, 20, 30, 40, 50, 70]
>>> 
Program:3
Write a program to find the common element between two list.
l1=[1,3,5,7,9,11]
l2=[2,4,5,8,10,12,9]
for i in l1:
    for j in l2:
        if(i==j):
            print("The common element is: ",i)
Output:-
The common element is:  5
The common element is:  9
>>> 
For More..
Watch complete Lecture 25 Video:-


3

List concepts in python part 1

By // No comments:
What is List
  • A 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.
  1. List
  2. Tuple
  3. Set
  4. Dictionary
  • Container 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 [].
3

How to use break,continue and pass statements in python with examples

By // No comments:
There are three most important statements are used in python program as given below:
  1. break statement
  2. continue statement
  3. pass statement
1.) 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 loop.
3

How to use for loop repetition control in python with examples

By // No comments:
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 3
3
Powered by Blogger.