p1Point456 p2Point 2 14 printp1distancep2 Task 4 import math class Point def

P1point456 p2point 2 14 printp1distancep2 task 4

This preview shows page 3 - 6 out of 8 pages.

p1=Point(4,5,6) p2=Point(-2,-1,4) print(p1.distance(p2)) Task 4 -------- import math class Point: def __init__(self,x,y,z): self.x=x self.y=y self.z=z def __str__(self): return 'point: ({self.x} {self.y} {self.z})'.format(self=self) def distance(self,a): dist = math.sqrt( (self.x-a.x)**2 + (self.y-a.y)**2 + (self.z-a.z)**2 ) def __add__(self,a): add=Point(self.x+a.x,self.y+a.y,self.z+a.z) return add p1=Point(4,5,6) p2=Point(-2,-1,4) print(p1+p2) Hands on 5 ----------- Task 1 ------- def iseven(a): if a%2==0: return True else: return False print(iseven(43)) Task 2 ------ import unittest class TestIsEvenMethod(unittest.TestCase): def iseven(a): if a%2==0: return True else:
Image of page 3
return False def test_isEven1(self): self.assertEqual(iseven(5),False) unittest.main() Task 3 ------ import unittest class TestIsEvenMethod(unittest.TestCase): def iseven(a): if a%2==0: return True else: return False def test_isEven1(self): self.assertEqual(iseven(5),False) def test_isEven2(self): self.assertEqual(iseven(10),True) unittest.main() Task 4 ------ import unittest class TestIsEvenMethod(unittest.TestCase): def iseven(a): if a%2==0: return True else: return False def test_isEven1(self): self.assertEqual(iseven(5),False) def test_isEven2(self): self.assertEqual(iseven(10),True) def test_isEven3(self): self.assertRaises("hello",TypeError) unittest.main() Hands on 6 ---------- n=int(input("enter a num")) try: if n<0 or n>100: raise ValueError except ValueError: print("Input integer value must be between 0 and 100.") Hands on 7 --------- n=input("enter a String") try: if len(n)>10: raise ValueError else: print(n) except ValueError:
Image of page 4
print("Input String contains more than 10 characters.") Hands on 8 ---------- import os try: if not os.path.isfile("unknown_file.txt"): raise FileNotFoundError except: print("File not found") Hands on 9 ---------- class Circle(Exception): def __init__(self,r): try: if not isinstance(r, int): raise RadiusInputError else: self.radius=r except: print("hello' is not a number") c=Circle("hello") Hands on 11 ---------- import os for files in os.walk(".", topdown=False): for name in files: if ".py" in str(name): print(name) Hands on 12 ---------- import calendar def find_five_sunday_months(year): calendar.setfirstweekday(calendar.SUNDAY) five_sunday_months = [] for month in range(1, 13): calendar_month = calendar.monthcalendar(year, month) # If you're counting Sunday as the first day of the week, then any month that extends into # six weeks, or starts on a Sunday and extends into five weeks, will contain five Sundays.
Image of page 5
Image of page 6

You've reached the end of your free preview.

Want to read all 8 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture