vakho

Prog. Langs (Python 3) 4

Apr 16th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. # Hello World program in Python
  2.  
  3. # class definition
  4. class Figure:
  5.     def __init__(self, x, y):
  6.         self.x = x
  7.         self.y = y
  8.        
  9.     # Override
  10.     def __str__(self):
  11.         return 'x = ' + str(self.x) + ', y = ' + str(self.y) + '.'
  12.  
  13.     def move(self, x, y):
  14.         if self.verify(x, y):
  15.             self.x = x
  16.             self.y = y
  17.  
  18.     def checkBorders(self, x, y):
  19.         if x > 8 or x < 1 or y > 8 or y < 1:
  20.             return 0
  21.         return 1
  22.  
  23. class Rook(Figure):
  24.     def verify(self, x, y):
  25.         return (self.checkBorders(x, y) and self.x == x) or (self.checkBorders(x, y) and self.y == y)
  26.  
  27. e = Rook(1, 1)
  28. print(e)
  29. e.move(1, 8)
  30. print(e)
  31.  
  32. class Triangle:
  33.     def __init__(self, a, b, c):
  34.         self.a = a
  35.         self.b = b
  36.         self.c = c
  37.        
  38.     def area(self):
  39.         p = (self.a + self.b + self.c) / 2
  40.         return p * (p - self.a) * (p - self.b) * (p - self.c)
  41.  
  42. class Prism(Triangle):
  43.     def __init__(self, a, b, c, h):
  44.         super().__init__(a, b, c)
  45.         self.h = h
  46.  
  47.     def area(self):
  48.         return 2 * super().area() + (self.a + self.b + self.c) * self.h
  49.        
  50. p = Prism(3, 4, 5, 6)
  51. print(p.area())
  52.        
  53. t = Triangle(3, 4, 5)
  54. print(t.area())
  55.  
  56.  
  57.  
  58. # function should be defined before its usage
  59. def GCD(a, b):
  60.     while a != b:
  61.         if a > b:
  62.             a = a - b
  63.         else:
  64.             b = b - a
  65.     return a
  66.            
  67. print (GCD (125, 75))
  68.  
  69. # object definition (json like)
  70. b = {
  71.     'fname': 'Vakhtangi',
  72.     'lname': 'Laluashvili',
  73.     'age': 22
  74. }
  75.  
  76. b['weight'] = 58 # add dinamically
  77.  
  78. for c in b:
  79.     print (c, ": ", b[c])
  80.    
  81. a = [2, 3, 5, 7, 11]
  82. a = a + [13, 17, 19, 23]
  83.  
  84. for c in a:
  85.     print (c)
Advertisement
Add Comment
Please, Sign In to add comment