<jupyter_start><jupyter_text>Exercices<jupyter_code># exercice
class Animal:
    def __init__(self, arms, legs):
        self.arms = arms
        self.legs = legs
        
    def limbs(self):
        return self.arms + self.legs
    
spider = Animal(4, 4)
spidlimbs = spider.limbs()
print(spidlimbs)
cityNames = ['Lyon', 'Paris', 'Marseille']
populations = [680_250, 117_070, 1_683]
states = ['Rhone', 'IDF', 'BDR']

city_tuples = zip(cityNames, populations, states)
list(city_tuples)
class City:
    def __init__(self, name, population, state):
        self.name = name
        self.population = population
        self.state = state
    
    def __str__(self):
        return f'{self.name}, {self.state}, (pop : {self.population})'
    
city_tuples = zip(cityNames, populations, states)

cities = []
for city_tup in city_tuples:
    name, pop, state = city_tup
    city = City(name, pop, state)
    cities.append(City)

cities
#other way
city_tuples = zip(cityNames, populations, states)
cities = [City(n, p, s) for n, p, s in city_tuples]
#another cool way
city_tuples = zip(cityNames, populations, states)
cities = [City(*t) for t in city_tuples] #t for tuple
cities<jupyter_output><empty_output><jupyter_text>Point distance<jupyter_code>import math

class Point:
    """ Point class for representing and manipulating x,y coordinates. """

    def __init__(self, initX, initY):

        self.x = initX
        self.y = initY

    def getX(self):
        return self.x

    def getY(self):
        return self.y

    def distanceFromOrigin(self):
        return ((self.x ** 2) + (self.y ** 2)) ** 0.5

    def distance(self, point2):
        xdiff = point2.getX()-self.getX()
        ydiff = point2.getY()-self.getY()

        dist = math.sqrt(xdiff**2 + ydiff**2)
        return dist
p = Point(4,3)
q = Point(0,0)
print(p.distance(q))<jupyter_output>5.0<jupyter_text>Converting object to stringmethode `__str__`<jupyter_code>class Point:
    """ Point class for representing and manipulating x,y coordinates. """

    def __init__(self, initX, initY):

        self.x = initX
        self.y = initY

    def getX(self):
        return self.x

    def getY(self):
        return self.y

    def distanceFromOrigin(self):
        return ((self.x ** 2) + (self.y ** 2)) ** 0.5

    def distance(self, point2):
        xdiff = point2.getX()-self.getX()
        ydiff = point2.getY()-self.getY()

        dist = math.sqrt(xdiff**2 + ydiff**2)
        return dist
    
    def __str__(self):
        return f"Point ({self.x}, {self.y})"
p = Point(4,3)
print(p)<jupyter_output>Point (4, 3)<jupyter_text>ExerciceCheck Your Understanding    Create a class called Cereal that accepts three inputs: 2 strings and 1 integer, and assigns them to 3 instance variables in the constructor: name, brand, and fiber. When an instance of Cereal is printed, the user should see the following: “[name] cereal is produced by [brand] and has [fiber integer] grams of fiber in every serving!” To the variable name c1, assign an instance of Cereal whose name is "<NAME>", brand is "Kellogg's", and fiber is 2. To the variable name c2, assign an instance of Cereal whose name is "<NAME>", brand is "General Mills", and fiber is 3. Practice printing both!<jupyter_code>class Cereal:
    
    def __init__(self, name: str, brand: str, fiber: int):
        self.name = name
        self.brand = brand
        self.fiber = fiber
        
    def __str__(self):
        return f"{self.name} cereal is produced by {self.brand} \
and has {self.fiber} grams of fiber in every serving!"
c1 = Cereal("Corn Flakes", "Kellogg's", 2)
c2 = Cereal("Honey Nut Cheerios", "General Mills", 3)
print(c1)
print(c2)<jupyter_output>Corn Flakes cereal is produced by Kellogg's and has 2 grams of fiber in every serving!
Honey Nut Cheerios cereal is produced by General Mills and has 3 grams of fiber in every serving!<jupyter_text>Special (dunderscore) Methods<jupyter_code>class Point:
    """ Point class for representing and manipulating 
    x,y coordinates. """
    
    def __init__(self, initX, initY):
        self.x = initX
        self.y = initY
        
    def __str__(self):
        return f"Point ({self.x}, {self.y})"
    
    def __add__(self, otherPoint):
        return Point(self.x + otherPoint.x,
                     self.y + otherPoint.y)
    
    def __sub__(self, otherPoint):
        return Point(self.x - otherPoint.x,
                     self.y - otherPoint.y)
a = Point(-5, 10)
b = Point(15, 20)

print(a + b)
print(a - b)<jupyter_output>Point (10, 30)
Point (-20, -10)<jupyter_text>Instances as Return Valuesreturn a new Point that is halway between two points<jupyter_code>class Point:
    """ Point class for representing and manipulating
    x,y coordinates. """

    def __init__(self, initX, initY):

        self.x = initX
        self.y = initY

    def getX(self):
        return self.x

    def getY(self):
        return self.y

    def distanceFromOrigin(self):
        return ((self.x ** 2) + (self.y ** 2)) ** 0.5

    def distance(self, point2):
        xdiff = point2.getX()-self.getX()
        ydiff = point2.getY()-self.getY()

        dist = math.sqrt(xdiff**2 + ydiff**2)
        return dist
    
    # return a new Point that is halway between two points
    def halfway(self, otherPoint):
        midX = (self.x + otherPoint.x) / 2
        midY = (self.y + otherPoint.y) / 2
        return Point(midX, midY)
    
    def __str__(self):
        return f"Point ({self.x}, {self.y})"
a = Point(-5, 10)
b = Point(15, 20)

mid = a.halfway(b)
print(mid)
print(mid.distanceFromOrigin())<jupyter_output>Point (5.0, 15.0)
15.811388300841896<jupyter_text>Sorting Lists of Instances<jupyter_code>class Fruit():
    def __init__(self, name, price):
        self.name = name
        self.price = price
        
    def sort_priority(self):
        return self.price
    
        
L = [
    Fruit("Pomme", 10),
    Fruit("Poire", 5),
    Fruit("Orange", 12)    
]

for f in sorted(L, key=Fruit.sort_priority):
    print(f.name)
    
print('\n\
methode 2 :')

for f in sorted(L, key=lambda x: x.sort_priority()):
    print(f.name)
    
print('\n\
methode 3 :')

for f in sorted(L, key=lambda x: x.price):
    print(f.name)<jupyter_output>Poire
Pomme
Orange

methode 2 :
Poire
Pomme
Orange

methode 3 :
Poire
Pomme
Orange<jupyter_text>Class Variables and Instance Variables<jupyter_code>class Point:
    """ Point class for representing and manipulating
    x,y coordinates. """
    
    printed_rep = '*' # Class variable

    def __init__(self, initX, initY):

        self.x = initX
        self.y = initY<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12130356065178033, 'fraction_numerical': 0.018859384429692216, 'mean_word_length': 2.897119341563786, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '377959', 'license': 'Apache-2.0', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2243, 'n_words': 653, 'path': '01_classesDefine_dunderscore.ipynb', 'repo_name': 'ThomasFavrel/Python-Classes-and-Inheritance', 'size': '6645'}