Functions

1- Python built-in functions

2- Build Your Own Functions

3- Help() Function

4- Import functions

  • A function is a piece of code that you can save and reuse it in other progrmas.

  • You can even use functions written by others (available in the Interent)

  • A function takes some input and produces an output

Python built-in functions

- len

Returns the length of input

Example:

In [3]:
A = "Hello world"
In [4]:
len(A)
Out[4]:
11
In [8]:
movie_rating = [10,9.5,4,3.5,10,9,5,5]
In [7]:
len(movie_rating)
Out[7]:
8

- Sum

Returns total of inputs

Example:

In [12]:
movie_rating = [10,9.5,4,3.5,10,9,5,5]
In [10]:
sum(movie_rating)
Out[10]:
56.0

- Sort & Sorted

Sorts the input

Example: (Sorted)

In [12]:
movie_rating = [10,9.5,4,3.5,10,9,5,5]
In [14]:
sorted(movie_rating)
Out[14]:
[3.5, 4, 5, 5, 9, 9.5, 10, 10]

Example: (Sort)

In [17]:
movie_rating = [10,9.5,4,3.5,10,9,5,5]
In [21]:
# it justs sorts the list but not creats a new list

movie_rating.sort()
In [20]:
movie_rating
Out[20]:
[3.5, 4, 5, 5, 9, 9.5, 10, 10]

Build Your Own Functions

How to define a function:

  • To define a function we use def + name of our function + formal parameter X in parentheses

  • Define a function -> def

  • Name of our function -> sub

  • Formal parameters in parentheses followed by a colon(:) -> (n):

      - Functions can have multiple parameters (n,m): 
  • Inside function we have our code block with indent -> be carefull with the indent

  • Return -> returns the output

  • """ Documentation """ -> Explain what this block of codes does -> """subtract 100 from n"""

  • Call the function and give it a value to see the output -> sub(200)

Example

In [88]:
def sub(n):
    """subtract 100 from n"""
    m = n - 100  

    return m 

sub(200)
Out[88]:
100

Example: (Function with one parameter)

In [56]:
def sub(n):  # Function has one parameter or inputs

    """subtract 100 from n"""
    
    m = n - 100   #notice the code with indent inside function

    return m      #notice the code with indent inside function
In [53]:
# our input is 600, we call sub function and give it our input -> it returns output 500

sub(600)
Out[53]:
500
In [54]:
# our input is 1000, we call sub function give it our input -> it returns output -60

sub(40)
Out[54]:
-60

Example: (Function with multiple parameters)

In [58]:
def mult(a,b):
    
    c = a*b
    return c
In [61]:
# we have two parameters or inputs (two int)

mult(10,9)
Out[61]:
90
In [63]:
# We have two parameters or inputs (one int, one float)

mult(5,2.5)
Out[63]:
12.5
In [68]:
# We have two parameters or inputs (one int, one string)

# print hello, three times

mult(3, 'hello,')
Out[68]:
'hello,hello,hello,'

Help() Function

Use help() function to display the documentation

In [55]:
help(sub)
Help on function sub in module __main__:

sub(n)
    subtract 100 from n

Example (for loops)

In [83]:
def staff_name(names):
    for name in names:
        print(name)
    
company_staff= ['sara', 'john' , 'smith' ]
staff_name(company_staff)
sara
john
smith

Example (for loops)

In [82]:
def staff_list(staff):
    for i,j in enumerate(staff):
        print('Staff', i , 'is', j)
    
company_staff= ['sara', 'john' , 'smith' ]
staff_list(company_staff)
Staff 0 is sara
Staff 1 is john
Staff 2 is smith

Import functions : It will be added later

In [ ]: