12/03/2022

In Python, a function is some reusable code that takes arguments(s) as input does some computation, and then returns a result.

We call/invoke the function by using the function name, parenthesis, and arguments in an expression

Types of Function

There are two kinds of functions in Python.
Built-in functions that are provided as part of
Python – raw_input(), type(), float(), int()
Functions that we define ourselves and then use

Function Calls

  1. firstly, A function is a named sequence of statements that performs a computation.
  2. Secondly, When you define a function, you specify the name and the sequence of statements.
  3. Thirdly, you can “call” the function by name.
    Example: >>> type(32)
  4. Fourthly, The name of the function types.
  5. Then, The expression in parentheses is called the argument of the function.
  6. At the last, It is common to say that a function “takes” an argument and “returns” a result. The result is called the return value

Type Conversion Function in python


Python provides built-in functions that convert values from one type to another. So, The int function
takes any value and converts it to an integer, if it can,
or complains otherwise:

so open your notepad

>>> int('32')
32 
>>> int('Hello')



ValueError: invalid literal for int(): Hello• int can convert floating-point values to
integers, but it doesn’t round off; it off the
fraction part:

• >>> int(3.99999)
3
• >>> int(-2.3)


-2• float converts integers and strings to floating-point numbers:

• >>> float(32)
32.0
• >>> float('3.14159')
3.14159


• Finally, str converts its argument to a string:

• >>> str(32)
'32'
• >>> str(3.14159)
'3.14159'

Math Functions in python

Python has a math module that provides most of the familiar mathematical functions. So, A module is a file that contains a collection of related functions.

Before we can use the module, we have to import it:

>>> import math

• This statement creates a module object named math. If you print the module object, you get some information about it:

>>> print math 

• Firstly, The module object contains the functions and variables defined in the module. To access one of the functions, you have to specify the name of the module and the name of the function, separated by a dot (also known as a period). At the last, This format is called dot notation.

• Example 1:

>>> ratio = signal_power / noise_power 
>>> decibels = 10 * math.log10(ratio) 

• Example 2:

>>> radians = 0.7 
>>> height = math.sin(radians)

Composition

One of the most useful features of programming languages is their ability to take small building blocks and then, compose them. For example, the argument of the function can be any kind of expression, including arithmetic operators:

x = math.sin(degrees / 360.0 * 2 * math.pi)
And even function calls:
x = math.exp(math.log(x+1))

Almost anywhere you can put a value, So, you can put an arbitrary expression, with one exception: the left side of an assignment statement has to be a variable name. Any other expression on the left side is a syntax error (we will see exceptions to this rule later).

>>> minutes = hours * 60 # right
>>> hours * 60 = minutes # wrong!
SyntaxError: can't assign to operator

Adding New Function in python

A function definition specifies the name of a new function and the sequence of statements that execute when the function is called.

Syntax:

def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
def printme( str ): 
 "This prints a passed string into this function“
 print str 
 return

Here is an example:
def print_lyrics():
print “I’m a lumberjack, and I’m okay.”
print “I sleep all night and I work all day.”

def is a keyword that indicates that this is a function definition.

programming, computer, icon, function in python

firstly, The name of the function is print_lyrics.

Secondly, The empty parentheses after the name indicate that this function doesn’t take any arguments.

Thirdly, The first line of the function definition is called the header; the rest is called the body.

Fourthly, The header has to end with a colon.

The strings in the print statements are enclosed in double-quotes.

Single quotes and double quotes do the same thing; most people use single quotes except in cases like this where a single quote (which is also an apostrophe) appears in the string

Adding New Functions using def

def print_lyrics():
… print “I’m a lumberjack, and I’m okay.” then,
… print “I sleep all night and I work all day.“

To end the function, So, you have to enter an empty line (this is not necessary for a script).

Defining a function creates a variable with the same name, then.

>>> print print_lyrics
<function print_lyrics at 0xb7e99e9c>
>>> type(print_lyrics)
<type 'function'>

The value of print_lyrics is a function object, which has the type ‘function’, so.

The syntax for calling the new function is the same as for built-in functions, so:

>>> print_lyrics()

I’m a lumberjack, and I’m okay.
I sleep all night and I work all day.

Once you have defined a function, you can use it inside another function. For example, to repeat the previous refrain, we could write a function called repeat_lyrics:

def repeat_lyrics():
print_lyrics()
print_lyrics()
And then call repeat_lyrics:
>>> repeat_lyrics()

I’m a lumberjack, and I’m okay.
I sleep all night and I work all day.
I’m a lumberjack, and I’m okay.
I sleep all night and I work all day.

for more studies visit our website:-https://learningpoints.in/

Leave a Reply

Your email address will not be published. Required fields are marked *