提问:
123 和 “123”相同吗?
() [ ] {}
Python数据类型
计算机人们日常事务的辅助工具,在程序设计中也映射了现实世界的分类,因此计算机中也引入类别以便进行抽象分析
Python的数据类型:
--- 数字
-----字符串
-----元组
-----列表
-----字典
数字类型
--- 整型
int 表示的范围 -2,147,483,648 到 2,147,483,647 例如: 0, 100, -100 >>>num= 2147483647 >>>type(num) >>><class 'int'> 一旦超出, 则变类型;
Python中不用指定数据的类型
---长整型
>>num=123L(低版本中存在) 在高版本中, 么有 , 所有数据在定义过程中不需要指定类型
---浮点型
>>num=12.0
>>type(num)
>><class,'float'>
---复数型 用 j表示虚部
>>c=3.12j
>>type(c)
>><class, 'complex'>
>>a=123
>>stra="123"
>>type(a)
>><class, 'int'>
type(stra)
>><class, 'str'>
字符串类型 string
使用引号定义的一组可以包含数组,字母,符号(非特殊系统符号)的集合。
strval='this isa test!' 单引号
strVal ="this is a Test!" 双引号
strval = """ This is a test!!""" 三重引号
三重引号(docstring)通常用来制作字符串,在面向对象时详解
>>> str1='hello world'
>>>type(str1)
>>><class, 'str">
>>>str2="Hello world"
>>>type(str2)
>>><class, 'str">
原则:如果在字符创中,存储多个引号 则单双引号配合用 如果存在多个双引号 则 用转义符号 \ , 换行转义符 \n
>>>str4=""" Tom:
... I am jack
... goodbye
... """
>>>str4
>>>Tom:\n I am jack\n goodbye\n"
python中 字符创,元组,列表,统称序列类型数据
// 字符串 索引取值
>>>a='abcde'
>>>a[2]
>>>'c'
>>a[1] + a[2]
>>'bc'
// 字符串切边取值 三个参数 1 起始位 2 结束位 3 步长值
>>>a='abcde;
>>>a[1:4] # 开始位和结束位
>>>'bcd'
>>>a[:4] #从头开始取
>>>'abcd'
>>>a[4:] #从特定位开始取到结束
>>>'e'
>>>a[::1]
>>>‘abcde'
>>>a[::2]
>>>'ace'
/// 索引为负号
>>>a[-1]
>>>'e'
>>>a[-4:-1] // 从左往右取值
>>>’bcd'
>>>a[-2:-4:-1] // 第三个步长参数 控制取值顺序
>>'dc‘