Created
March 2, 2018 13:46
-
-
Save enseitankad0/4c0a654e67121a5db035ac0b8c030981 to your computer and use it in GitHub Desktop.
Exceptions handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##### FUNCTIONS IN PYTHON ### | |
def returning(a): | |
return "I am a result!",a | |
returnedVal = returning(5) | |
print(returnedVal) ## need to extract from brackets | |
def add(a,b=4,d=10): | |
c = a + b + d | |
return c | |
result1=add(10) | |
print(result1) | |
### SCOPE ### | |
def outer(a): | |
def nested(x): | |
return x*a; | |
a = nested(a) | |
return a | |
print(outer(5)) ##5*5 | |
def f(a): | |
def g(b): | |
return a*b; | |
return g | |
print(f(4)(24)) | |
### RECUIRSIVE FUNCTIONS ### | |
def factorial(n): | |
if n ==1: | |
return 1 | |
else: | |
return n* factorial(n-1) # 5*4*3*2*1 | |
print(factorial(5)) | |
def tailSum (n,acuumulator=0): | |
if n==0: | |
return acuumulator | |
else: | |
print(n,acuumulator) | |
return tailSum(n-1, acuumulator+n) | |
print(tailSum(5)) | |
## LAMBDA | |
f = lambda a,b: a+b ## no return statement needed | |
print(f(2,3)) | |
g = lambda a: lambda b, c: lambda d: (a * b * c * d) | |
print(g(5)(3,3)(2)) | |
### EXCEPTION HANDLING ### | |
# try block | |
# try: | |
# a=6/0 | |
# except Exception as e: | |
# print(e) | |
# | |
# try: | |
# n = str(input("Enter an string: ")) | |
# except ValueError: | |
# print ("That is not an string") | |
# try: | |
# n = int(input("Enter an integer: ")) | |
# except ValueError: | |
# print ("That is not an integer") | |
10 | |
# try: | |
# sum = 0 | |
# file = open('numbers.txt','r') | |
# for number in file: | |
# sum = sum +1.0/int(number) | |
# print(sum) | |
# except ZeroDivisionError: | |
# print("Number in file equals to zero") | |
# except IOError: | |
# print('File DNE') | |
### THROWING EXCEPTION ### | |
a = 1 | |
# def RaiseException(a): | |
# if type(a) != type('a'): | |
# raise ValueError("This is not string !") | |
# try: | |
# RaiseException(a) | |
# except Exception as e: | |
# print(e) | |
def TestCase( a, b): | |
assert a < b, "a is a greater than b" | |
try: | |
TestCase(32,4) | |
except AssertionError as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment