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
def show ():
    print("Welcome to MNT LAB")
#calling the show() function
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.

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

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.

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.

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.

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. 
set1=set() #create an empty set
{40, 10, 50, 20, 30}
  • A set does not contain duplicate items like list and tuple.
#set1 contains duplicate items
{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.

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
tuple3=("MNT LAB")
tuple4=("MNT LAB",)
<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.
Powered by Blogger.