Python_查漏补缺

python 数据类型

 =有哪些数据类型
   list tuple set dict
   Iteration  Looping Function Object Class
   Modules Packages 
 判断数据类型
  type   isinstance  issubclass()
   d = (1,"2",3.0)
   print(isinstance(d,int))
   print(isinstance(d[0],int))
   print(isinstance(d,tuple))
   print(isinstance(d,(int,float,tuple)))
   print(type(d)) #type函数输出数据类型
   print(isinstance(d,type(d))) #此输出恒为true
 
 数据类型转换
 
 类的情况
    dir  返回一个列表,列出了一个对象所拥有的属性和方法
    id()函数返回任意不同种类对象的唯一ID
    len()
         object()	 

函数

01.参数
 *args 和**kwargs
 形参  位置参数  默认参数  关键字参数--键值对参数
     函数定义时 *代表聚合。
	 *  将所有的位置参数聚合成一个元组,赋值给了args
	 ** 将所有的关键字参数聚合到一个字典中,将这个字典赋值给了kwargs
 实参--调用  test_args_kwargs(*args)  test_args_kwargs(**kwargs)
 参数顺序:位置参数,*args,默认参数,仅限关键字参数,**kwargs 
     函数的调用时*代表打散 
	  func(*[1,2,3],*[22,33]) #得出(1,2,3,22,33)
02.实参-
   可变(mutable)与不可变(immutable)的数据类型
03.返回值
  函数里返回两个变量
    返回一个包含多个值的tuple(元组),list(列表)或者dict(字典),

python进阶

01.迭代(Iteration)
  可迭代对象(Iterable)  定义了可以返回一个迭代器的__iter__方法,或者定义了可以支持下标索引的__getitem__方法
  迭代器(Iterator)      __next__方法
   生成器也是一种迭代器
  Python内置函数:
     iter()   一个可迭代对象返回一个迭代器对象
	 next()
	 
02.三元运算符 三元运算符通常在Python里被称为条件表达式
 device = "cuda" if torch.cuda.is_available() else "cpu"
 
03.Decorators  装饰器


04.comprehensions


img_file_name = []
[img_file_name.append(os.path.join(src_leaf_dir, file_name))
 for file_name in os.listdir(src_leaf_dir)
 if file_name.lower().endswith("jpeg")
 or file_name.lower().endswith("png")
 or file_name.lower().endswith("jpg")
 ]

img_file_name =
[ os.path.join(src_leaf_dir, file_name) 
 for file_name in os.listdir(src_leaf_dir)
 if file_name.lower().endswith("jpeg")
 or file_name.lower().endswith("png")
 or file_name.lower().endswith("jpg")
 ]
 
 {v: k for k, v in some_dict.items()}
 
05.第三方包
   collections   defaultdict
   
06. lambda  filter  map  reduce

07.上下文管理器  定义__enter__和__exit__方法。

类和对象

 Python的内置函数
  getattr() setattr() hasattr()m delattr()   
  isinstance() issubclass() type()  super()
   callable() hash() object() len() id()
 classmethod  staticmethod property

参考

Python 3.10.2 documentation https://docs.python.org/3/
Python Cookbook https://python3-cookbook.readthedocs.io/zh_CN/latest/
Intermediate Python  https://github.com/yasoob/intermediatePython
Python进阶 https://docs.pythontab.com/interpy/
Python Distilled https://github.com/dabeaz/python-distilled
Robust Python  https://github.com/pviafore/RobustPython
posted @ 2022-01-18 17:24  辰令  阅读(34)  评论(0编辑  收藏  举报