-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataType.py
More file actions
104 lines (66 loc) · 2.16 KB
/
dataType.py
File metadata and controls
104 lines (66 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#coding:utf8
#序列类型的数据类型:列表、元组、字符串
#列表的定义:[元素1,元素2,....] 通过索引下标来引用
list = [1,2,3,4,'python','helloworld']
#通过列表的append(item)向列表后面添加元素
print(list[4]); #输出的是python
list.append('beautyful');
print(list);#输出[1, 2, 3, 4, 'python', 'helloworld', 'beautyful']
#元组(tuple) 和列表的一些方法
t = (1,2,3) #定义元组
#len() 获取元素的长度
print(len(t)); # 3
#del() 删除元素 , 不能删除元组的元素,可以删除列表的元素
del(list[0]); # 删除了 1
print(list)
#切片方法(slice) 获取元组或者列表的元素组成的子集,通过[开始下标:结束下标]来获取
print(t[1:3]) #获取到[2,3]
#in关键词 3 in [1,2,3] 判断元素是否存在元组或者列表中
if 3 in t:
print('存在')
else:
print('不存在')
#''' 字符串内容 ''' '''..'''长字符串类型
str = '''
<html>
<head></head>
<body></body>
</html>
'''
print(str) #按原格式输出
#字典数据类型,相当于js的对象格式或者JSON格式{"name":"python","version":3.3.2}
#它是一组hash值
isHash = {"name":"helloPython","version":3}
print(isHash);
#dict(key) 获取字典中key的值
print(isHash["name"]) #输出helloPython
#判断key是否存在字典中
print("name" in isHash)
#异常处理
demo = {"name":"python"}
try:
demo['age']
except:
print(" The key in not ready")
else:
print("no error")
#def关键字定义一个函数
def echoYourName(name):
print(name)
#调用函数echoYourName
echoYourName('my name is python!')
#python中一个简单的类定义
class person: #定义一个类名为person的类
#自定义的类变量
country = "我来自荷兰"
#构造函数或者是初始化函数
def __init__(self,name,gender,age):
self.name = name,
self.gender = gender,
self.age = age
#自定义的一个类方法
def getInfo(self): #self相当于JS的this 指向当前对象
print("我是",self.name)
python = person('python','language',25)
python.getInfo()
print(python.country) #输出 我来自荷兰