How to use scores in Python

Source: Internet
Author: User
This article describes how to use scores in Python. it mainly involves simple operations such as score calculation and scoring. it is the basis of the Python learning process, you may not need to process scores frequently, but the Python Fraction class will help you a lot when you need it. In this guide, I will provide some interesting examples to demonstrate how to handle scores and highlight some cool features.
1 Basic

The Fraction class is in the Lib/fractions. py file, so you can import it as follows:

from fractions import Fraction

There are many ways to instantiate the Fraction class.

First, you can pass in the numerator and denominator:

>>> Fraction(1, 2)Fraction(1, 2)

Or use another score for instantiation:

>>> f = Fraction(1, 2)>>> Fraction(f)Fraction(1, 2)

Use a floating point number for instantiation:

>>> Fraction(2.5)Fraction(5, 2)

Or use a decimal:

>>> from decimal import Decimal>>> Fraction(Decimal('1.1'))Fraction(11, 10)

The last method may be the most interesting one. you can use a string to instantiate the Fraction class:

>>> Fraction('9/16')Fraction(9, 16)

In essence, the design of the Fraction class aims to prevent you from doing much processing before instantiating the class. The Fraction class knows how to process different data types.
2 automatic scoring

It is not very difficult to score, but for some complicated scores, it is still a matter of time. The Fraction class is particularly useful in this regard because it can automatically score points.

>>> Fraction(153, 272)Fraction(9, 16)

You may not be able to score about 153/172 points, but the Fraction class can quickly score points.
3 Binary operations

You can perform binary operations on the Fraction object like integers and floating-point numbers.

Add two scores:

>>> Fraction(1, 2) + Fraction(3, 4)Fraction(5, 4)

This operation is very convenient, but you can also mix integers or floating point numbers. As expected, the Fraction object and an integer are added to return an Fraction object, but a floating point number is added to return a floating point number.

>>> Fraction(5, 16) + 3Fraction(53, 16)>>> Fraction(5, 16) + 3.03.3125

Here are some examples of other binary operations:

>>> Fraction(5, 16) - Fraction(1, 4)Fraction(1, 16)>>> Fraction(1, 16) * Fraction(3, 16)Fraction(3, 256)>>> Fraction(3, 16) / Fraction(1, 8)Fraction(3, 2)

Now let's try the multiplication operation:

>>> Fraction(1, 8) ** Fraction(1, 2)0.3535533905932738

It returns a floating point number, probably because the score cannot be reasonably calculated. In fact, we can use the limit_denominator method to obtain an approximate Fraction value.

>>> f = Fraction(1, 8) ** Fraction(1, 2)>>> Fraction(f).limit_denominator()Fraction(235416, 665857)

Remember, you can mix strings with the data types mentioned in the previous instantiation section.

>>> Fraction("1/2") + Fraction(2.0)Fraction(5, 2)>>> Fraction(2) * Fraction("  1/2  ")Fraction(1, 1)

4. get the attributes of the Fraction object

You already have a Fraction object and have made some calculations. how can we access its attributes?

If you do not read the document, you may try Fraction. numerator and Fraction. denominator. it turns out that you are correct.

>>> f = Fraction(221, 234) + Fraction(1, 2)>>> f.numerator13>>> f.denominator9

Or print the entire score as a string:

>>> print f13/9>>> a = str(f)>>> a'13/9'
5 GCD

This is not part of the Fraction class. it is in the fractions Library. You can use it to quickly find the maximum common divisor of two numbers.

First import:

from fractions import gcd

Some examples:

>>> gcd(100, 75)25>>> gcd(221, 234)13


6. Conclusion

I hope you have learned something about processing scores in Python. For more information, see the document. If you feel very motivated, you can look at the source code.

If you have more interesting scores, let me know and I will add them to the guide.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.