python 初学教程 做比较和逻辑运算,python初学,python 做比较和逻
python 初学教程 做比较和逻辑运算,python初学,python 做比较和逻
python 做比较和逻辑运算
#!/usr/bin/python# 演示python比较和逻辑运算import random# 在python中逻辑运算是可以写成一串的,例如 a >= b > c 相当于c语言中的 a>= b && b > cc = -2for a in range(1,4): c = c + 4 for b in range(1,4): print '(' + str(a), '<', str(b) + ') ==', a < b, ' ', print '(' + str(a), '>=', b, '>', str(c) + ') ==', a >= b > c, ' ', print '(' + str(a), '==', b, '==', str(c) + ') ==', \ a == b == c, ' ', print '(' + str(a), '!=', b, '!=', str(c) + ') ==', a != b != c c = c - 1print# 在python中逻辑运算符是 and or not 不能使用 && || !c = -1for a in range(0,3): c = c + 5 for b in range(0,3): print '(' + str(a), '==', b, 'or', a, '==', c, 'and', b, '<', \ str(c) + ') ==', a == b or a == c and b < c, ' ', print '(not', a, '<', str(b) + ') == ', not a < b, ' ' c = c - 2print# When and or or returns true, it returns the second argument.c = -1for a in [0, 3, 4]: c = c + 2 for b in [-2, 0, 5]: print '(' + str(a), 'and', b, 'or', str(c) + ') == ',\ a and b or c, ' ', print '(' + str(a), 'or', b, 'and', str(c) + ') == ',\ a or b and c c = c - 1print# python 中使用 in表示 元素是否在list中存在some = [2,4,7]for a in range(1,5): if a in some: print a, 'is', else: print a, 'is not', print 'in', some
在逻辑运算方面Python和c,java等不同的地方有:
python支持一串逻辑运算 例如:a>b>c
可以写到一起,这是合法的python的逻辑运算符是 and or not另外python中有boolean类型,boolean类型有两个值:True,False
相关内容
- python编写web service,pythonwebservice,以前都是用C#写webs
- python初学教程 读入控制台输入并打印到控制台,pytho
- python实现子网掩码格式转换,python子网掩码,python实现子
- python判断当前用户是否是root,pythonroot,import osif
- Python初学教程:函数定义,python初学教程函数,Python 方
- python获取mp3文件信息,python获取mp3,需要安装ID3 包。i
- python 网络爬虫,python爬虫,[Python]代码#F
- Python 的多进程 fork,python进程fork,Python 的多进程
- Python初学教程:为函数参数指定默认值,python默认值
- Python初学教程:变量的作用域,python初学变量,如下代码
评论关闭