当前位置:   article > 正文

Python基本数据结构_python 1j

python 1j

基本数据类型「number数字类型:整数int、浮点数float、bool布尔类型、complex复数(用j表示);字符串str;列表;元组;字典」

type()方法判断数据类型

  1. >> type(1) #1为整数型
  2. <class 'int'>
  3. >>> type(1.0) #1.0为浮点数型
  4. <class 'float'>
  5. >>> type(1j) #1j为复数型
  6. <class 'complex'>
  7. >>> type(True) #数值为布尔类型
  8. <class 'bool'>
  9. >>> type(False)
  10. <class 'bool'>
  11. >>> type('1') #'1'为字符串类型
  12. <class 'str'>
  13. >>> type([1]) #[1]为列表类型
  14. <class 'list'>
  15. >>> type((1,)) #(1,)为元组类型
  16. <class 'tuple'>
  17. >>> type((1)) #注意(1)表示运算
  18. <class 'int'>
  19. >>> type({'a':1}) #{'a':1}为字典类型
  20. <class 'dict'>

序列:字符串、列表、元组「每个元素会被分配序号,即偏移量,从0开始」

  • 字符串
  1. #字符串的拼接
  2. >>> 'hello'+'world'
  3. 'helloworld'
  4. >>>'hello'*3
  5. 'hellohellohello'
  6. #获取字符串中的字符
  7. >>>'hello world'[5] #获取字符串中的第5个字符「首个字符偏移量为0,即从0取起」
  8. ' '
  9. >>> 'hello world'[-5] #获取字符串中的倒数第5个字符
  10. 'w'
  11. >>> 'hello world'[0:5] #获取字符串中前5个字符,切片原则「左取右不取」
  12. 'hello'
  13. >>> 'hello world'[0:-5] #获取字符串中从头至倒数第五的字符段,切片原则「左取右不取」
  14. 'hello '
  15. >>> 'hello world'[:-5] #获取字符串中从头至倒数第五的字符段,切片原则「左取右不取」
  16. 'hello '
  17. >>> 'hello world'[5:] #获取字符串中从第5个字符段开始后的字符,切片原则「左取右不取」
  18. ' world'
  19. >>> 'hello world'
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/885913?site
推荐阅读
相关标签
  

闽ICP备14008679号