python判断是否为空字典_python判断元组、列表、字典是否为空

python判断元组、列表、字典是否为空的代码,在python中()、[]、{}、0、''、False都会是假,所以出现这些情况时,就好判断是不是为空了。#!/usr/bin/python

# -*- coding: utf-8 -*-

p1 = () #空元组

p2 = [] #空列表

p3 = {} #空字典

p4 = 0  #变量0

p5 = '' #变量空字符串

p6 = False #变量假

if p1 :

print 'p1非空'

else:

print 'p1空'

#print "p1的类型"

#print type(p1)

if p2 :

print 'p2非空'

else:

print 'p2空'

#print "p2的类型"

#print type(p2)

if p3 :

print 'p3非空'

else:

print 'p3空'

#print "p3的类型"

#print type(p3)

if p4 :

print '0为真'

else:

print '0为假'

#print "p4的类型"

#print type(p4)

if p5 :

print '空字符串为真'

else:

print '空字符串为假'

#print "p5的类型"

#print type(p5)

if p6 :

print 'False真'

else:

print 'False为假'

#print "p6的类型"

#print type(p6)

运行结果:

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值