Python入门教程 数据类型篇

简单数据类型

字符串:”hello,world“  ”hello“+”world“  \n换行(延伸阅读)

1. https://www.python.org/dev/peps/pep-3101/ 
2. https://www.python.org/dev/peps/pep-0498/
3.https://docs.python.org/3/library/stdtypes.html#numeric- types-int-float-complex
  1. 判断字符是不是某个单词开始和结尾 endswith/startwith
IN:  'this'.startswith('t')
OUT: True

IN:  'this'.endswith('t')
OUT: False 

 2.统一大小写:lower/upper 

IN:  'abc'.upper()
OUT: ABC

IN:  'ABC'.lower()
OUT: abc

 3.split/rsplit 分割字符 默认是空格,传入参数的情况下会改变

IN:  'a b c'.split()
OUT: ['a','b','c']
##split没有参数的情况下默认是空格分割,输出是数组?有顺序吗?

IN:  'a,b,c'.split(',')
OUT: ['a','b','c']

IN:  'a,b,c'.split()
OUT: ['a,b,c']

IN:  'a b c'.rsplit(None,1)
OUT: ['a b','c']
##从右向左的分割,保留第一个分割

 4.strip/lstrip/rstrip 对空格的处理

IN:  ' abc '.strip()
OUT: 'abc'
##去除两边的空格
IN:  ' abc '.lstrip()
OUT: 'abc '
##去除左边的空格
IN:  ' abc '.rstrip()
OUT: ' abc'
##去除右边的空格

 5.replace 替换

IN:  ' abc '.replace('a','d')
OUT: 'dbc'
##d替换a

IN:  ' abc '.replace('a','d').replace('d','e')
OUT: 'ebc'
##替换之后再替换

 6.partition/rpartition 分为三部分分割

IN:  '/a/b/c '.partition('/')
OUT: ("","/","a/b/c")
##遇到/就分割
IN:  '/a/b/c '.rpartition('/')
OUT: ("/a/b","/","c")
##从右往左,遇到/分割,分割之后顺序还是不变。rXX就是从右rstrip同理

 7.join字符相加

IN:  ','.join(['a','b','c'])
OUT: ("a, b, c")

##字符相加之后用,_间隔,要是列表吗?有疑问就百度。为什么不直接用+去运算字符,因为会造成很多垃圾操作
s = ''
for p in parts:
    s += p

 8.format占位 老子先把位置占了,在考虑放什么东西

IN:  'thi{}'.format('')
OUT: 'this'

##python2写法
IN:  'thi%s'%'s'
OUT: 'this'

9.更多

>>> dir('')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

10.转义字符串(保留‘’)

IN:  name = "Fred"

IN: f"He said his name is {name}."
OUT: "He said his name is Fred"

11数据类型:整型(Int)浮点数(float)

IN:  1.1-0.2
OUT: 0.9000000000000001

IN:  1.1*0.2
OUT: 0.22000000000000003

##浮点数不能精确表示 2进制编码造成的误差
Decimal

IN:  from decimal import Decimal
IN:  Decimal("1.1")*Decimal("0.2")
OUT: Decimal('0.22')

12 类型转换

IN:  str(1)
OUT: "1"

IN:  int("1")
OUT: 1

IN:  float(1)
OUT: 1.0

##int("a")就会报错

整数除法只保留整数部分 2/3=0

13.变量和关键字

变量
a = 1
IN:  a * 2
OUT: 2

IN: a += 1
IN: a
OUT: 2

关键字

>>> import keyword
>>> ",".join(keyword.kwlist)
'False,None,True,and,as,assert,break,class,continue,def,del,elif,else,except,finally,for,from,global,if,import,in,is,lambda,nonlocal,not,or,pass,raise,return,try,while,with,yield'

##32个关键字 命名不要用上面这些

14.数值-布尔值(bool)

1.None #null
2.False(布尔型)
3.0
4.0.0 #浮点型0
5.''#空字符串
6.[]空列表    
7()#空元祖
8.{}#空字典

猜你喜欢

转载自blog.csdn.net/weixin_42199275/article/details/81417440