Python爬虫学习笔记_DAY_3_Python类型查询与类型转换【Python爬虫】

p.s.高产量博主,点个关注💓不迷路!

目录

I.Python数据类型查询

II.类型转换


I.Python数据类型查询

Python,众所周知,是一个弱类语言,也即它在定义变量的时候,不会事先声明变量的类型变量的类型真正被确定的地方是在变量的赋值处

在python中,函数type()可以返回变量的数据类型,例如下面的代码示例:

# int
a = 1
print(a)
print(type(a))

# float
b = 1.2
print(b)
print(type(b))

# boolean
c = True
print(c)
print(type(c))

# string
d = '跳探戈的小龙虾'
print(d)
print(type(d))

# list
e = [1,2,3,4,5]
print(e)
print(type(e))

# tuple
f = (1,2,3,4,5)
print(f)
print(type(f))

# dict
g = {'name' : '张三'}
print(g)
print(type(g))

通过type(),在变量赋值步骤被隐藏时,能够很快地找出变量的类型,这在爬虫技术中非常常用(爬取的数据进行类型查询)


II.类型转换

类型转换在python中也非常的常见,通常情况下类型转换分为这几个情况:

1️⃣ 其他类型转整型(int)

a = '123'
print(type(a))
b = int(a)
print(type(b))
print(b)

# float --> int
c = 1.63
print(type(c))
# float转成整数,会返回小数点前的整数,不涉及四舍五入
d = int(c)
print(type(d))
print(d)

# boolean --> int
# True 代表 1,False 代表 0
e = True
print(type(e))
f = int(e)
print(type(f))
print(f)
g = False
h = int(g)
print(h)

这部分有两个注意事项:

🅰️ 在float转int过程中,不涉及四舍五入,只是单单的把小数点后面的数据抹掉了(丢失精度)

🅱️ 布尔型变量True转int后的值是 1,False转int后的值是 0


2️⃣ 其他类型转浮点数(float)

a = '12.34'
print(type(a))
b = float(a)
print(type(b))
print(b)

c = 666
print(type(c))
d = float(c)
print(type(d))
print(d)

这部分本身没有需要注意的地方,但是要点名的是,第一句 a = '12.34' 的变量a,是不能直接由字符串转成整型int类型的,只能直接转浮点数float(即转int的字符串本身不能包含特殊字符)


3️⃣ 其他类型转字符串(string)

# int -> string
a = 80
print(type(a))
b = str(a)
print(type(b))
print(b)

# float -> string
c = 1.2
print(type(c))
d = str(c)
print(type(d))
print(d)

# boolean -> string
e = True
print(type(e))
f = str(e)
print(type(f))
print(f)

要注意的是,布尔型转string的时候,不是转成0或1,而是转成字符串'False'或'True'。


4️⃣ 其他类型转布尔型(boolean)

# int -> boolean
a = 1
print(type(a))
b = bool(a)
print(type(b))
print(b)
# 非0的整数都是true,包括负整数和正整数

c = 0
print(type(c))
d = bool(c)
print(type(d))
print(d)

e = -1
print(type(e))
f = bool(e)
print(type(f))
print(f)

# float -> boolean
g = 1.0
print(type(g))
h = bool(g)
print(type(h))
print(h)

# 0.0相当于0,所以0.0是false,除了0.0的浮点数无论正负都是false

i = 0.0
print(type(i))
j = bool(i)
print(type(j))
print(j)

# string -> boolean
# 只要字符串有内容,都是True(内容甚至包括空格),但是没有内容的,就是False
k = '跳探戈的小龙虾'
print(type(k))
l = bool(k)
print(type(l))
print(l)

# 这个就是包含空格,运行结果是True
m = ' '
print(type(m))
n = bool(m)
print(type(n))
print(n)

# list -> boolean
# 只要列表有数据,就是True
o = ['a','b','c']
print(type(o))
p = bool(o)
print(type(o))
print(p)

# 空列表是False
q = []
print(type(q))
r = bool(q)
print(type(r))
print(r)

# tuple -> boolean
# 只要元组有数据,就是True
s = ('a','b')
print(type(s))
t = bool(s)
print(type(t))
print(t)

# 空元组是False
u = ()
print(type(u))
v = bool(u)
print(type(v))
print(v)

# dict -> boolean
# 只要字典有数据,就是True
w = {'a' : '1','b' : '2'}
print(type(w))
x = bool(w)
print(type(x))
print(x)

# 空字典是False
y = {'a' : '1','b' : '2'}
print(type(y))
z = bool(y)
print(type(z))
print(z)

这部分的所有规定全部注释在代码中,可认真观察代码学习!🌟🌟🌟

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

跳探戈的小龙虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值