脚本专栏 发布日期:2025/2/23 浏览次数:1
本文实例讲述了Python变量、数据类型、数据类型转换相关函数用法。分享给大家供大家参考,具体如下:
说明:虽然python声明变量时没有一个类型来圈注,但它并不是弱类型语言,相反,它是一门强类型语言。
python3中移除了python2中的long
Python3中没有限制int数值的大小
> i=0b1111 > print(i) 15 > i=0x0010 > print(i) 16 > i=0o0010 > print(i) 8 > i=0O0010 > print(i) 8
1.2e-5)
> a=1.5 > print(a) 1.5 > a=-9999.5 > print(a) -9999.5 > a=1.5e5 > print(a) 150000.0 > a=1.5e-10 > print(a) 1.5e-10 > a=0.0000000000000001 > print(a) 1e-16
注:对于太小的数,会自动转化成科学计数法表示,太大的数不会自动转化
> type(True) <class 'bool'> > type(False) <class 'bool'> > a=bool(2) > a True > a=int(True) > a 1 > print(int(False)) 0
1
的字符串> type("aaaa") <class 'str'> > type('aaaa') <class 'str'>
> str1="123" > str1[0] '1' > str1[-1] '3'
【 [:]代表完全切片,[:右下标]代表从零开始,[左下边:]代表结尾是最后一位,-1下标代表最后一位 】
【切片也可以有间隔,用法字符串名[左下标:右下标:间隔] 】
> hello="hello world!" > new_hello=hello[:] > new_hello 'hello world!' > hello[:2] 'he' > hello[1:3] 'el' > hello[1:-1] 'ello world'
> hello[1:-1:1] 'ello world' > hello[1:-1:2] 'el ol'
> type("""hahah haha hahah""") <class 'str'> > type('''第一行 第二行 第三行''') <class 'str'>
> i=['a',100,True] > type(i) <class 'list'>
> list("abcd") ['a', 'b', 'c', 'd']
> i ['a', 100, True] > i[0]='b' > i ['b', 100, True]
> i ['b', 100, True] > l=[i,"helloworld"] > l [['b', 100, True], 'helloworld'] > l[0][0] 'b'
> l2=i*2 > l2 ['b', 100, True, 'b', 100, True] > l3=i+l > l3 ['b', 100, True, ['b', 100, True], 'helloworld']
> t1=('a',1,True) > type(t1) <class 'tuple'> > t2=('a') > type(t2) <class 'str'> > ####注意上面的类型### > t3=('a',) > type(t3) <class 'tuple'>
> tuple2=1,2,3,4,5 > type(tuple2) <class 'tuple'> > tuple2 (1, 2, 3, 4, 5)
> t1=('a',i) > t1 ('a', ['b', 100, True]) > id(t1[1]) 1673650817160 > id(i) 1673650817160
tuple一旦初始化就不能修改,所以它没有append()、insert(),也没有pop()等能增删元素的方法
> tuple1=(1,2,3,4) > print(tuple1.index(2))#查找指定元素的下标 1 > print(tuple1.count(2))#查找某元素存在的数量 1
> d1={'a':'apple','b':'banana','c':'cabbage'} > type(d1) <class 'dict'>
> l1=['a'] > d1[l1]='c' Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> d1[l1]='c' TypeError: unhashable type: 'list'
> d1 {'a': 'apple', 'b': 'banana', 'c': 'cabbage', ('a',): 'c'} > d1['a'] 'apple' > d1['d'] Traceback (most recent call last): File "<pyshell#17>", line 1, in <module> d1['d'] KeyError: 'd'
> d1 {'a': 'apple', 'b': 'banana', 'c': 'cabbage', ('a',): 'c'} > d1['a']='apple pen' > d1 {'a': 'apple pen', 'b': 'banana', 'c': 'cabbage', ('a',): 'c'}
> d1 {'a': 'apple pen', 'b': 'banana', 'c': 'cabbage', ('a',): 'c'} > d1['ai']='python' > d1 {'a': 'apple pen', 'b': 'banana', 'c': 'cabbage', ('a',): 'c', 'ai': 'python'}
> dict10={1:"苹果","雪碧":"雪梨"} > > for i in dict10: print(i) 1 雪碧
> dict10 {1: '苹果', '雪碧': '雪梨'} > 1 in dict10 True > 3 in dict10 False > print(dict10.get(3)) None > print(dict10.get(1)) 苹果
> s1={'a','b','c'} > s1 {'b', 'c', 'a'} > type(s1) <class 'set'>
> s2=set() > type(s2) <class 'set'> > s3=set(['a','b','c']) > type(s3) <class 'set'>
> s3.add(['cbd']) Traceback (most recent call last): File "<pyshell#37>", line 1, in <module> s3.add(['cbd']) TypeError: unhashable type: 'list'
int()
函数可以把其他数据类型转换为整数:
> print(type(int("123"))) <class 'int'> > print(type(float("123"))) <class 'float'> > float("123") 123.0 > str(123) '123' > bool(2) True > list("123") ['1', '2', '3'] > tuple("123") ('1', '2', '3')
注:转换是有规则的,要符合规则才能转化,比如int()不能转换"abc"
关于Python相关内容感兴趣的读者可查看本站专题:《Python函数使用技巧总结》、《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。