Skip to Content
Python金融实战
book

Python金融实战

by Posts & Telecom Press, Yuxing Yan
February 2024
Intermediate to advanced content levelIntermediate to advanced
347 pages
5h 19m
Chinese
Packt Publishing
Content preview from Python金融实战

第2章 用Python完成普通计算器的功能

本章将介绍一些基本概念和几个常用的Python内置函数,如赋值、数值精度、加、减、乘、除、幂函数和平方根函数。将学习如何使用Python作为一个普通的计算器来解决许多与金融相关的问题。本章主要内容如下:

  • 变量的赋值
  • 显示一个变量的值
  • 错误提示
  • 不能调用一个没有被赋值的变量
  • 选择有意义的变量名
  • 使用dir()来查找变量和函数
  • 删除或取消变量
  • 基本的数学运算:加、减、乘、除
  • 幂函数、取整和余数函数
  • 选择合适的精度
  • 找出指定的内置函数的详细信息
  • 如何导入数学模块
  • pi、e、对数和指数函数
  • 区分import mathfrom math import*
  • 几个常用函数:print()type()upper()、下划线_,以及如何结合两个字符串
  • 元组数据类型简介

赋值给一个Python变量非常简单。我们不需要定义一个变量就可以给它赋值。这一点与其他语言,如C++或FORTRAN不同。

>>>pv=22
>>>pv+2 
24

可以同时给几个变量赋予同样的值。在下面的例子中,一次给3个变量xyz赋值100

>>>x=y=z=100

为显示一个变量的值,只需输入其名称。例如:

>>>pv=100
>>>pv 
100
>>>R=0.1
>>>R 
0.1

如果使用sqrt()函数来估计\sqrt{3} ,将得到以下出错信息。

>>>sqrt(3) Traceback (most recent call last): File ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

实时数据处理和分析指南

实时数据处理和分析指南

Posts & Telecom Press, Shilpi Saxena, Saurabh Gupta
数据科学实战手册(R+Python)(第2版)

数据科学实战手册(R+Python)(第2版)

Posts & Telecom Press, Prabhanjan Narayanachar Tattar, Tony Ojeda, Sean Patrick Murphy, Sean P Murphy, Benjamin Bengfort, Bengfort Benjamin, ABHIJIT DASGUPTA
Python应用开发指南

Python应用开发指南

Posts & Telecom Press, Ninad Sathaye
编写整洁的Python代码(第2版)

编写整洁的Python代码(第2版)

Posts & Telecom Press, Mariano Anaya

Publisher Resources

ISBN: 9781803230375