How many ways are there to declare functions in Python?

How many ways are there to declare functions in Python?
How many ways are there to declare functions in Python?

How many ways are there to declare functions in Python?

What is Function: A Python function is the most fundamental concept of Python in simple words Functions help to group similar tasks.

the function can be reused and they get executes only when they are called that is whenever you wish them to execute.

Definition Function: A Python function is a block of code that performs a particular task and may accept optional user inputs according give outputs.

This block of code is written once and can be executed n the number of times required.

Function: A python function is a collection of statement that performs a specific task. python function executes when it is called by its name.

A large program is divided into some small building blocks for simplicity and this building block is called function.
Python function can be called a function again and again.

Python function's most important feature of function is code reusability. we can be using the def keyword is used to define the function in python.

Syntax of Python Function 

def function_name(parameter list): 
     function_body 


Syntax of Python Function Description 

def: A Python def keyword is used to define a function.

function_name: A Python function is the actual name of the function.
It is also used at the time of calling the function.

Parameter List: A Python function is a place where we can pass some parameters or variables.
The variables may be used in the program.

A python function value of a parameter is passed from the calling of function.

Body: A Python function body is a place where the actual code is written to perform the specific task.


How To  Use Function in Python?

def add(): 
       x=100
       y=200; 
       z=x+y 
       print("Addition of Two Value:",z) 
add()
*****Output*****
Addition of Two Value:400


How To  Use Function With Parameter?

def Language(name): 
        print("I Like",name)

Language("Python","Java","Php")
*****Output*****
I Like Python
I Like Java
I Like Php


How To  Use Function With Parameter?

def Language(name,Fruit): 
        print(name,"Like",fruit)

Fruit("Ravi","Mango")
Fruit("Jitendra","Orange")
Fruit("Vijay","Apple")
*****Output*****
Ravi Like Mango 
Jitendra Like Orange 
Vijay Like Apple


default parameter: A python function can also assign a default value to a parameter.
If the function is called without an argument, it uses the default value.

How To Use Function default Parameter?

def Language(name="Kartik",Fruit="Banana"): 
        print(name,"Like",fruit)

Fruit("Ravi","Mango")
Fruit("Jitendra","Orange")
Fruit()
*****Output*****
Ravi Like Mango 
Jitendra Like Orange 
Kartik Like Banana


Python Function with return type: A Python function can also return a value from the function using return type.A python function 
the return keyword is used to return a value.


How To  Use Function return type

def add(x,y): 
return (x+y)  
print("Adding Value:",add(50,50)) 
print("Adding Value:",add(100,100))

*****Output*****
Adding Value:100
Adding Value:200


Python Argument List: Python Function can also pass a list as an argument.

How To  Use Function Argument List

def Language(data):
    for  name in data:
        print("I Like:",name)
datalist=["Python","Java","Php"]
Language(datalist)

*****Output*****
I Like Python
I Like Java
I Like Php


Python Arbitrary Arguments Function*args: A Python function number of arguments is unknown add asterisk(*) symbol before the parameter name.
Python function can be access the arguments using an index value.

How To  Use Function Argument List

def Language(*data):
      print("I Like:",data[0])
      print("I Like:",data[1])
      print("I Like:",data[2])
Language["Python","Java","Php"]

*****Output*****
I Like Python
I Like Java
I Like Php


Python passes statement: A Python function can be used compulsory to define a function in python means we can not declare a function. So to define a function with no content pass statement is used.

How To Use Pass Function 

def Language(data):
     pass
def Language(data):
      print("I Like:",data)
Language("Python")

*****Output*****
I Like Python


Math function: 
A Python contains the following math functions.

1. sin()
: sin function can be used to get sine of any number in radians.

How To Use sin() Function 

import math
a=math.sin(1)
print("Value Of sin() Function:",a)

*****Output*****
Value Of sin() Function:0.8414709848078965


2. cos()
: cos function can be used to get the cosine of any number in radians.

How To Use cos() Function 

import math
a=math.cos(1)
print("Value Of cos() Function:",a)

*****Output*****
Value Of cos() Function:0.5403023058681398


3. tan()
: tan function can be used to get tangent of any number in radians.

How To Use tan() Function 

import math
a=math.tan(1)
print("Value Of tan() Function:",a)

*****Output*****
Value Of tan() Function:1.5574077246549023


4. sqrt()
: sqrt function can return the square root of any number.

How To Use sqrt() Function 

import math
a=math.sqrt(9)
print("Value Of sqrt() Function:",a)

*****Output*****
Value Of sqrt() Function:3.0


5.log10():log function can return the base-10 logarithm of any number.

How To Use log() Function 

import math
a=math.log(5)
print("Value Of log() Function:",a)

*****Output*****
Value Of log() Function:1.6094379124341003


6. pow(): pow function can be used to the power of any number.

if pow(a,b) then it returns a raised to the power b.


How To Use pow() Function 

import math
a=math.pow(2,5)
print("Value Of pow() Function:",a)

*****Output*****
Value Of pow() Function:32.0


7.factorial():factorial function can return the factorial of any number.

How To Use Factorial() Function 

import math
a=math.factorial(7)
print("Value Of Factorial() Function:",a)

*****Output*****
Value Of Factorial() Function:5040


8. exp():
exp function can return the exponential of any number.

How To Use exp() Function 

import math
a=math.exp(5.7)
print("Value Of exp() Function:",a)

*****Output*****
Value Of exp() Function:298.8674009670603


9. floor(): floor function can return the largest integer less than or equal to a given number.

How To Use floor() Function 

import math
a=math.floor(5.7)
print("Value Of floor() Function:",a)

*****Output*****
Value Of floor() Function:5


10. Ceil (): 
ceil function can return the smallest integer greater than or equal to the given number.

How To Use ceil() Function 

import math
a=math.floor(5.7)
print("Value Of ceil() Function:",a)

*****Output*****
Value Of ceil() Function:6




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.