Home >Backend Development >Python Tutorial >How to import math library in python

How to import math library in python

尚
Original
2019-07-03 13:21:2223013browse

How to import math library in python

The math library contains mathematical formulas. We can use the math library to find the value of an expression.
First import the math library (two methods):

import math
from math import x #x表示math库中方法

Example:

import math

def main():
    money_everyweek = 10    # 每周存入的金额
    i = 1                   # 第几周
    increasing_money = 10   # 每周递增的金额
    end_week = 52           # 总共存52周
    saving_money = 0        # 目前账户有多少金额

    money_list = []
    while i <= end_week :
        saving_money = math.fsum(money_list)         #现在账户有多少元
        money_list.append(money_everyweek)
        print(&#39;第{}周,存入{}元,目前账户有{}元&#39;.format(i, money_everyweek, saving_money))
        money_everyweek += increasing_money     #每周存入的金额数为上一周的加上递增金额
        i += 1                                  # 递增的周数

if __name__ ==&#39;__main__&#39;:
    main()

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to import math library in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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