Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import my_math.arith.ops
- import my_math.arith.ops.PI
- from math import sqrt as sq
- print(sq(25))
- a= my_math.arith.ops.add(3,5);
- #Function
- def add(n1, n2):
- return n1+n2;
- a = add(4,5);
- print(a)
- def fnc(x, y, z = None):
- x = x * x
- y = y * y
- if z == None:
- z = []
- return x, y, z
- x, y, z = fnc(10, 20, "arfin")
- print(x,y,z)
- #Split, Join, rjust, ljust, center, format
- lis = ['a', 'b', 'c']
- joined = ','.join(lis)
- print(joined)
- li = joined.split(',')
- print(li)
- s = "abc"
- li = list(s)
- s = '+'.join(li)
- print(s)
- for i in range(15):
- print(str(i).rjust(2))
- s = '1000'.rjust(10)
- print(s)
- s = '1000'.center(10)
- print(s)
- s = '12'.zfill(6)
- print(s)
- print("{0} vs {1}".format("BN", "IN"))
- print("{team1} vs {team2}".format(team1 = "BN", team2 = "IN"))
- #FILE I/O
- file = 'text.txt'
- fp = open(file, 'w')
- fp.write("this is us")
- fp.close();
- fp = open(file, 'a')
- fp.write("this is appended")
- fp.close()
- fp = open(file, 'r')
- fp.read()
- fp.close()
- fp = open(file, 'r')
- fp.radline();
- fp.seek(0)
- fp.readline()
- fp.seek(1)
- fp.readline()
- fp.seek(0)
- for line in fp:
- print(line)
- fp.close()
- with open(file, 'r') as fp:
- data = fp.read()
- print(data)
- #Error & exception handling
- def div(a,b):
- try:
- result = a/b
- except ZeroDivisionError:
- print("Division by 0 is not possible")
- except TypeError:
- print("Data type not supported")
- else:
- return result
- finally:
- print("inside finally")
- print(div(4,2))
- div(4,0)
- div('4', 2)
- for i in range(10):
- print(i)
- else:
- print("Inside Else")
- #OOP in python
- #Declare
- class Square:
- side = 0;
- def __init__(self, x):
- self.side = x
- def area(self):
- return self.side * self.side;
- sq = Square(4);
- area = sq.area();
- print(area)
- #Inheirtance
- class Rect:
- x = 0;
- y = 0;
- def __init__(self):
- print("Inside rect")
- def area(self, x, y):
- return self.x * self.y
- class Sq(Rect):
- def __init__(self):
- print("Inside sq")
- sq = Sq()
- print(sq.area(4,4))
- print(sq.__class__)
- print(isinstance(sq, Sq))
- print(isinstance(sq, Rect))
- print(isinstance(sq, Square))
- print(issubclass(Sq,Sq))
- print(issubclass(Sq, Rect))
- print(issubclass(Sq, Square))
- #Iterator
- li = [1,2,3]
- print(li.__iter__)
- city = 'Dhaka'
- print(city.__iter__)
- icity = iter('Dhaka')
- print(next(icity))
- print(next(icity))
- print(next(icity))
- #Doesnt works for some reason :/
- class Reverse:
- """Iterator for looping over a sequence backwards."""
- def __init__(self, data):
- self.data = data
- self.index = len(data)
- def __iter__(self):
- return self
- def next(self):
- if self.index == 0:
- raise StopIteration
- self.index = self.index - 1
- return self.data[self.index]
- rev = Reverse("abcd")
- print(rev)
- next(rev)
- #Generators
- def reverse(data):
- for index in range(len(data)-1, -1, -1):
- yield data[index]
- for ch in reverse('abcd'):
- print(ch)
- gen = reverse('defh')
- print(type(gen))
- for char in gen:
- print(char)
- for char in gen:
- print(char)
- def gen(n):
- i = 0
- while i < n:
- yield i
- i = i + 1
- x = gen(5)
- print(x)
- for item in x:
- print(item)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement