Python有五个标准的数据类型:
Numbers(数字)
String(字符串)
List(列表)
Tuple(元组)
Dictionary(字典)
1、Numbers(数字)
Python Number 数据类型用于存储数值。
数据类型是不允许改变的。
以下Number对象被创建。
var1 = 1
var2 = 10
用Del语句删除对象引用。
del var
del var_a, var_b
2、字符串
字符串是 Python 中最常用的数据类型。
我们可以使用引号('或")来创建字符串
示例:
var1 = 'Hello World!'
var2 = "Python"
Python访问子字符串,可以使用方括号来截取字符串
示例:
var1 = 'Hello World!'
var2 = "Python"
print "var1[0]: ", var1[0]
print "var2[1:5]: ", var2[1:5]
结果:
var1[0]: H
var2[1:5]: ytho
对已存在的字符串进行修改,并赋值给另一个变量
示例:
var1 = 'Hello World!'
print "update : ", var1[:6] + 'luo!'
结果:
update : Hello luo!
字符串运算符
+
字符串连接
*
重复输出字符串
[]
通过索引获取字符串中字符
[ : ]
截取字符串中的一部分
in
成员运算符 - 如果字符串中包含给定的字符返回 True
not in
成员运算符 - 如果字符串中不包含给定的字符返回 True
r/R
原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。 原始字符串除在字符串的第一个引号前加上字母"r"(可以大小写)以外,与普通字符串有着几乎完全相同的语法。
%
格式字符串
示例:
a = "hello"
b = "world"
print "the exampes of a + b: ",a + b
print "the exampes of a * 2: ",a * 2
print "the exampes of a[1]: ",a[1]
print "the exampes of a[1:4]: ",a[1:4]
if( "H" in a) :
print "H in a "
else :
print "H not in a "
if( "M" not in a) :
print "M not in a "
else :
print "M in a "
print r'\n'
print R'\n'
结果:
the exampes of a + b: helloworld
the exampes of a * 2: hellohello
the exampes of a[1]: e
the exampes of a[1:4]: ell
H not in a
M not in a
\n
\n
3、列表
列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。
列表的数据项不需要具有相同的类型
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]
使用下标索引来访问列表中的值,同样也可以使用方括号的形式截取字符
示例:
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7 ]
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]
结果:
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
注:下标索引是从零开始的。
对列表进行更新和修改。
示例:
list = ['physics', 'chemistry', 1997, 2000]
print "Value available at index 2 : "
print list[2]
list[2] = 2001
print "New value available at index 2 : "
print list[2]
结果:
Value available at index 2 :
1997
New value available at index 2 :
2001
删除列表重的元素
list1 = ['physics', 'chemistry', 1997, 2000]
print list1
del list1[2]
print "After deleting value at index 2 : "
print list1
列表的截取
结果:
['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]
示例:
i = ["what","is","your","name"]
print i[3]
print i[-2]
print i[1:]
结果:
name
your
['is', 'your', 'name']
注:
L[3]
读取列表中第四个元素
L[-2]
读取列表中倒数第二个元素
L[1:]
从第二个元素开始截取列表
4、元组
Python的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号,列表使用方括号。
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = "a", "b", "c", "d"
注:元组中只包含一个元素时,需要在元素后面添加逗号
元组中的元素值是不允许删除的,但可以使用del语句来删除整个元组
示例:
tup = ('physics', 'chemistry', 1997, 200)
print tup
del tup
print "After deleting tup : "
print tup
结果:
('physics', 'chemistry', 1997, 2000)
File "C:/Users/USER/Desktop/PYluo/test1.py", line 151, in
After deleting tup :
print tup
NameError: name 'tup' is not defined
元组中的元素值是不允许修改的,但可以对元组进行连接组合
示例:
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
tup3 = tup1 + tup2
print tup3
结果:
(12, 34.56, 'abc', 'xyz')
元组用下标索引来访问元组中的值、元组的索引截取都和列表的一样
5、字典
字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,键必须是唯一的,但值则不必。
d = {key1 : value1, key2 : value2 }
访问字典里的值
示例:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']
结果:
dict['Name']: Zara
dict['Age']: 7
修改字典
示例:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8 # update existing entry
dict['School'] = "DPS School" # Add new entry
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
结果:
dict['Age']: 8
dict['School']: DPS School
删除字典元素
示例:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
del dict['Name'] # del 'Name'
dict.clear() # del all
del dict # del dict
print "dict['Age']: ", dict['Age']
print "dict['class']: ", dict['class']
结果:
dict['Age']:
File "C:/Users/USER/Desktop/PYluo/test1.py", line 171, in
print "dict['Age']: ", dict['Age']
TypeError: 'type' object has no attribute '__getitem__'