GithubHelp home page GithubHelp logo

py-distance-class's Introduction

Distance class

Every day you have morning running. You want to store your result. For easier calculating, comparison and printing your result write class Distance.

Distance's constructor takes only one argument km and saves it to self.km.

For Distance class you should implement such magic methods:

__init__
distance = Distance(20)  # distance.km == 20

__str__
distance = Distance(20)
print(distance)  # "Distance: 20 kilometers."

__repr__
distance = Distance(20)
repr(distance)  # "Distance(km=20)"

__add__
distance1 = Distance(20)
distance2 = Distance(30)
distance3 = distance1 + distance2  

# isinstance(distance3, Distance) is True
# distance3.km == 50

distance1 = Distance(20)
distance2 = distance1 + 10

# isinstance(distance2, Distance) is True
# distance2.km == 30
# both variants ^ are possible

__iadd__
distance1 = Distance(20)
distance2 = Distance(30)
distance1 += distance2  # distance1.km is 50

distance = Distance(20)
distance += 30  # distance.km == 50

__mul__
distance1 = Distance(20)
distance2 = distance1 * 5  

# isinstance(distance2, Distance) is True
# distance2.km == 100

__truediv__
distance1 = Distance(20)
distance2 = distance1 / 7  

# isinstance(distance2, Distance) is True
# distance2.km == 2.85
# Note: rounded to 2 decimals

__lt__, __gt__, __eq__, __le__, __ge__
distance = Distance(50)
distance < Distance(60)  # True  # distance.km < 60 == True
distance > Distance(120)  # False
distance == Distance(100)  # False
distance <= Distance(49)  # False
distance >= Distance(50)  # True

distance < 60  # True  # distance.km < 60 == True
distance > 120  # False
distance == 100  # False
distance <= 49  # False
distance >= 50  # True

py-distance-class's People

Contributors

masterpieceelbow avatar nattalli avatar abnormaltype avatar tpolina avatar danylott avatar ivanramyk avatar dmytrosvirsa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.