DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Python in 2026: uv vs Poetry vs pip: The Definitive Comparison
  • Real-Time Face Recognition Using OpenCV, Dlib, and Python
  • From Polling to PubSub: Building an Asynchronous OPC UA Stack in Python
  • Introducing RAI Audit Kit: Evidence-Grade Responsible AI Audits in Python

Trending

  • Method injection with Spring
  • How to Format Articles for DZone
  • Observability for Agents and Workflows: Tracing Prompts, Tool Calls, and Business Outcomes End-to-End
  • HTTP QUERY in Java: The Missing Method for Complex REST API Searches
  1. DZone
  2. Coding
  3. Languages
  4. Reduce Fractions Function Python

Reduce Fractions Function Python

By 
Snippets Manager user avatar
Snippets Manager
·
Jul. 19, 10 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
16.5K Views

Join the DZone community and get the full member experience.

Join For Free
A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.


def reducefract(n, d):
    '''Reduces fractions. n is the numerator and d the denominator.'''
    def gcd(n, d):
        while d != 0:
            t = d
            d = n%d
            n = t
        return n
    assert d!=0, "integer division by zero"
    assert isinstance(d, int), "must be int"
    assert isinstance(n, int), "must be int"
    greatest=gcd(n,d)
    n/=greatest
    d/=greatest
    return n, d
Python (language)

Opinions expressed by DZone contributors are their own.

Related

  • Python in 2026: uv vs Poetry vs pip: The Definitive Comparison
  • Real-Time Face Recognition Using OpenCV, Dlib, and Python
  • From Polling to PubSub: Building an Asynchronous OPC UA Stack in Python
  • Introducing RAI Audit Kit: Evidence-Grade Responsible AI Audits in Python

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook