Function

Function:
The function tells Python the name of the function and if applicable, what kind of information the function needs to do its job. Function is a self-contained block of statements that performs a given task. the program we were writing until now had only one function i.e the main function
As a simple example is:

def love_pizza():
print "I love Pizza!"

Adding a new function:
A function definition specifies the name of a new of a new function and the sequence of statements that execute when the function is called.
Here is an example:

def print_lyrics():
print "Im a lumberjack,and Im okay."
print "I sleep all night and I work all day."
def repeat_lyrics():
print_lyrics()
print_lyrics()
>>>repeat_lyrics()
OUTPUT:
Im a lumberjack,and Im okay.
I sleep all night and I work all day.
Im a lumberjack,and Im okay.
I sleep all night and I work all day.

def is a keyword that indicates that this is a function definition. The name of the function is print_lyrics. The rules for function names are the same as for variable names: letters, numbers, and some punctuation marks are legal, but the first character can’t be a number.

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

>>>int('32')
32
e.g:
>>>float(32)
32.0

Translate »