python数据类型练习
时间: 2023-11-28 07:45:10 浏览: 88
python数据分析练习题
5星 · 资源好评率100%
以下是Python数据类型练习的一些例子:
1. 将字符串转换为数字
```python
a = "123"
print(type(a)) # 输出:<class 'str'>
b = int(a)
print(type(b)) # 输出:<class 'int'>
```
2. 判断字典中是否存在某个值
```python
dic = {"key01": "value01", "key02": "value02", "key03": "value03"}
v1 = "key01" in dic.keys()
v2 = "value01" in dic.values()
print(v1) # 输出:True
print(v2) # 输出:True
```
3. 列表切片
```python
lst = [1, 2, 3, 4, 5]
print(lst[1:3]) # 输出:[2, 3]
```
4. 列表去重
```python
lst = [1, 2, 3, 2, 4, 3, 5]
lst = list(set(lst))
print(lst) # 输出:[1, 2, 3, 4, 5]
```
5. 列表排序
```python
lst = [3, 1, 4, 2, 5]
lst.sort()
print(lst) # 输出:[1, 2, 3, 4, 5]
```
6. 判断变量类型
```python
a = None
b = ""
c = ()
d = []
e = {}
f = 0
print(bool(a)) # 输出:False
print(bool(b)) # 输出:False
print(bool(c)) # 输出:False
print(bool(d)) # 输出:False
print(bool(e)) # 输出:False
print(bool(f)) # 输出:False
```
阅读全文