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
可以看出,字符在内存中存在时,不再开辟新的内存地址,当这个字符串不存在时,会开新的内存地址。
网友评论