python运算函数

python

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>> 

输入输出函数

input() :用户用于读取键盘输入的函数,返回值为“string”类型

不管输入什么类型的字符,返回都是字符型的

>>> a=input("请输入:")
请输入:123
>>> print(type(a))
<class 'str'>
>>> type(a)
<class 'str'>
>>> b=input()
ads
>>> print(b)
ads
>>> print(type(b))
<class 'str'>
>>> c=input()
12.3
>>> print(type(c))
<class 'str'>
>>> print(c)
12.3

一次可以有多个输入,调用split()函数,调整输入的间隙控制符

a,b,c =input(":").split(',')#用‘,’隔开
d,e,f =input(':').split(' ')#用‘ ’隔开
print(a,b,c)
print(d,e,f)

输出结果:
在这里插入图片描述

split()

split(chars,int)函数有两个参数,第一个参数为指定的分隔符,第二个参数为指定的分割次数

运算符

在这里插入图片描述

//

//:确保得到的结果是一个整数,向下取整
取比目标结果小的最大整数
对比 / 和 // 的区别

#当结果为正数时
>>> 3/2
1.5
>>> 3//2
1
#当结果为负数时
>>> -3/2
-1.5
>>> -3//2
-2
>>> 3/-2
-1.5 
>>> 3//-2
-2

x=(x//y)*y+x%y

>>> x=15
>>> y=4
>>> (x//y)*y+x%y
15

运算函数

abs(x) :x的绝对值

在这里插入图片描述

int(x) :将x转换成整型(截掉小数部分)

在这里插入图片描述

float(x):浮点数

在这里插入图片描述

divmod(x,y):返回(x//y,x%y)

x//y:向下取整
在这里插入图片描述

complex(re,im):返回一个复数,re是实部,im是虚部

在这里插入图片描述

pow(x,y):计算x的y次方

x**y:计算x的y次方
如果加入第三个参数,则结果对第三个参数取余
即:222%5=3
在这里插入图片描述

bool(x):输出True或False

在这里插入图片描述
在这里插入图片描述

range(stop):依次生成一个(0~stop)的数

range(stop) — 依次生成一个(0~stop)的数

例如:
range(5)
打印输出:
0
1
2
3
4

range(start,stop) — 依次生成一个(start~stop)的数

例如:
range(5,10)
打印输出:
5
6
7
8
9

range(start,stop,step) — 依次生成一个(start~stop)的数,跨度为step

例如:
range(3,10,2)
打印输出:
3
5
7
9

随机数

头文件 random
函数原型:

random.randint(下限,上限)

返回值:int型

import  random
counts=0
while counts<5:
    print("随机数为",random.randint(1,10))
    counts+=1

在这里插入图片描述

random.getstate()

import  random

x=random.getstate()
print(x)
counts=0
while counts<5:
        print("随机数为",random.randint(1,10))
        counts+=1
random.setstate(x)
counts=0
while counts<5:
        print("随机数为",random.randint(1,10))
        counts+=1

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值