Python modules

 Modules… From the name itself, we know, a module is a set of statements which can perform a functionality. It has properties, functions and methods.

Modules can be of two types

  • ·       Built in modules
  • ·       User defined modules
Built in modules:

These are the modules available in the python library. The declaration and definitions are included in the python itself.

To know about the built in modules, just type help(‘modules’) in the python command prompt.It list outs all the modules in the library.


 User defined module:

A sample module in python should have an extension “.py”. Let us create a python module “mul.py” which multiplies the two numbers given

“mul.py”

def pro(a, b):

 return(a*b)

import mul

mul.pro(2 ,3)

while executing this, the values are multiplied and returned. If you want to print the value, you can use print().

 

Python for beginners - Lambda function in python

Python has some interesting function called “Lambda”. In this function, the user can have any number of arguments in one expression.

The syntax be like,

Lambda arguments: expression

For example, Multiply the variable by 2 and return the result. The code will be given below..

y = lambda b: b * 2

print(y(3))


Next, using 2 arguments in lambda function. It has 2 arguments x, y. we add the values and return it.

a = lambda x, y : x + y

print (a(7,8))

while executing this, you get the below output.

When you use 3 arguments in lambda function, it look like below program.

Let d as the returning value. x,y,z are three variables. The function is add these 3 variables.

d = lambda x, y, z : x + y + z

print(d(3,4,5))

The output is


Lambda function can be used in many ways.

It can be defined once, use it various places.

The function may be called in many times.

But, it can be used in short period of time.

Python for beginners - Python Tuples

 Tuple

Tuple is one of the inbuilt datatypes in python for storing data. The data is stored in ordered way and it cannot be changeable.

The syntax for creating tuple is

Tuplename=(“data1”,”data2”,”data3”)

Example is given below.

mytuple=(“carrot”,”beetroot”,”beans”)

print(mytuple)

While executing this code,the output is look like below.

('carrot', 'beetroot', 'beans')

Features:

Tuple has some features.

Tuples are indexed. The index value starts from 0 to n values.

Once created, it cannot be changed.

Sometimes, same values can be duplicated.

 

Next,the datatypes like int,Boolean and strings are given below.

Da.py

#This is a tuple which contains integer values.

Nutuple=(1,2,3,4,5)

 #This is a tuple which contains Boolean values.

Botuple=(True,False,True)

#This is a tuple which contains string values.

Strtuple=(“happy”,”days”,”today”)

print(Nutuple)

print(Botuple)

print(Strtuple)

while executing this program, the user get the output.



The combination of all data types in a tuple is defined as follows.

mytuple=(“Jeeva”,39,”Maruthi nagar”,”Chennai”)

print(mytuple)

while executing this, you get this output.


Usage of constructor in tuple:

Constructor is used to create a object for a tuple.

Eg:

Mytuple=tuple((“carrot”,”beetroot”,”Beans”)

Print(mytuple)

 The operations on tuple is finding length of a tuple. The coding is given below.



 These are ways of creating a tuple and its operations are explained.