一、简介
- 数据类型:整数、浮点、字符串、布尔、列表、元组、字典、集合...
- 可以通过 type() 来获取数据类型:
```py
name = "abc" type(name)
name = 1 type(name)
name = 12.2 type(name)
name = False type(name)
name = [1, 2, 3] type(name)
name = {'name': 'dzm'} type(name)
```
- 可以通过 obj.,然后按两下 tab 键来获得属性所支持的方法:
```py
name = [] name. name.append( name.copy() name.extend( name.insert( name.remove( name.sort( name.clear() name.count( name.index( name.pop( name.reverse() name. ```
- 可以通过 help(数据类型) 来获取数据类似支持的方法:
```py
help(str) class str(object) | str(object='') -> str | str(bytesorbuffer[, encoding[, errors]]) -> str | | Create a new string object from the given object. If encoding or | errors is specified, then the object must expose a data buffer | that will be decoded using the given encoding and error handler. | Otherwise, returns the result of object.str() (if defined) | or repr(object). | encoding defaults to sys.getdefaultencoding(). | errors defaults to 'strict'. | | Methods defined here: | | add(self, value, /) | Return self+value. | | contains(self, key, /) | Return key in self. | | eq(self, value, /) | Return self==value. : ```
二、数据类型
- int:整数类型
- float:浮点数类型
- str:字符串类型
```py
name1 = "abc" name2 = "def" # 相加可以进行拼接 print(name1 + name2) abcdef # 乘以数字则会输出指定次数 print(name1 * 3) abcabcabc ```
- bool:布尔类型
```py
a = 3 b = 4 print(a > b) False print(a < b) True py a = 3 b = 4 if (a > b): print("成功") else: print("失败") ```
- list:列表类型
```py
定义空列表
name = []
查看类型
type(name)
设置列表数据
name = ['a', 'b', 'c']
获取指定索引的对象
print(name[0]) a
修改指定索引的对象
name[0] = 'd' print(name) ['d', 'b', 'c']
将对象插入到指定索引
name.insert(3, 'a') print(name) ['d', 'b', 'c', 'a']
在列表尾部追加对象
name.append('f') print(name) ['d', 'b', 'c', 'a', 'f']
```
- dict:字典类型
```py
name = {'a': 'b'} print(name['a']) b ```
- 元组类型