Skip to content

Nayan-2006-opensource/Fraction-data-type

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Fraction Class using OOP in Python

A Python project that demonstrates Object-Oriented Programming (OOP) by creating a custom Fraction class. The project uses operator overloading to perform arithmetic operations on fractions just like built-in numeric types.

Features

  • Create fraction objects
  • Addition of fractions
  • Subtraction of fractions
  • Multiplication of fractions
  • Division of fractions
  • Operator overloading using Python magic methods
  • Clean and readable OOP implementation

Concepts Used

  • Python Classes & Objects
  • Constructors (__init__)
  • Magic Methods
    • __str__
    • __add__
    • __sub__
    • __mul__
    • __truediv__
  • Object-Oriented Programming (OOP)

Project Structure

fraction.py
README.md

Example

a = Fraction(5, 4)
b = Fraction(4, 3)

print(a + b)
print(a - b)
print(a * b)
print(a / b)

Output

5/4 + 4/3 = 31/12
5/4 - 4/3 = -1/12
5/4 * 4/3 = 20/12
5/4 / 4/3 = 15/16

Learning Outcomes

This project helped me understand:

  • How classes and objects work in Python
  • Operator overloading using magic methods
  • Creating custom data types
  • Implementing arithmetic operations with OOP

Future Improvements

  • Simplify fractions automatically
  • Validate denominator (prevent zero division)
  • Compare fractions using comparison operators
  • Convert fractions to decimal values

Author

Nayan Samadhiya

Aspiring AI/ML Engineer | Learning Python & Object-Oriented Programming

About

A Python OOP project implementing a custom Fraction class with operator overloading.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages