Python 中的简单算术计算

1. 除法

a, b, c, d, e =3, 2, 2.0, -3, 10

print(a / b)

print(a / c)

输出都是: 1.5


print(d / b)

print(b / a)

print(d / e)

输出: 

-1.5

0.6666666666666666

-0.3


整除 //

print(a//b) 输出:1

print(a//c) 输出:1.0

如果有一个浮点型数据参与整除,结果是浮点型


2. 加法

a, b =1, 2

print(a+b)

输出:3 


a+=b

print(a)

输出:3


import operator

a, b =1, 2

print(operator.add(a, b))

a = operator.iadd(a,b)

print(a)

print("first string " +"second string")

输出:

3

3

first string second string


3. 幂运算

import math

a, b =2, 3

print(a ** b)

print(math.pow(2,3))


输出:

8

8.0


x = 8

math.pow(x, 1/3) 

x**(1/3)

输出:

2.0

2.0


4. 开方

import math

import cmath

c =4

print(math.sqrt(c))

print(cmath.sqrt(c))

输出:

2.0

(2+0j)


5. 减法

a, b =1, 2

print(b-a)

输出:1


import operator

print(operator.sub(b, a))

输出:1


6. 乘法

a, b =2, 3

print(a*b)

输出:6


print(operator.mul(a, b) )

输出:6

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Python中默认的编码格式是 ASCII 格式,在没修改编码格式时无法正确打印汉字,所以在读取中文时会报错。 解...
    蔷北阅读 3,130评论 0 0
  • 1. 摘要 本文讲解了Python语法的要点,便于入门者学习之用。 2.内容 2.1 基本语法 2.2.1 Pyt...
    笔名辉哥阅读 5,262评论 0 1
  • Python-菜鸟驿站 tags: Python 菜鸟驿站 2018年 12月 简介说明 以下全文来自于菜鸟驿站官...
    SuperScfan阅读 3,641评论 0 0
  • 前言 Python的创始人为Guido van Rossum。1989年圣诞节期间,在阿姆斯特丹,Guido为了打...
    依依玖玥阅读 8,927评论 6 37
  • 做数据挖掘、机器学习需要用到Python,写下一些基本备忘,特别是相对C++不同的地方 信息源 RUNOOB.CO...
    林空谷阅读 1,722评论 0 0