- file IO
- how to read from a file on disk
- how to write to a file on disk
Burkay Genç, Ahmet Selman Bozkır, and Selma Dilek
24/05/2023
3
is an object'3'
is an object"Three"
is an object3.141592
is an object[1,2,3]
is an object(1,2,3)
is an object{1:'a', 2:'b', 3:'c'}
is an objecttype(3)
## <class 'int'>
type('3')
## <class 'str'>
type("Three")
## <class 'str'>
type(3.141592)
## <class 'float'>
type([1,2,3])
## <class 'list'>
type((1,2,3))
## <class 'tuple'>
type({1:'a', 2:'b', 3:'c'})
## <class 'dict'>
class Human: def __init__(self, age, gender, height, weight): self.Age = age self.Gender = gender self.Height = height self.Weight = weight
__init__(self)
function is a special function used to create an object instance from a class
__init__
and its first argument is always self
self
means the object’s selfAhmet = Human(23, 'Male', 170, 65) print(Ahmet)
## <__main__.Human object at 0x000002D5BBC98A10>
Ahmet
:print(Ahmet.Age)
## 23
print(Ahmet.Gender)
## Male
Ahmet
as an argument to a function:def isTeenager(human): if human.Age < 20: return True else: return False isTeenager(Ahmet)
## False
isTeenager
function only works, if the provided argument has an Age
attributetry: isTeenager(5) except Exception as e: print("Error: ", e)
## Error: 'int' object has no attribute 'Age'
class Human: def __init__(self, age, gender, height, weight): self.Age = age self.Gender = gender self.Height = height self.Weight = weight def isTeenager(self): # We always send self to object methods if self.Age < 20: return True else: return False
isTeenager()
method!Ahmet = Human(23, 'Male', 170, 65) Ahmet.isTeenager()
## False
width
, height
area()
, circumference()
width
, height
area()
, circumference()
class Rectangle: def __init__(self, w, h): self.width = w self.height = h def area(self): return self.width * self.height def circumference(self): return 2 * (self.width + self.height)
r1 = Rectangle(3, 4) r1.area()
## 12
r1.circumference()
## 14
r1 = Rectangle(3, 6) r2 = Rectangle(4, 5) r2.area() > r1.area()
## True
r3 = Rectangle(r1.width, r2.height) r3.circumference()
## 16
__str__
is called by Python to print an objectclass Rectangle: def __init__(self, w, h): self.width = w self.height = h def __str__(self): # returns the string to print return ("<" + str(self.width) + "," + str(self.height) + ">") r1 = Rectangle(3, 6) r2 = Rectangle(4, 5) print(r1)
## <3,6>
print(r2)
## <4,5>
__add(self, other)__
: adds self and other__sub(self, other)__
: subtracts other from self__eq(self, other)__
: returns whether self == other__lt(self, other)__
: returns whether self < other__len(self)__
: returns the length of self__len__
for Rectangle
class__eq__
for Rectangle classclass Rectangle: def __init__(self, w, h): self.width = w self.height = h def __eq__(self, other): return self.width == other.width and self.height == other.height r1 = Rectangle(3, 5) r2 = Rectangle(4, 6) r3 = Rectangle(3, 5) r1 == r2
## False
r1 == r3
## True
class Rational: def __init__(self, a, b): self.A = a self.B = b def __str__(self): return str(self.A) + "/" + str(self.B) r1 = Rational(3, 5) print(r1)
## 3/5
simplify()
first:
def simplify(self): # Find the GCD gcd = 1 for i in range(2, min(self.A, self.B) + 1): if self.A % i == 0 and self.B % i == 0: gcd = i # Update self self.A = self.A // gcd # Force integer division self.B = self.B // gcd # Force integer division
r1 = Rational(12, 15) print(r1)
## 12/15
r1.simplify() print(r1)
## 4/5
__init__
to be smarter:def __init__(self, a, b): self.A = a self.B = b self.simplify()
r1 = Rational(12, 15) print(r1)
## 4/5
def __eq__(self, other): self.simplify() # To be on the safe side, simplify both rationals other.simplify() return self.A == other.A and self.B == other.B
r1 = Rational(12, 15) r2 = Rational(8, 10) r3 = Rational(9, 12) r1 == r2
## True
r1 == r3
## False
r2 == r3
## False
def __add__(self, other): # Find the LCM of self and other's denominators big, small = max(self.B, other.B), min(self.B, other.B) i = 1 while not (big * i) % small == 0: i += 1 lcm = big * i return Rational(self.A * (lcm // self.B) + other.A * (lcm // other.B), lcm)
r1 = Rational(1, 5) r2 = Rational(3, 4) r3 = Rational(12, 8) print(r1 + r2)
## 19/20
print(r2 + r3)
## 9/4
print(r1 + r3)
## 17/10
def __mul__(self, other): if type(other) == int: return Rational(self.A * other, self.B) else: return Rational(self.A * other.A, self.B * other.B) def __rmul__(self, other): if type(other) == int: return Rational(self.A * other, self.B) else: return Rational(self.A * other.A, self.B * other.B)
r1 = Rational(2, 9) r2 = Rational(3, 4) print(2 * r1)
## 4/9
print(r1 * 2)
## 4/9
print(r1 * r2)
## 1/6
def __sub__(self, other): return self + (-1 * other)
r1 = Rational(2, 9) r2 = Rational(3, 4) print(r1 - r2)
## -19/36
print(r2 - r1)
## 19/36