Python赋值语句后逗号的作用分析

yipeiwu_com5年前Python基础

本文实例讲述了Python赋值语句后逗号的作用。分享给大家供大家参考。具体分析如下:

IDLE 2.6.2

>>> a = 1
>>> b = 2,
>>> print type(a)
<type 'int'>
>>> print type(b)
<type 'tuple'>
>>> c = []
>>> d = [],
>>> print type(c)
<type 'list'>
>>> print type(d)
<type 'tuple'>

赋值表达式的后面加了逗号后,会自动得到一个tuple的对象,在作一些与类型相关的工作或需要序列化时,是不能得到期望的结果的。工作中碰到类似灵异现象时,可以把这个放到自己的checklist中了。

>>> print c
[]
>>> print d
([],)
>>> print a
1
>>> print b
(2,)

希望本文所述对大家的Python程序设计有所帮助。

相关文章

解决Python pandas plot输出图形中显示中文乱码问题

解决方式一: import matplotlib #1. 获取matplotlibrc文件所在路径 matplotlib.matplotlib_fname() #Out[3]: u'...

python决策树之CART分类回归树详解

python决策树之CART分类回归树详解

决策树之CART(分类回归树)详解,具体内容如下 1、CART分类回归树简介   CART分类回归树是一种典型的二叉决策树,可以处理连续型变量和离散型变量。如...

Python字典循环添加一键多值的用法实例

循环写入字典key、value、删除指定的键值对: 原文本‘jp_url.txt'每行元素以逗号分隔: host_key,product_id,product_name,cont_s...

python中pygame模块用法实例

python中pygame模块用法实例

本文实例讲述了python中pygame模块用法,分享给大家供大家参考。具体方法如下: import pygame, sys from pygame.locals import *...

Python读取MRI并显示为灰度图像实例代码

Python读取MRI并显示为灰度图像实例代码

本文实例主要关于Python实现读取MRI(核磁共振成像)为numpy数组,使用imshow显示为灰度。 代码如下: import matplotlib.pyplot as plt...