Python第二讲基本数据类型
来源:互联网 发布:温度监控软件 编辑:程序博客网 时间:2024/06/02 02:34
Python第二讲基本数据类型
python的数据类型大致有这几类:
- 数字Number
python3支持4种:int,float,bool,complex(复数),可以用type()来查看类型
>>>a,b,c,d=1,1.2,True,1+1.3j>>>print(type(a),type(b),type(c),type(d))<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>
运算也很简单,可以直接计算
>>>1+1 #加法2>>>9.9-1.8 #减法8.1>>>3*0.9 #乘法2.7>>>8/2 #除法:得到一个浮点数4.0>>>8/1.1 #除法:得到一个浮点数7.2727272727272725>>>8//3 #除法:得到一个整数2>>>13%5 #取余2>>>5**3 ##5的3次方125
- 字符串String
Python中的字符串str用单引号(’ ‘)或双引号(” “)括起来,当然也可以用三个单引号三个双引号,同时使用反斜杠()转义特殊字符
>>>a,b,c,d='hello',"world",'''hello''',"""python""">>>print(a,type(a),b,type(b),c,type(c),d,type(d))hello <class 'str'> world <class 'str'> hello <class 'str'> python <class 'str'>>>>c='I\'m Tom'>>>print(c,type(c))I'm Tom <class 'str'>
\n是换行,有的时候不想转义,那么可在字符串前面加r
>>>c='C:\Program Files\nature'>>>print(c,type(c))C:\Program Filesature <class 'str'>>>>c=r'C:\Program Files\nature'>>>print(c,type(c))C:\Program Files\nature <class 'str'>
字符串拼接用+,重复出现用*,长度用len()
>>>c='hello'>>>print('Str'+'ing','ok'*3,len(c))String okokok 5
字符串的索引规则从左为0开始,从右为-1开始
>>>c='hello'>>>print(c[0],c[4]) #注意不能越界h o>>>print(c[-1],c[-5])o h
- 列表list
和java的数组差不多,不过里面的元素类型可以不相同
>>>a=['My','age',25]>>>print(a,type(a))['My', 'age', 25] <class 'list'>
list可以直接用+拼接,并且可以通过下标获取值
>>>a=['My','age',24]>>>a+['hello','python']['My', 'age', 25, 'hello', 'python']>>>a[0]'My'
- 元组Tuple
tuple和list差不多,tuple是写在小括号中,用逗号隔开
>>>a=(1,2,3,4,'hello')>>>print(a,type(a))(1, 2, 3, 4, 'hello') <class 'tuple'>
和list用法差不多,就不详细介绍了,说几个注意点:
1.支持+号拼接
2.元素值不可改变
3.可以把list放元组中
4.支持空元组
5.支持索引,索引用方括号,例如:a[0]
- 集合Sets
set是无序不重复的集合,可以用()也可以用{},用()时必须写上set()
>>>a={'a','b','c','a'}>>>print(a,type(a)){'b', 'c', 'a'} <class 'set'>>>>b=set('aabbcc')>>>print(b,type(b)){'b', 'c', 'a'} <class 'set'>>>>c=('ddffcc')>>>print(c,type(c))ddffcc <class 'str'>
- 字典Dictionaries
和java中的json很像,是无序的键值对,关键字不能重复
>>>computer={u'联想':u'中国',u'华硕':u'台湾'}>>>print(computer,type(computer)){'联想': '中国', '华硕': '台湾'} <class 'dict'>>>>computer[u'联想']'中国'>>>computer[u'三星']=[u'韩国']>>>print(computer,type(computer)){'联想': '中国', '三星': ['韩国'], '华硕': '台湾'} <class 'dict'>>>>del computer[u'三星']>>>print(computer,type(computer)){'联想': '中国', '华硕': '台湾'} <class 'dict'>>>>u'联想' in computerTrue>>>u'三星' in computerFalse
目录
用 [TOC]
来生成目录:
1 0
- Python第二讲基本数据类型
- java第二讲 基本数据类型
- PYTHON第二讲:输入基本控制结构
- 武sir第二讲(2)基本数据类型
- 第二讲 数据类型
- 第二讲:c/c++数据类型
- [Python]第三讲:数据类型
- python学习第二讲
- 第二讲 基本图元
- 韩顺平 java 第二讲 变量 数据类型
- python基本数据类型
- python 的基本数据类型
- 基本数据类型-python
- python 基本数据类型
- python基本数据类型笔记
- Python基本数据类型
- python基本数据类型
- Python基本数据类型
- ajax post中文乱码问题
- iOS 开发小技巧
- 正益移动王国春当选“2015中国互联网+最具创新力年度人物”
- 实施针对 TLS_FALLBACK_SCSV 的支持。禁用 SSLv3 支持
- 她是张作霖钦定的儿媳 但张学良最终没选择她
- Python第二讲基本数据类型
- 安卓onkeyup onkeydown事件小记
- 2015年终总结,细细的数曾经走过的路
- iOS每日一记————————CoreText初识
- Android原生下拉刷新SwipeRefreshLayout
- ScrollView滚动到指定位置
- 三种获取打气筒常用的API
- XML特殊字符转义
- CentOS下UTF8和GBK的互转