This page looks best with JavaScript enabled
Python笔记-基础
Python数据类型
-
Number 数字
- int 有符号整数
- long 长整形,也可以代表十六进制或八进制
- float 浮点型
- complex 复数
按索引存取
1
|
list = ['item1', 2, '3', 4, 5]
|
按索引存取
1
|
tuple = ('item1', 2, '3', 4, 5)
|
按键值对存取
1
2
3
4
|
dict = {} //声明一个字典变量
dict[2] = "2" //key为2的值为"2"
dict['key2'] = "this is key2" //key为`key2`的值为"this is key2"
tinyDict = {'key1': 'value1', 'key2': 2} //声明并用键值对初始化字典
|
运算符
1
2
3
|
a = 10
b = 2
a = a ** b #a=100
|
1
2
|
9//2 输出结果 4
9.0//2.0 输出结果 4.0
|
- 幂赋值运算
a **= c
等效于a = a ** c
- 取整除运算
a //= c
等效于a = a // c
1
2
3
4
|
a = 5.0
c = 2
a /= c #a=2.5
a // = c #a=2.0
|
逻辑运算符
and
等效 与
or
等效 或
not
等效 非
成员运算符
成员运算符常用于字符串,列表,元组
- in 如果在序列中找到就返回True,否则返回False
- not in 如果在序列中没有找到就返回True,否则返回False
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
a = 'test'
b = 1
list = ['test', 0, 10, 11]
if (a in list):
print a, 'in list'
else:
print a, 'not in list'
if (b not in list):
print b, 'not in list'
else:
print b, 'in list'
|
身份运算符
判断两个对象的储存单元
is
判断两个标识符是不是引用自一个对象
is not
判断两个标识符是否引用自不同对象
1
2
3
4
|
a = 1
b = 1
print (a is b) # True
|
is
和==
的区别
is
类似Java的==
,判断两个对象的引用是否为同一个
==
类似Java的equals()
,判断两个对象的值是否相等
1
2
3
4
5
6
7
|
a = [1, 2, 3, 4]
b = a
print (a is b) # True
b = a[:] #相当于 b = a[0:],b中内容与a完全一样,只是b引用了一个新的对象
print (a is b) # False
print (a == b) # True
|
WRITTEN BY
Yang
Developer