Python 条件判断的缩写方法

yipeiwu_com6年前Python基础
return (1==1) ? "is easy" : "my god" //C#中的用法

其实,在Python中,是这样写的:

print (1==2) and 'Fool' or 'Not bad'


输出结果:
Not bad
                       

相关文章

Python字符串格式化输出方法分析

本文实例分析了Python字符串格式化输出方法。分享给大家供大家参考,具体如下: 我们格式化构建字符串可以有3种方法: 1 元组占位符 m = 'python' astr = 'i...

python将print输出的信息保留到日志文件中

具体代码如下所示: import sys import os import sys import io import datetime def create_detail_day()...

Python最长公共子串算法实例

本文实例讲述了Python最长公共子串算法。分享给大家供大家参考。具体如下: #!/usr/bin/env python # find an LCS (Longest Common...

Python实现批量转换文件编码的方法

本文实例讲述了Python实现批量转换文件编码的方法。分享给大家供大家参考。具体如下: 这里将某个目录下的所有文件从一种编码转换为另一种编码,然后保存 import os impor...

Python简单I/O操作示例

本文实例讲述了Python简单I/O操作。分享给大家供大家参考,具体如下: 文件: poem = ''' hello world ''' f = file('book.txt', '...