What does a python score represent?

爱喝马黛茶的安东尼
Release: 2019-06-26 14:09:12
Original
5478 people have browsed it

The Fraction function is a module (module) in Python that implements fractions. A module is a code program written by others and can be used directly, including the definition of classes, functions and labels. It is a part of the Python standard function library. part. The module must be inserted first to use it.

What does a python score represent?

>>> from fractions import Fraction     #插入模块
>>> f =Fraction(1,2)    #创建Fraction 类,并初始化为1/2
>>> f #输出分数类对象Fraction(1, 2)
Copy after login

When entering fractions, the first letter of Fraction must be capitalized, otherwise an error will be reported. We operate on fractions.

>>> Fraction(2,8)+1+1.5
2.75
Copy after login

If the float data type appears during the operation, the final result will be float type data. If there are only integer types and fraction types in the operation expression, the output result will be of type fraction.

>>> Fraction(2,8)+Fraction(2,5)+3
Fraction(73, 20)
Copy after login

Fraction() method is very interesting, it can directly receive the fraction string and turn it into input. Note that the entered fraction must be enclosed in quotation marks.

>>> a = Fraction('1/5')
>>> a
Fraction(1, 5)
>>>
Copy after login

Input decimals directly, and the Fraction method directly converts decimals into fractions.

>>> from decimal import Decimal
>>> Fraction(1.1)
Fraction(11, 10)
Copy after login

To convert a string into a decimal, you need to use the decimal class, and then convert the decimal into a fraction.

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

First convert the 1.1 string into a decimal, and then into a fraction.

Related recommendations: "Python Video Tutorial"

Features

1. Automatic reduction

When there is a negative sign in the numerator and denominator, the division is automatically reduced and the negative sign is finally assigned to the numerator

>>>Fraction(10, -5)
Fraction(-2, 1)
Copy after login

2. Binary operations:

Add two fractions to get a fraction

Add a fraction to an integer to get a fraction

Add a fraction to a floating point number to get a floating point number

Other binary operations are the same as addition

3. Get the Fraction object properties

Literally, numerator gets the numerator and denominator gets the denominator

4.gcd quickly gets the greatest common divisor

>>> from fractions import gcd
>>> gcd(9, 6)
3
Copy after login

Example

What does a python score represent?

>>> s =1
>>> for i in range(1,10):   
      s=s+Fraction(1,i)
>>> print(s)
9649/2520
Copy after login

The above is the detailed content of What does a python score represent?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!