التخطي إلى المحتوى الرئيسي

Function or Procedure in Python

Function or Procedure in Python:-

It is used to subdivide the program based on different sub-routine. The function will be defined by def keyword in Python.

def Functionname():
   statements
   ...


Functionname()  #call to function


Type of Function:-

1  Default:-

we can not pass any parameters to default function, Input variable will be defined under function block.
1.1.1  Without return statement:-  This function will display output under method definition

def Functioname():
    statements
    statements

1.1.2  With return statement:- This function will return output data from the method definition

Def functionname():
    Statements
    Statements
    return statements

data = functionname()

//Program of Default With ReturnType and Without ReturnType
def add():
    a=int(input("enter first number"))
    b=int(input("enter second number"))
    c=a+b
    print(c)
def sub():
    a=int(input("enter first number"))
    b=int(input("enter second number"))
    c=a-b
    return c 

add()
res = sub()
print(res)


2 Parametrized:-

We can pass the value as a parameter from calling a function to called function.


2.1 Default  Argument:-

we will set a default value in "called argument", if we do not pass any value then the default value will be substituted.

def fun1(a=100,b=200):
   print(a+b)

fun1(10,20)
fun1(10)
fun1()
fun1(b=20)


2 Required Argument:-

We should pass an argument from  "Calling Function" to "Called Function" , if we do not pass then it will provide an error.

def functionname(a,b):
     print(a+b)

functionname(10,20)

Example:-

def fun1(a,b):
 print(a+b)

fun1(10,2)

3 Keyword-based Argument:-

We can pass the value as a "Calling argument name" from called function .argument name is called keyword.

def functionname(a,b):
   print(a/b)

functionname(b=2,a=3)

b=2,a=3 is keyword argument

4 variable-length Argument:-

it is also called, call by reference we will use a pointer as a reference variable under calling function.

mostly list and tuple object will be passed a variable-length argument.

def functionname(*a):
   print(a)

b=(10,20,30)
functionname(b)

by default variable-length argument store data using tuple object.

def functionname(b,*a):
   print(b,"\n",a)


functionname(10,20,30)




3 Lambda:-

It is used to declare a function in inline similar to the ternary statement of c and CPP language.

var = lambda argument:result

res =lambda a,b:a+b

res(10,20)





ASSIGNMENT:-

CREATE MULTI DIMENSION LIST USING PARAMETRISED KEYWORD BASED FUNCTION and display complete row which contains the largest element?

Find the Highest value foeach row element in a multi-dimension dictionary using the required parameters?

SWAP TWO NUMBER USING Variable-length arguments?

Calculate Addition of list using lambda function?








تعليقات

المشاركات الشائعة من هذه المدونة

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...