Advertisement
arfin97

ref - Python

Feb 18th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. import my_math.arith.ops
  2. import my_math.arith.ops.PI
  3. from math import sqrt as sq
  4. print(sq(25))
  5.  
  6. a= my_math.arith.ops.add(3,5);
  7.  
  8. #Function
  9. def add(n1, n2):
  10.     return n1+n2;
  11.  
  12. a = add(4,5);
  13. print(a)
  14.  
  15. def fnc(x, y, z = None):
  16.     x = x * x
  17.     y = y * y
  18.     if z == None:
  19.         z = []
  20.     return x, y, z
  21.  
  22. x, y, z = fnc(10, 20, "arfin")
  23. print(x,y,z)
  24.  
  25. #Split, Join, rjust, ljust, center, format
  26. lis = ['a', 'b', 'c']
  27. joined = ','.join(lis)
  28. print(joined)
  29.  
  30. li = joined.split(',')
  31. print(li)
  32.  
  33. s = "abc"
  34. li = list(s)
  35. s = '+'.join(li)
  36. print(s)
  37.  
  38. for i in range(15):
  39.     print(str(i).rjust(2))
  40.  
  41. s = '1000'.rjust(10)
  42. print(s)
  43. s = '1000'.center(10)
  44. print(s)
  45. s = '12'.zfill(6)
  46. print(s)
  47.  
  48. print("{0} vs {1}".format("BN", "IN"))
  49. print("{team1} vs {team2}".format(team1 = "BN", team2 = "IN"))
  50.  
  51. #FILE I/O
  52. file = 'text.txt'
  53. fp = open(file, 'w')
  54. fp.write("this is us")
  55. fp.close();
  56. fp = open(file, 'a')
  57. fp.write("this is appended")
  58. fp.close()
  59. fp = open(file, 'r')
  60. fp.read()
  61. fp.close()
  62. fp = open(file, 'r')
  63. fp.radline();
  64. fp.seek(0)
  65. fp.readline()
  66. fp.seek(1)
  67. fp.readline()
  68. fp.seek(0)
  69. for line in fp:
  70.     print(line)
  71. fp.close()
  72.  
  73. with open(file, 'r') as fp:
  74.     data = fp.read()
  75.     print(data)
  76.  
  77. #Error & exception handling
  78.  
  79. def div(a,b):
  80.     try:
  81.         result = a/b
  82.     except ZeroDivisionError:
  83.         print("Division by 0 is not possible")
  84.     except TypeError:
  85.         print("Data type not supported")
  86.     else:
  87.         return result
  88.     finally:
  89.         print("inside finally")
  90.  
  91. print(div(4,2))
  92. div(4,0)
  93. div('4', 2)
  94.  
  95. for i in range(10):
  96.     print(i)
  97. else:
  98.     print("Inside Else")
  99.  
  100.  
  101.  
  102.  
  103. #OOP in python
  104.  
  105. #Declare
  106. class Square:
  107.     side = 0;
  108.     def __init__(self, x):
  109.         self.side = x
  110.     def area(self):
  111.         return self.side * self.side;
  112.  
  113. sq = Square(4);
  114. area = sq.area();
  115. print(area)
  116.  
  117. #Inheirtance
  118.  
  119. class Rect:
  120.     x = 0;
  121.     y = 0;
  122.     def __init__(self):
  123.         print("Inside rect")
  124.     def area(self, x, y):
  125.         return self.x * self.y
  126.  
  127. class Sq(Rect):
  128.     def __init__(self):
  129.         print("Inside sq")
  130.  
  131. sq = Sq()
  132. print(sq.area(4,4))
  133.  
  134. print(sq.__class__)
  135.  
  136. print(isinstance(sq, Sq))
  137. print(isinstance(sq, Rect))
  138. print(isinstance(sq, Square))
  139.  
  140. print(issubclass(Sq,Sq))
  141. print(issubclass(Sq, Rect))
  142. print(issubclass(Sq, Square))
  143.  
  144.  
  145. #Iterator
  146. li = [1,2,3]
  147. print(li.__iter__)
  148.  
  149. city = 'Dhaka'
  150. print(city.__iter__)
  151.  
  152. icity = iter('Dhaka')
  153. print(next(icity))
  154. print(next(icity))
  155. print(next(icity))
  156. #Doesnt works for some reason :/
  157. class Reverse:
  158.     """Iterator for looping over a sequence backwards."""
  159.     def __init__(self, data):
  160.         self.data = data
  161.         self.index = len(data)
  162.     def __iter__(self):
  163.         return self
  164.     def next(self):
  165.         if self.index == 0:
  166.             raise StopIteration
  167.         self.index = self.index - 1
  168.         return self.data[self.index]
  169.  
  170. rev = Reverse("abcd")
  171. print(rev)
  172. next(rev)
  173.  
  174.  
  175. #Generators
  176. def reverse(data):
  177.     for index in range(len(data)-1, -1, -1):
  178.         yield data[index]
  179.  
  180. for ch in reverse('abcd'):
  181.     print(ch)
  182.  
  183. gen = reverse('defh')
  184. print(type(gen))
  185.  
  186. for char in gen:
  187.     print(char)
  188.  
  189. for char in gen:
  190.     print(char)
  191.  
  192. def gen(n):
  193.     i = 0
  194.     while i < n:
  195.         yield i
  196.         i = i + 1
  197. x = gen(5)
  198. print(x)
  199.  
  200. for item in x:
  201.     print(item)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement