Python中几个重要的函数

1.  获取变量类型的函数 type()

给变量赋值后,变量就具有确定的类型,同一变量赋值类型不同,变量的类型也就跟随变化。

money = 1000000

print(money)

print(type(money))

输出:

1000000

<class ‘int’>

money =9999.99

print(money)

print(type(money))

输出:

9999.99

<class ‘float’>

money = True

print(money)

print(type(money))

输出:

True

<class ‘bool’>

money = ‘100万’

print(money)

print(type(money))

输出:

100万

<class ‘str’>

2. 字符串输入函数input()

该函数阻塞进程,等等输入,直到输入完成。

name = input(‘请输入名字:’)

print(name)

print(type(name))

输出:

请输入名字:122

122

<class ‘str’>

所以输出类型总是字符串,需要参数算术运算则需要做类型转换。数字122,但是实际类型是字符串的。

3. id( ) 函数返回变量的内存地址

s1 = ‘hello’

s2 = s1

s3 = “hello”

s4 = “hell02”

print(id(s1))

print(id(s2))

print(id(s3))

print(id(s4))

print(s1 is s4)

print(s1, s2, s3)

s1 = ‘world’

print(s1, s2, s3)

输出:

1793056982832

1793056982832

1793056982832

1793057314160

False

hello hello hello

world hello hello

可以看出,字符在内存中存在时,不再开辟新的内存地址,当这个字符串不存在时,会开新的内存地址。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

flysh05

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

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

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

打赏作者

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

抵扣说明:

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

余额充值