小Y的Python学习日志--数据类型
#本文仅为个人学习过程的整理和记录,如有从他人博客、网站摘录的内容,本人会明确标明,如有涉及侵权,请联系本人,本人会在第一时间删除。
以下的资料整理来自(1)廖雪峰的Python教程 http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000
(2)简明Python教程 http://woodpecker.org.cn/abyteofpython_cn/chinese/
四.Python数据类型
Python中主要数据类型有:数字、字符串、列表、元组、字典
(1)数字
整形(int)
int表示范围-2,147,483,648到2,147,483,647
>>> num1=123
>>> type(123)
<class 'int'>
>>> type (num1)
<class 'int'>
长整型(long)
#长整型数据存在于Python2.X版本,3.X版本后,现在只有一种整型——int,但它的行为就像2.X版本的long
long几乎可以说任意大的整数均可存储。
为了区分整数和长整数,需要在整数后加L或小写l。
>>> num2=999999999999999999999999999
>>> type (num2)
<class 'int'>
>>> num3=3l
SyntaxError: invalid syntax
浮点型
>>> num4=1
>>> type(num4)
<class 'int'>
>>> num5=1.0
>>> type (num5)
<class 'float'>
复数型
Python对复数提供内嵌支持
>>> num6=1.23j
>>> type (num6)
<class 'complex'>
>>> num6
1.23j
>>> print (num6)
1.23j
(2)字符串
使用引号定义的一组可以包括数字、字母、符号(非特殊系统符号)的集合。
Strval='Just test.'
Strval=''Just test.''
Strval='''Just test.'''
#三引号(docstring)用来制作字符串。(注释、doc数据)
>>> str1='Hello world!'
>>> type(str1)
<class 'str'>
>>> str2="Hello world!"
>>> type (str2)
<class 'str'>
>>> str3='''Hello World'''
>>> type (str3)
<class 'str'>
>>> str4='What's up?'
SyntaxError: invalid syntax
>>> str5="What's up?"
>>> type (str5)
<class 'str'>
>>> str6='''He said : "what's up ?" '''
>>> type (str6)
<class 'str'>
>>> str7="What's\"up?\""
>>> type (str7)
<class 'str'>
>>> str7
'What\'s"up?"'
>>> mail='Tom : \n Hello! \n I am Jerry.'
>>> mail
'Tom : \n Hello! \n I am Jerry.'
>>> print (mail)
Tom :
Hello!
I am Jerry.
>>> mail='''Tom :
I am Jerry.
Come out to play.'''
>>> mail
'Tom :\n\tI am Jerry.\n\tCome out to play.'
>>> print (mail)
Tom :
I am Jerry.
Come out to play.
小结:字符串、列表、元组都被称为序列类型数据。
利用索引取字符串中的值(取值)
>>> str9='asdfghj'
>>> str9[1]
's'
>>> str9[0]+str9[1]
'as'
切片(起始值、结束值、步长值 #起始会包括,结束会截断)
>>> str9='asdfghj'
>>> str9[1]
's'
>>> str9[0]+str9[1]
'as'
>>>
>>> str9[1:4]
'sdf'
>>> str9[:4]
'asdf'
>>> str9[4:]
'ghj'
>>> str9[::]
'asdfghj'
>>> str9[::3]
'afj'
>>> str9[-1]
'j'
>>> str9[-3:-1]
'gh'
>>> str9[-1:-4]
''
>>> str9[-1:-4:-1]
'jhg'
>>> str9[-1:-5:-2]
'jg'
(3)序列
列表、元组、字符串都是序列。
序列的两个主要特点:索引操作符和切片操作符。
索引操作符人我们可以从序列中抓取一个特定项目。
索引可以是负数,位置是从序列末尾开始计算。
>>> str1='asdfghjkl'
>>> str1[3]
'f'
>>> str1[1:3]
'sd'
>>> str1[-1]
'l'
切片操作符让我们能获取序列的一个切片,即一部分序列。
切片操作符是序列名后跟一个方括号,方括号中有一对可选数字,并用冒号分割。
注意1:这与使用的索引操作非常相似,但是数字是可选的,而冒号是必须的。切片操作符中的第一个数字(冒号之前)表示切片开始位置,第二个数字(冒号之后)表示切片到哪里结束。若不指定第一个数字,从序列首开始,不指定第二个数字,停止在序列尾。
注意2:返回的序列从开始位置开始,刚好在结束位置之前结束。 即 开始位置是包括在序列切片中的,而结束位置被排斥再切片外。
>>> str1='asdfghjkl'
>>> str1[1:3]
'sd'
>>> str1[:]
'asdfghjkl'
>>> str1[::]
'asdfghjkl'
>>> str1[::2]
'adgjl'
序列的基本操作:
1.len() :求序列长度
2.+ :连接两个序列
3.* :重复序列元素
4.in :判断元素是否在序列中
5.max() :返回最大值
6:min() :返回最小值
7.cmp(tuple1,tuple2) :比较两个序列值是否相同
#关于cmp函数,Python3.0中已经取消,官方文档原文如下:The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and other rich comparisons as needed. (If you really need the cmp() functionality, you could use the expression (a > b) - (a < b) as the equivalent for cmp(a, b).)
>>> str1
'asdfghjkl'
>>> len(str1)
9
>>> str2='qwertyuio'
>>> str1+str2
'asdfghjklqwertyuio'
>>> str1*2
'asdfghjklasdfghjkl'
>>> 'k' in str1
True
>>> 'm' in str2
False
>>> m = 'milk'
>>> m in str1
False
>>> str3='1234457890'
>>> max(str3)
'9'
>>> min(str3)
'0'
>>> max(str1)
's'
>>> min(str2)
'e'
>>> str4='abcdefg'
>>> max(str4)
'g'
>>> min(str4)
'a'
>>> cmp(str1,str2)
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
cmp(str1,str2)
NameError: name 'cmp' is not defined
>>> (str1>str2)-(str1<str2)
-1
(4)元组
元组通过圆括号中用逗号分割的项目定义。
元组通常用在使语句或用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变。
>>> userinfo=('AFlin',25,'male')
>>> userinfo[2]
'male'
>>> userinfo[0]
'AFlin'
创建元组
空元组
>>> empty=()
单一元素元组
>>> singleton=(15,)
一般元组
>>> fruit=('apple','orange','banana')
>>> new_fruit=(fruit,'peach')
元组操作
元组和字符串类型同属于序列类型,可通过索引和切片操作
>>> userinfo=('AFlin',25,'male')
>>> name,age,gender=userinfo
>>> name
'AFlin'
>>> age
25
>>> gender
'male'
>>> a,b,c=(1,2,3)
>>> a
1
>>> b
2
>>> c
3

浙公网安备 33010602011771号