Advance python
# try
'''
while(True):
print("press q to quit")
a = input("Enter a number:")
if a == 'q':
break
try:
print("trying...")
a = int(a)
if a>6 :
print("you enter a number greater than 6")
except Exception as e:
print(f"your input required in an error : {e}")
print("thanks")
'''
# handling different exception
'''
try:
a = int(input("enter a number: "))
c = 1/a
print(c)
except ValueError as e:
print("Exception1 occured")
print(e)
except ZeroDivisionError as e:
print("Exception2 occured")
print(e)
print("Thanks")
'''
# raising exception
#try with else
'''
try:
i = int(input('enter nuber'))
c = 1/i
except Exception as e:
print(e)
else:
print("successful")
'''
# try with finally
'''
try:
i = int(input('enter nuber'))
c = 1/i
except Exception as e:
print(e)
finally:
print("Done")
'''
# Globle variable
'''
a = 54 #Globle variable
def func1():
global a
print(f"Print statement 1: {a}")
a = 3 # local variable if globle keyword is not used
print(f"Print statement 2: {a}")
func1()
print(f"Print statement 3: {a}")
'''
# enumerate function
'''
list1 = [1,2,4,False, 'onam']
# index = 0
# for item in list1:
# print(item,index)
# index += 1
for index, item in enumerate(list1):
print(item,index)
'''
# List comprehension
a = [2,1,4,5,67,65,43,88,92,8]
# b =[]
# for item in a:
# if item %2 == 0:
# b.append(item)
# print(b)
# shortcut to write the above code
b = [i for i in a if i % 2 ==0]
print(b)
Comments
Post a Comment