wwfl.net
当前位置:首页 >> python 取负数 >>

python 取负数

直接int(..)或float(...)就好啦 [willie@bogon zhidao]$ python3 Python 3.5.2 (default, Sep 30 2016, 01:32:24) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux Type "help", "copyright", "credits" or "license" for more information. ...

-5 // 3

%用来获得除法运算的余数,比如5%2=1 divmod(x,y)这个函数也可以获得商和余数

apple pie^ 下标0 ^ 下标-3从下标-3向右找不到下标0,所以是空字串,如果是food[-3:]就是'pie',因为不指定范围的结束就等于在遇到最后一个字符之后结束 -3是p,0是开始,这是规定的

number = int(input('Please input a number: '))# print( number)if number >= 0: print( number)else: print( -number)

来个简单的,for循环,弄个if,判断大于0和小于0,并做计数, 比如: s = [1,5,-7]counter = 0for i in s: if i > 0: counter += 1print counter

def getInt(prompt, limit=(0, None)): while True: try: x = int(input(prompt)) if limit[0] is not None and x < limit[0]: continue if limit[1] is not None and limit[1] < x: continue return x except: pass def setlimits(): lb = getI...

用int()那个不叫系统崩溃,叫“未处理的意外”,try/except就是干这个的。def func(x): try: x=int(x) return isinstance(x,int) except ValueError: return Falsefunc(1)Truefunc('1.1')Falsefunc('-1')Truefunc('-1.1')Falsefunc('asdf')False

a=int(input()) if a>0: print('正数') elif a<0: print('负数') else: print('0')

import randoma=[]for i in range(10): a.append(random.randrange(-19,19))a.reverse()b = [x for x in a if x < 0]print "a The biggest negative is :"b[0],b[1]a.sort()b = [x for x in a if x > 0] print "a The smallest positive is:",b[...

网站首页 | 网站地图
All rights reserved Powered by www.wwfl.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com