Python functions
Python functions

Python functions begin with keywork def, then a function name, and list of parameters that function takes. The general syntax of Python function is :

def function_name (param1, param2): statement 1 statement 2 statement n
Function Examples
def fibnocci (c):
    p = 1
    n = p + 1
    count = 1
    print (p, end = ', ')
    print (n, end = ', ')
    while count <= c:
        print (n + p, end = ', ')
        n = n + p
        p = n - p
        count += 1
    print ()


print ("Fibnocci 1 :- ", end = ' ')
fibnocci (15)
def fibnocci2 (c):
    p = 1
    n = 2

    count = 1
    while count <= c:
        print (p, end = ', ')
        p, n = n, p + n
        count += 1
    print ()

print ("\nFibnocci 2 :- ", end = ' ')        
fibnocci2 (15)
Function which returns a value
def function_name (param1, param2):
    statements
    return value
Example : Function which returns a value
def fun_with_return (a, b):
    c = a + b
    return c

x = fun_with_return (3, 2)
print (x)
Function with default parameter values
def function_name (param1 = "val1", param2 = "val2"):
Example : Function with default parameter values
def printHello (str1 = "hello", str2 = "Good morning"):
    print (str1 + " " + str2)

printHello ()
printHello ("Hi")
printHello (str2 = "Good night")
printHello ("Hey", "Good night")
Python Lists
sq = [1, 2, 4, 9, 16, 25]

print (sq)

print (sq[0])

print (sq[:])

print (sq[-3:])

print (sq[:3])

print (sq[3:])

print (sq[3:5])

sq1 = sq[:]

print (sq1)

sq.append (36)
print (sq)

sq[:] = []
print (sq)

print (len (sq1))
print (sq1.pop ())
Comments