1、运算符
以下运算符中,a=100,b=200
算术运算符
运算符 | 描述 | 实例 |
---|---|---|
+ | 加 - 两个对象相加 | a + b 输出结果 300 |
- | 减 - 得到负数或是一个数减去另一个数 | a - b 输出结果 -100 |
* | 乘 - 两个数相乘或是返回一个被重复若干次的字符串 | a * b 输出结果 20000 |
/ | 除 - x除以y | b / a 输出结果 2 |
% | 取模 - 返回除法的余数 | b % a 输出结果 0 |
** | 幂 - 返回x的y次幂 | a**b 为100的200次方 |
// | 取整除 - 返回商的整数部分 | 9//2 输出结果 4 , 9.0//2.0 输出结果 4.0 |
比较(关系)运算符
运算符 | 描述 | 实例 |
---|---|---|
== | 等于 - 比较对象是否相等 | (a == b) 返回 False。 |
!= | 不等于 - 比较两个对象是否不相等 | (a != b) 返回 true. |
<> | 不等于 - 比较两个对象是否不相等 | (a <> b) 返回 true。这个运算符类似 != 。 |
> | 大于 - 返回x是否大于y | (a > b) 返回 False。 |
< | 小于 - 返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。 | (a < b) 返回 true。 |
>= | 大于等于 - 返回x是否大于等于y。 | (a >= b) 返回 False。 |
<= | 小于等于 - 返回x是否小于等于y。 | (a <= b) 返回 true。 |
赋值运算符
运算符 | 描述 | 实例 |
---|---|---|
= | 简单的赋值运算符 | c = a + b 将 a + b 的运算结果赋值为 c |
+= | 加法赋值运算符 | c += a 等效于 c = c + a |
-= | 减法赋值运算符 | c -= a 等效于 c = c - a |
*= | 乘法赋值运算符 | c *= a 等效于 c = c * a |
/= | 除法赋值运算符 | c /= a 等效于 c = c / a§ |
%= | 取模赋值运算符 | c %= a 等效于 c = c % a |
**= | 幂赋值运算符 | c **= a 等效于 c = c ** a |
//= | 取整除赋值运算符 | c //= a 等效于 c = c // a |
逻辑运算符
运算符 | 描述 | 实例 |
---|---|---|
and | x and y | 布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。 |
or | x or y | 布尔"或" - 如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值。 |
not | not x | 布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。 |
位运算符
运算符 | 描述 | 实例 |
---|---|---|
& | 按位与运算符:参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0 | (a & b) 输出结果 64 ,二进制解释: 0100 0000 |
按位或运算符:只要对应的二个二进位有一个为1时,结果位就为1。 | ||
^ | 按位异或运算符:当两对应的二进位相异时,结果为1 | (a ^ b) 输出结果 172 ,二进制解释:1010 1100 |
~ | 按位取反运算符:对数据的每个二进制位取反,即把1变为0,把0变为1 。~x 类似于 -x-1 | (~a ) 输出结果 -101 ,二进制解释: 0110 0101,在一个有符号二进制数的补码形式。 |
<< | 左移动运算符:运算数的各二进位全部左移若干位,由 << 右边的数字指定了移动的位数,高位丢弃,低位补0。 | a << 2 输出结果 400 ,二进制解释: 1 1001 0000 |
>> | 右移动运算符:把">>"左边的运算数的各二进位全部右移若干位,>> 右边的数字指定了移动的位数 | a >> 2 输出结果 25 ,二进制解释: 0001 1001 |
成员运算符
运算符 | 描述 | 实例 |
---|---|---|
in | 如果在指定的序列中找到值返回 True,否则返回 False。 | x 在 y 序列中 , 如果 x 在 y 序列中返回 True。 |
not in | 如果在指定的序列中没有找到值返回 True,否则返回 False。 | x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。 |
身份运算符
运算符 | 描述 | 实例 |
---|---|---|
is | is 是判断两个标识符是不是引用自一个对象 | x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False |
is not | is not 是判断两个标识符是不是引用自不同对象 | x is not y , 类似 id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False。 |
运算符优先级
运算符 | 描述 |
---|---|
** | 指数 (最高优先级) |
~ + - | 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@) |
* / % // | 乘,除,取模和取整除 |
+ - | 加法减法 |
>> << | 右移,左移运算符 |
& | 位 'AND' |
^ | | 位运算符 |
<= < > >= | 比较运算符 |
<> == != | 等于运算符 |
= %= /= //= -= += *= **= | 赋值运算符 |
is is not | 身份运算符 |
in not in | 成员运算符 |
not or and | 逻辑运算符 |
2、数据类型
数值类型
Python 数字数据类型用于存储数值。
数据类型是不允许改变的,这就意味着如果改变数字数据类型得值,将重新分配内存空间。
Python 支持四种不同的数值类型:
- 整型(Int) - 通常被称为是整型或整数,是正或负整数,不带小数点。
- 长整型(long integers) - 无限大小的整数,整数最后是一个大写或小写的L。
- 浮点型(floating point real values) - 浮点型由整数部分与小数部分组成,浮点型也可以使用科学计数法表示(2.5e2 = 2.5 x 102 = 250)
- 复数( (complex numbers)) - 复数的虚部以字母J 或 j结尾 。如:2+3i
变量赋值时数字对象将被创建:
>>> var1 = 10
>>> print(var1)
10
>>> type(var1)
<class 'int'>
>>> var2 = 10.0
>>> print(var2)
10.0
>>> type(var2)
<class 'float'>
一些数值类型的实例:
int | long | float | complex |
---|---|---|---|
10 | 51924361L | 0.0 | 3.14j |
100 | -0x19323L | 15.20 | 45.j |
-786 | 0122L | -21.9 | 9.322e-36j |
080 | 0xDEFABCECBDAECBFBAEl | 32.3+e18 | .876j |
-0490 | 535633629843L | -90. | -.6545+0J |
-0x260 | -052318172735L | -32.54e100 | 3e+26J |
0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
str类型
Python 字符串是 Python 中最常用的数据类型。我们可以使用引号(单引号、双引号、三引号)来创建字符串。如下
>>> str1 = 'this is a string'
>>> print(str1)
this is a string
>>> type(str1)
<class 'str'>
>>> str2 = "this is a string"
>>> print(str2)
this is a string
>>> type(str2)
<class 'str'>
>>> str3 = '''this is a string'''
>>> print(str3)
this is a string
>>> type(str3)
<class 'str'>
- 三引号只那个是三个三引号或三个双引号,单引号和双引号不可以混合
- type()是一个函数,用来查看变量的类型
Python不支持单字符类型,单字符也在Python也是作为一个字符串使用。 Python访问子字符串,可以使用方括号来截取字符串,如下实例:
>>> str1 = 'this is a string'
>>> print(str1[0])
t
通过字符串的index索引值访问,0表示第一个;
字符串中常用的方法
- find
>>> str1 = 'this is a string'
>>> str1.find("t")
0
>>> str1.find("z")
-1
find方法是在查找字符串中的字符,如果存在就返回该字符的索引;如果不存在则返回-1;
- replace
>>> str1 = 'this is a string'
>>> str1.replace("this","THIS")
'THIS is a string'
replace方法是替换字符串,然后返回新的字符串;
- split
>>> str1 = 'this is a string'
>>> str1.split(" ")
['this', 'is', 'a', 'string']
split方法用来分割字符串,可以指定分割的元素;
- join
>>> str2 = "hello"
>>> " ".join(str2)
'h e l l o'
(string.join(seq))join方法是以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串。
- strip
>>> str2 = " hello python "
>>> str2.strip()
'hello python'
strip()方法是去除字符串前后的空格;
- format
>>> name = "tom"
>>> age = 20
>>> print("my name is {0},{1} year old this year".format(name,age))
my name is tom,20 year old this year
format方法是对字符串进行格式化操作。
- startswith
>>> str1 = "this is a string"
>>> str1.startswith("this")
True
>>> str1.startswith("is")
False
startswith方法是判断字符串是否以指定的字符开头;是则返回 True,否则返回 False;
- endswith
>>> str1 = "this is a string"
>>> str1.endswith("string")
True
endswith方法是判断字符串是否以指定的字符结尾;是则返回 True,否则返回 False
list类型
序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。 Python有6个序列的内置类型,但最常见的是列表和元组。
序列都可以进行的操作包括索引,切片,加,乘,检查成员。
此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法。
列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。
列表的数据项不需要具有相同的类型。
- 创建列表
>>> list1 = ["hello", "python", 100,"ab"]
>>> print(list1)
['hello', 'python', 100, 'ab']
>>> type(list1)
<class 'list'>
列表的常用方法
- append
>>> list1 = ["hello", "python", 100,"ab"]
>>> list1.append("shell")
>>> list1
['hello', 'python', 100, 'ab', 'shell']
append方法是向列表的最后添加元素;然后返回一个新的列表
- pop
>>> list1
['hello', 'python', 100, 'ab', 'shell']
>>> list1.pop()
'shell'
>>> list1
['hello', 'python', 100, 'ab']
pop方法是从列表的最后删除元素,并且返回该删除的元素;
- index
>>> list1
['hello', 'python', 100, 'ab']
>>> list1.index("python")
1
lidex方法返回列表元素中的索引值;
- insert
>>> list1
['hello', 'python', 100, 'ab']
>>> list1.insert(0,"shell")
>>> list1
['shell', 'hello', 'python', 100, 'ab']
insert方法是在指定索引前面插入元素
- remove
>>> list1
['bash', 'shell', 'hello', 'python', 100, 'bash', 'ab']
>>> list1.remove("bash")
>>> list1
['shell', 'hello', 'python', 100, 'bash', 'ab']
>>>
remove方法移除列表中某个值的第一个匹配项
- reverse
>>> list1
['shell', 'hello', 'python', 100, 'bash', 'ab']
>>> list1.reverse()
>>> list1
['ab', 'bash', 100, 'python', 'hello', 'shell']
reverse方法是反向列表中元素;
- sort
>>> list1
['hello', 'python', 'shell', 'bash', 'ab']
>>> list1.sort()
>>> list1
['ab', 'bash', 'hello', 'python', 'shell']
sort方法是对列表元素进行排序,列表中的元素需要具有相同的数据类型,否则会出现异常;
tuple类型
Python的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号,列表使用方括号。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
- 创建元组
>>> tup1 = (1,"a",2,"b")
>>> print(tup1)
(1, 'a', 2, 'b')
>>> type(tup1)
<class 'tuple'>
元组使用(),元组中只包含一个元素时,需要在元素后面添加逗号;元组中可以包含数字和字符;
元组常用的方法
- count
>>> tup2 = ("a","b","a","ab","b")
>>> tup2.count("a")
2
count是用来统计元组中元素的数量;
- index
>>> tup2 = ("a","b","a","ab","b")
>>> tup2.index("ab")
3
index是用来返回元组中元素的索引值
访问元组中的元素
>>> tup2 = ("a","b","a","ab","b")
>>> tup2[3]
'ab'
访问元组中的元素,通过索引获取
元组分片
>>> tup2 = ("a","b","a","ab","b")
>>> tup2[3:]
('ab', 'b')
和访问元组中的元素类似,使用冒号设定一个索引范围;
dict类型
字典是另一种可变容器模型,且可存储任意类型对象。
字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:
d = {key1 : value1, key2 : value2 }
键必须是唯一的,但值则不必。
值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
字典的创建
- 方法1
>>> dict1 = dict(a=1,b=2)
>>> print(dict1)
{'a': 1, 'b': 2}
>>> type(dict1)
<class 'dict'>
- 方法2
>>> dict2 = {"name":"tom","age":20}
>>> type(dict2)
<class 'dict'>
>>> print(dict2)
{'name': 'tom', 'age': 20}
- 方法3
>>> dict3 = dict([("name","tom"),("age",20)])
>>> print(dict3)
{'name': 'tom', 'age': 20}
>>> type(dict3)
<class 'dict'>
字典常用方法
- get
>>> print(dict2)
{'name': 'tom', 'age': 20}
>>> dict2.get("name")
'tom'
get方法是用来获取键值对应的值;
- setdefault
>>> print(dict2)
{'name': 'tom', 'age': 20}
>>> dict2.setdefault("high",180)
180
>>> print(dict2)
{'name': 'tom', 'age': 20, 'high': 180}
如果key有不存在,设置一个默认值v,并返回v;如果k存在,返回k所对应的value
- keys()
>>> print(dict2)
{'name': 'tom', 'age': 20, 'high': 180}
>>> dict2.keys()
dict_keys(['name', 'age', 'high'])
获得所有keys
- values
>>> print(dict2)
{'name': 'tom', 'age': 20, 'high': 180}
>>> dict2.values()
dict_values(['tom', 20, 180])
获取所有的value
- update
>>> dict1
{'a': 1, 'b': 2}
>>> dict2
{'name': 'tom', 'age': 20, 'high': 180}
>>> dict1.update(dict2)
>>> dict1
{'a': 1, 'b': 2, 'name': 'tom', 'age': 20, 'high': 180}
dict1.update(dict2)把字典dict2的键/值对更新到dict1里更新字典中的元素
- pop
>>> dict1
{'a': 1, 'b': 2, 'name': 'tom', 'age': 20, 'high': 180}
>>> dict1.pop("a")
1
>>> dict1
{'b': 2, 'name': 'tom', 'age': 20, 'high': 180}
删除k:v对应的元素
zip函数
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
>>> dict1 = dict(a=1,b=2)
>>> dict2 = dict(x=10,y=20)
>>> list(zip(dict1,dict2))
[('a', 'x'), ('b', 'y')]