# 类型转换
s ="0425"print(f'{type(int(s))}')# int可以将由纯整数构成的字符串直接转换成整型,若包含其他任意非整数符号,则会报错# 十进制转换print(bin(3))print(hex(3))print(oct(3))# float
s ="4.25"print(f'{type(float(s))}')
<class 'int'>
0b11
0x3
0o3
<class 'float'>
数字类型的使用
# 1、数学运算
a =5
b =5
c =5.5print(a +c)# int 和 float 可以相加
res = a + b
print(res)
类型: <class 'int'>
10.5
10
# 2、比较大小
a =10
b =5
res = a < b
print(res)
False
字符串 str
# str 用来记录人的名字,家庭住址,性别等描述性质的状态# 用 单引号、双引号、多引号 都可以定义字符串,在本质上是没有区别的。# 定义
name ='cody'
address ='广东省深圳市'
sex ='男'print('类型:',type(name))# 特殊情况# 字典
info ={'name':'cody','age':18}print("f'姓名:{info['name']}, 年龄:{info['age']}'")# 这里要注意 引号问题 单引号,双引号
# 1、列表类型是用索引来对应值,索引代表的是数据的位置,从0开始计数
info =['cody','18','Python']print(info.index('cody'))# 查看 列表 info 中 ‘cody’ 的索引print(info[0])# 正向取(从左往右)print(info[-2])# 反向取(负号表示从右往左)
info[1]="coding"print(info)# 2、列表可以嵌套,嵌套取值如下
info =[['cody',18],['coding',20,['篮球']]]print(info[1][2])
0
cody
18
['cody', 'coding', 'Python']
['篮球']
# 切片
info =['cody','18','Python','java','c#']print(info[0:2])# 顾头不顾尾:取出索引为0到3的元素print(info[0:4:2])# 步长:0:4:2,第三个参数2代表步长,会从0开始,每次累加一个2即可,所以会取出索引0、2的元素# len() 长度print(len(info))# 成员运算符 in not inprint('cody'in info)print("coding"notin info)
tuple1 =('cody','python','18')print(tuple1[0])print(tuple1[-1])
tuple1[0]='coding'print(tuple1)# TypeError: 'tuple' object does not support item assignment# 按索引取值(正向取/反向取):只能取,不能改
cody
18
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-154-bab0f2781e6e> in <module>
3 print(tuple1[-1])
4
----> 5 tuple1[0]= 'coding'
6 print(tuple1)
TypeError: 'tuple' object does not support item assignment
# 循环
info ={'name':'cody','age':18,'hobby':'篮球'}# 默认遍历的是字典的keyfor key in info:print(key)print('-'*30)# 只遍历 keyfor key in info.keys():print(key)print('-'*30)# 只遍历 value for key in info.values():print(key)print('-'*30)# 遍历 key 和 value for key in info.items():print(key)
name
age
hobby
------------------------------
name
age
hobby
------------------------------
cody
18
篮球
('name', 'cody')
('age', 18)
('hobby', '篮球')
# .get()
info ={'name':'cody','age':18,'hobby':'篮球'}print(info.get("name"))# key存在,则获取key对应的value值print(info.get('like','key 不存在'))# key不存在时,设置默认返回的值# popprint(info.pop('age'))# 删除指定的key对应的键值对,并返回值print(info)print(info.popitem())# 随机删除一组键值对,并将删除的键值放到元组内返回print(info)
# 去重# 1. 只能针对不可变类型# 2. 集合本身是无序的,去重之后无法保留原来的顺序
L =[1,2,4,2,4,5,6]
s =set(L)print(s)
l1 =[]for i in L:if i notin l1:
l1.append(i)print(l1)# 重重且保证了排序
{1, 2, 4, 5, 6}
[1, 2, 4, 5, 6]
布尔 bool
# bool 用来记录真假这两种状态# 定义:
a =True
b =Falseprint(type(a))print(b)# 通常用来当作判断的条件
<class 'bool'>
False
数据类型转换
# 转整数 int()
a ='10'# int 只能将纯数字的字符串转成整型# b = int('10.5')print(type(int(a)))# print(type(b))# ValueError: invalid literal for int() with base 10: '10.5'# 转字符串 str()
age =10print(type(str(age)))# 将字符串形式的数据,转换为原本的a类型 eval()
str1 ='135.5'print(type(eval(str1)))
<class 'int'>
<class 'str'>
<class 'float'>
可变和不可变类型
# 可变类型: 值改变, id不变 可以说明改的是 值# 不可变类型: 值改变 id 也改变 可以说明 是产生了新的值 ,没有改变原值, 原值 是不可修改# int 是 不可变类型
a =10print(f'10的id:{id(a)}')
a =11print(f'12的id:{id(a)}')# float 是 不可变类型
b =1.1print(f'1.1的id:{id(b)}')
b =1.2print(f'1.2的id:{id(b)}')# str 是 不可变类型
c ='cody'print(f'cody的id:{id(c)}')
c ='coding'print(f'coding的id:{id(c)}')# bool 是 不可变类型# list 是 可变类型
l1 =['cody','coding']print(f'l1的id:{id(l1)}')
l1[0]=['java']print(f'改值之后的id{id(l1)}')# dict 是 可变类型
dict1 ={'name':'cody','age':20}print(f'dict1的id:{id(dict1)}')
dict1['name']='coding'print(f'改值之后的id{id(dict1)}')# 不可变类型 : int、 str 、float 、bool# 可变类型: list 、 dict