首页 > 编程语言 >python 数据类型判断

python 数据类型判断

时间:2023-11-17 15:25:35浏览次数:26  
标签:判断 return exec python sum 数据类型 int isinstance

python 数据类型判断

#encoding=utf-8
l = [1,2,"s",[1,23],{1:2},(1,2),set([1,2]),"b",-2]
d = {"int":0,"str":0,"list":0,"tuple":0,"set":0,"dict":0,"complex":0}    
#先用字典定义列表中的数据类型
for i in l:
if isinstance(i,str):   判断字符串类型
d["str"]+=1
if isinstance(i,int):   判断整型类型
d["int"]+=1
if isinstance(i,list):
d["list"]+=1
if isinstance(i,set):
d["set"]+=1
if isinstance(i,tuple):
d["tuple"]+=1
if isinstance(i,dict):
d["dict"]+=1
if isinstance(i,complex):  判断复数类型
d["complex"]+=1
print(d)
在Python中,定义一个函数要使用def语句,依次写出函数名、括号、括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回。
以自定义一个除法div()函数为例:
def div(a,b):
if not isinstance(a,(int,float)):  # 判断一下a是否是数字
return None
if not isinstance(b,(int,float)): # 判断一下b是否是数字
return None
if b == 0:
return None
return a/b
UnboundLocalError:局部变量a在赋值前被引用
1.如果使用了赋值操作,此变量为局部变量在使用前必须初始化。
2.函数中使用全局变量,申明为global


注意:*args 是元组,**kw 是字典
def sum(*args):                           
result=1
 for i in args:
  result*=int(i)
return result


该函数为 sum(1,2,3,4,b=5,c=6,d=7)
#encoding=utf-8
def sum(a,*arg,**args):
sum=0
sum=sum+a
for i in arg:
sum=sum+int(i)
for i in args.values():
sum=sum+int(i)
return sum
当我们需要动态的创造python代码,然后将其作为语句或作为表达式去执行。exec语句用来执行存储在字符串或文本中有效的python语句
exce语句执行python语句不会返回结果
def a():
print 'hello python'
exec('a()')
执行结果:hello python
def a():
return 'test'
exec('a()')
>>> exec("print ('hello python')")
hello python
eval语句用来执行存储在字符串或文本中有效的python表达式,并返回计算结果
和exec函数区别:
a:eval函数有返回值,而exec函数没有返回值
b:eval函数可以打印,而print exec函数会报语法错误
def a():
print 'hello python'
return 1
exec('a()')
eval('a()')

标签:判断,return,exec,python,sum,数据类型,int,isinstance
From: https://www.cnblogs.com/HeroZhang/p/17838822.html

相关文章

  • python 目录操作
    __author__='Administrator'#_*_coding:UTF-8_*_#@Createbygengyu#@CreateTime:2021/12/4#@File_name:exists#wn.run/https://importos,sys,pathlibimportglobimportshutil'''thisis__doc__'''def......
  • 测试某个python库是否正常导入
    我这里的测试代码:python-c"importmagenta"出现错误:/Users/ghj1976/opt/anaconda3/envs/magenta/lib/python3.7/site-packages/librosa/util/decorators.py:9:NumbaDeprecationWarning:Animportwasrequestedfromamodulethathasmovedlocation.Importrequested......
  • python装饰器
    装饰器概念:可以在不修改原来代码的情况下(函数原有的功能或者类原有的功能),为需要被装饰的函数或者类增加新的功能或者添加限制调剂以及帮助输出常用种类函数的装饰器类的装饰器不管是哪种类型的装饰器设计模式原则是:开放封闭的原则(对外扩展开发,对内关闭修改)1.1装饰器的定......
  • 【Python入门教程】Python中类的用法和意义
    ​        在Python中,类是一种重要的面向对象编程概念。它们为我们提供了一种方法,可以将现实世界中的对象抽象为代码中的类,并通过类创建对象的实例。类定义了对象的结构和行为,使我们可以构建复杂的程序和数据模型。一、类的定义        在Python中,类的定义使......
  • python-tkinter去除命令日志
    Python打包exe文件后,执行exe文件总会打开命令行窗口,通过查找相关解决的方法,经过亲测,介绍几种可行的方案。修改文件名后缀将.py文件改成.pyw文件(使用的是脚本解析程序pythonw.exe)修改打包命令pyinstaller-i添加图标        -w去除命令行解决报错AttributeError......
  • ! (空引用忽略判断) 操作符 (C# reference)
    ref: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving主要是.Net6开始判断引用类型是否空,在项目文件中  PropertyGroup节点下  <Nullable>enable</Nullable,代表开启 ,在这个情况我想某个变量或者属性引用不要......
  • Python 中将 None 转换为 0
    如何在Python中将None转换为0–码微(mwell.tech)some_number=Noneresult=some_numberor0print(result)#......
  • Python模块的搜索路径
    在Python中,模块搜索路径是指解释器用来查找导入模块的位置列表。了解和掌握Python模块搜索路径对于正确导入模块和管理模块的位置至关重要。Python模块搜索路径的主要来源包括当前目录、Python标准库目录和用户自定义的目录。你可以通过sys模块中的sys.path来查看和修改模块搜索......
  • Communication Setup中VCDL与Python交互
     ApplicationMoudles基础代码[email protected]_scriptclassLinkToSigFile:#Calledbeforemeasurementstarttoperformnecessaryinitializations,#e.g.tocreateobjects.Duringmeasurement,fewadditionalobjects......
  • python有用链接
    Python处理日期方法大全、三十种方法 Python中的13个搔操作    ......