Python运算符和数据类型

1、运算符

以下运算符中,a=100,b=200

算术运算符

运算符描述实例
+加 - 两个对象相加a + b 输出结果 300
-减 - 得到负数或是一个数减去另一个数a - b 输出结果 -100
*乘 - 两个数相乘或是返回一个被重复若干次的字符串a * b 输出结果 20000
/除 - x除以yb / 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

逻辑运算符

运算符描述实例
andx and y布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。
orx or y布尔"或" - 如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值。
notnot 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。

身份运算符

运算符描述实例
isis 是判断两个标识符是不是引用自一个对象x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False
is notis not 是判断两个标识符是不是引用自不同对象x is not y , 类似 id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False。

运算符优先级

运算符描述
**指数 (最高优先级)
~ + -按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % //乘,除,取模和取整除
+ -加法减法
>> <<右移,左移运算符
&位 'AND'
^ |位运算符
<= < > >=比较运算符
<> == !=等于运算符
= %= /= //= -= += *= **=赋值运算符
is is not身份运算符
in not in成员运算符
not or and逻辑运算符

2、数据类型

数值类型

Python 数字数据类型用于存储数值。

数据类型是不允许改变的,这就意味着如果改变数字数据类型得值,将重新分配内存空间。

Python 支持四种不同的数值类型:

  1. 整型(Int) - 通常被称为是整型或整数,是正或负整数,不带小数点。
  2. 长整型(long integers) - 无限大小的整数,整数最后是一个大写或小写的L。
  3. 浮点型(floating point real values) - 浮点型由整数部分与小数部分组成,浮点型也可以使用科学计数法表示(2.5e2 = 2.5 x 102 = 250)
  4. 复数( (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'>

一些数值类型的实例:

intlongfloatcomplex
1051924361L0.03.14j
100-0x19323L15.2045.j
-7860122L-21.99.322e-36j
0800xDEFABCECBDAECBFBAEl32.3+e18.876j
-0490535633629843L-90.-.6545+0J
-0x260-052318172735L-32.54e1003e+26J
0x69-4721885298529L70.2-E124.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')]

转载于:https://my.oschina.net/u/3805676/blog/1787713

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值