详谈python中冒号与逗号的区别

yipeiwu_com5年前Python基础

注意if\while\for等(或函数定义)语句在结尾处包含一个冒号——我们通过它告诉python下面跟着一个语句块。

--------------冒号的用法

if guess == number:
print 'Congratulations, you guessed it.' # New block starts here
print "(but you do not win any prizes!)" # New block ends here
elif guess < color="#ff0000">:
print 'No, it is a little higher than that' # Another block
# You can do whatever you want in a block ...
else:
print 'No, it is a little lower than that' 
--------------
def printMax(x, y): 
'''输出最大的2个数.
2个数值必需是整数.'''
x = int(x) # convert to integers,
if possible
y = int(y)
if x > y: 
print x, 'is maximum'
else:
print y, 'is maximum'
========逗号的特殊用途
for item in shoplist:
print item,

我们在print语句的结尾使用了一个 逗号 来消除每个print语句自动打印的换行符。这样做有点难看,不过确实简单有效。

以上这篇详谈python中冒号与逗号的区别就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

基于python requests库中的代理实例讲解

直接上代码: #request代理(proxy) """ 1.启动代理服务器Heroku,相当于aliyun 2.在主机1080端口启动Socks 服务 3.将请求转发到1080端口...

python将unicode转为str的方法

问题:  将u'\u810f\u4e71'转换为'\u810f\u4e71'   方法:  s_unicode = u'\u810f\u4e...

windows环境中利用celery实现简单任务队列过程解析

windows环境中利用celery实现简单任务队列过程解析

这篇文章主要介绍了windows环境中利用celery实现简单任务队列过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一、背景...

基于python 字符编码的理解

一、字符编码简史: 美国:1963年 ASCII (包含127个字符  占1个字节) 中国:1980年 GB2312 (收录7445个汉字,包括6763个汉字和682个其它符号...

Tensorflow中使用tfrecord方式读取数据的方法

Tensorflow中使用tfrecord方式读取数据的方法

前言 本博客默认读者对神经网络与Tensorflow有一定了解,对其中的一些术语不再做具体解释。并且本博客主要以图片数据为例进行介绍,如有错误,敬请斧正。 使用Tensorflow训练神...