中谷教育05 Python数据类型
来源:互联网 发布:seo自动优化工具 编辑:程序博客网 时间:2024/04/19 10:20
Python中主要的数据类型
- 数字
- 字符串
- 列表
- 元组
- 字典
数字
- 整型
- 长整型(数字后面加l或者L)
- 浮点型(数字后面加. 或者.0)
- 复数型
Python 中不需要事先定义类型,它会根据具体的值判断类型
>>> num1 = 123>>> type(num1)<type 'int'>>>> type(123)<type 'int'>>>> num2 = 999999999999999999999999999999999>>> type(num2)<type 'long'>>>> num3 = 123L>>> type(num3)<type 'long'>>>> num4 = 123l>>> type(num4)<type 'long'>>>> f1 = 123.>>> type(f1)<type 'float'>>>> f2 = 123.0>>> type(f2)<type 'float'>>>> c = 3.14j>>> type(c)<type 'complex'>>>> c1 = 3.14J>>> type(c1)<type 'complex'>
字符串
使用引号定义的一组可以包含数字、子母、符号(非特殊系统符号)的集合。
字符串定义
可以使用单引号,双引号和三重引号定义
>>> str1 = 'str1'>>> str2 = "str2">>> str3 = """str3""">>> str4 = '''str4'''>>> str5 = "''str5''">>> str6 = "'"str6"'" File "<stdin>", line 1 str6 = "'"str6"'" ^SyntaxError: invalid syntax>>> str7 = ""str7"" File "<stdin>", line 1 str7 = ""str7"" ^SyntaxError: invalid syntax>>> str8 = '""str8""'>>> str9 = ''"str9"''>>> str10 = '"'str10'"' File "<stdin>", line 1 str10 = '"'str10'"' ^SyntaxError: invalid syntax
那么该怎么用呢?其实一般情况下区别不大,除非引号中有特殊的符号,如下:
>>> str11 = 'let's go' File "<stdin>", line 1 str11 = 'let's go' ^SyntaxError: invalid syntax>>> str12 = "let's go">>> str13 = "let's "go" !" File "<stdin>", line 1 str13 = "let's "go" !" ^SyntaxError: invalid syntax>>> str13 = "let's \"go\" !">>> print str13let's "go" !>>> str14 = 'let\'s \"go\" !'>>> print str14let's "go" !
"\"是转移字符"\n"代表换行,通过这个就可以打印出规定格式的字符串。但是有时候,这个看起来不够直观,而且难以控制,这时候就需要三重引号啦!~
>>> mail = """ tom:... i am jack!... I miss U... bye~~""">>> print mail tom: i am jack! I miss U bye~~>>> mail' tom:\n i am jack!\n I miss U\n bye~~'
字符串操作(切片和索引)
a[start:end:step]分别代表:开始位置,结束为止(不包括在内),和步长
>>> a = 'abcdefg'>>> len(a)7>>> a[1]'b'>>> a[6]'g'>>> a[7]Traceback (most recent call last): File "<stdin>", line 1, in <module>IndexError: string index out of range>>> a[0][1]Traceback (most recent call last): File "<stdin>", line 1, in <module>IndexError: string index out of range>>> a[0]a[1] File "<stdin>", line 1 a[0]a[1] ^SyntaxError: invalid syntax>>> a[0]+a[1]'ab'>>> a[-1]'g'>>> a[0:1]'a'>>> a[0:7]'abcdefg'>>> a[:7]'abcdefg'>>> a[:]'abcdefg'>>> a[1:]'bcdefg'>>> a[0:5]'abcde'>>> a[0:5:0]Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: slice step cannot be zero>>> a[0:5:1]'abcde'>>> a[0:5:2]'ace'>>> a[0:5:-2]''>>> a[5:0:-2]'fdb'
0 0
- 中谷教育05 Python数据类型
- 中谷教育06 Python数据类型 序列 元组
- python视频教学笔记 中谷教育
- python视频教学笔记 中谷教育
- 中谷教育01 走进 python
- 中谷教育02 搭建Python环境
- 中谷教育03 Python变量
- 中谷教育Python05笔记——数据类型、数字、字符串
- 中谷教育04 Python运算符与表达式
- (中谷教育视频学习)python笔记
- Python中数据类型
- Python中数据类型-练习
- Python中基本数据类型
- Python中数据类型
- python中数据类型
- Python中数据类型
- Python中数据类型
- python中数据类型转换
- uva 12325 - Zombie's Treasure Chest
- Nginx系列(五)--nginx+tomcat实现负载均衡
- const 用法
- SQL Server 2012 SEQUENCE 使用
- composer 报 zlib_decode(): data error
- 中谷教育05 Python数据类型
- 基于json-lib.jar包 创建JSONArray的四个常用方法
- PHP检测当前字符编码并转码
- linux磁盘分区详解
- 何谓组合事务ID
- 浅谈语言
- HDU1542 线扫描求矩形面积并
- c++ bitset类用法
- 38Khz红外遥控发射