发布于 2015-11-17 01:25:38 | 623 次阅读 | 评论: 0 | 来源: 网友投递
这里有新鲜出炉的Python3 官方中文指南,程序狗速度看过来!
Python编程语言
Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。
下面说的和三目运算有点相似,但又不一样,实在不知道该如何拟定标题,先就是这个标题吧,大家都知道python中没有三目运算,但是and/or有点类似三目运算:
and/or
单独使用表示逻辑关系与和或,也可以组和使用,用法如下
and
and前后如果某一个值为假(False, '', [], {}, None…)则返回第一个假值 如果所有值都为真则返回最后一个真值
or
如果or任意一个值为真,则立刻返回这个值 如果所有值都为假,则or返回最后一个假值
例子
result = 'test' and True # result = True result = 'test' and 'ortest' # result = ortest result = False and 'ortest' # result = False result = '' and None # result = '' result = '' or "Hall" # result = Hall result = False or None # result = None result = 'test' or 'nottest' # result = test
使用单行if else 模拟三目运算
result if True / False else fresult if为真时候结果为result,为假的时候结果为fresult
result = 'test' if True else 'not test' # result = 'test' result = 'test' if False else 'not test' # result = 'not test'