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.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.
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 PythonI Like JavaI 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
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:100Adding Value:200
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 PythonI Like JavaI Like Php
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 PythonI Like JavaI Like Php
How To Use Pass Function
def Language(data): passdef Language(data): print("I Like:",data)Language("Python")
*****Output*****I Like Python
1. sin(): sin function can be used to get sine of any number in radians.
How To Use sin() Function
import matha=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 matha=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 matha=math.tan(1)print("Value Of tan() Function:",a)
*****Output*****Value Of tan() Function:1.5574077246549023
How To Use sqrt() Function
import matha=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 matha=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.