Sunday, December 2, 2012

Some Python Code



-As usual, Python program to print Hello World:


print("Hello World!")


- Python program to print Fibonnaci using Recursion:

Recursion - The programming technique where function calls itself till it encounters a break condition and then exits. So there must be condition to check the exit condition, or else, the program would loop till infinity which would cause memory overflow error, as the computer has got limited memory.




#To compute Fibonacci number
#using Recursion

def Fib(n):
 
    if n == 0 or n == 1: return 1  
    else: return Fib(n-1) + Fib(n-2)
 
print(Fib(4))


-Python program to reverse a string:


def Str_reverse(Str1):
 
    #return Str1
 
 
    return Str1[::-1]

print(Str_reverse("Hello World!"))







No comments: