python数字类型及运算_python数据类型分类以及运算类型

------------恢复内容开始------------

一、python数据类型

目录:

1.数字(整数、小数)

2.字符串(单引号、双引号、三引号)

3.元组  #元素确定之后不能修改

4.列表 #元素可以修改

5.集合 #不讲顺序,得到的结果没有重复元素、可以用于去重

6.字典

二、python运算类型

目录:

1.数学运算(+ - * / ** // % )

2.赋值运算

3.逻辑运算(and or not)

4.比较运算(> < != >= <= ==)

5.关系运算(in not in is not is)

6.位运算(&(与) |(或) ^(异或) ~(求反) >>(左移) <<(右移) )

一、python数据类型(数字、字符串、元组、列表、集合、字典)

代码如下:

copycode.gif

1 #数字

2 print("整数",1)

3 print("小数",1.2)

4 print("整数的类型:",type(1))

5 print("小数的类型:",type(1.2))

6 #字符串

7 print("单引号字符串:",type('aa'))

8 print("双引号字符串:",type("aa"))

9 print("三引号字符串:",type('''aa'''))

10 #元组

11 print("元组:",(1,2,3))

12 print("元组的类型:",type((1,2,3)))

13 #列表

14 print("列表:",[1,2,3])

15 print("列表的类型:",type([1,2,3]))

16 #集合

17 print("集合:",{1,2,3})

18 print("集合的类型:",type({1,2,3}))

19 #字典

20 print("字典:",{1:"a",2:"b",3:"c"})

21 print("字典的类型:",type({1:"a",2:"b",3:"c"}))

copycode.gif

运行如下:

1592114-20190122153841479-2116339129.png

二、运算类型

1.数学运算(+ - * / ** // % )

代码如下:

copycode.gif

1 # + - * / ** // %

2 #加

3 print("1+2 = ",1+2)

4 #减

5 print("3-2 = ",3-2)

6 #乘

7 print("3*2 = ",3*2)

8 #除

9 print("3/2 = ",3/2) #不管是否能够整除,结果都为小数

10 print("6/3 =",6/3)

11 #幂

12 print("3**2 = ",3**2)

13 #整除

14 print("3//2 = ",3//2) #结果为1

15 #取余

16 print("5%3 = ",5%3) #结果为3

copycode.gif

运行效果如下:

1592114-20190122154414803-1504688839.png

2.赋值运算

代码如下:

copycode.gif

1 #+= -= *= /= //= %= **=

2 #+=

3 a=2

4 print("a的值:",a)

5 print("a+=3 ")

6 a+=3

7 print(a)

8 #-=

9 b=4

10 print("b的值:",b)

11 print("b-=1 ")

12 b-=1

13 print(b)

14 #*=

15 c=2

16 print("c的值:",c)

17 print("c*=2 ")

18 c*=2

19 print(c)

20 #/=

21 d=5

22 print("d的值:",d)

23 print("d/=2 ")

24 d/=2

25 print(d)

26 #//=

27 e=7

28 print("e的值:",e)

29 print("e//=2 ")

30 e//=2

31 print(e)

32 #%=

33 f=7

34 print("f的值:",f)

35 print("f%=2 ")

36 f%=2

37 print(f)

38 #**=

39 g=3

40 print("g的值:",g)

41 print("g**=2 ")

42 g**=2

43 print(g)

copycode.gif

运行效果如下:

1592114-20190122154845477-812533726.png

3.逻辑运算(and or not)

代码如下:

copycode.gif

1 # and or not

2 #and

3 print("and运算:")

4 print("true and false:")

5 print(True and False)

6 print("true and true:")

7 print(True and True)

8 print("false and false:")

9 print(False and False)

10 #and 先判断第一个数是否为0,若为0,则结果为0;否则看第二个数,不管第二个数是多少,输出结果都是第二个数

11 print("1 and 2 的结果是:",1 and 2)

12 print("0 and 2 的结果是:",0 and 2)

13 #or

14 print("or 运算:")

15 print("true or false:")

16 print(True or False)

17 print("true or true:")

18 print(True or True)

19 print("false or false:")

20 print(False or False)

21 #or 先判断第一个数是否为0,不为0,则输出第一个数;若为0,则看第二个数,不管第二个数是多少,输出结果都是第二个数

22 print("1 or 2 的结果是:",1 or 2)

23 print("0 or 3 的结果是:",0 or 3)

24 print("2 or 0 的结果是:",2 or 0)

25 #not

26 print("not 运算:")

27 print("not false:")

28 print(not False)

29 print("not true:")

30 print(not True)

copycode.gif

运行效果如下:

1592114-20190122155242398-926269087.png

4.比较运算(> < >= <= == !=)

代码如下:

copycode.gif

1 #> < == >= <= !=

2 #>

3 print("2>1:")

4 print(2>1)

5 #<

6 print("2<3:")

7 print(2>3)

8 #==

9 a=2

10 b=2

11 print("a的值:",a)

12 print("b的值:",b)

13 print("a==b:")

14 print(a==b)

15 #>=

16 print("a>=b")

17 print(a>=b)

18 #<=

19 print("a<=b")

20 print(a<=b)

21 #!=

22 print("2!=3")

23 print(2!=3)

copycode.gif

运行效果如下:

1592114-20190122155545497-1967049836.png

5.关系运算(in not in is not is)

代码如下:

copycode.gif

1 #in not in is not is

2 print("in not in的用法:")

3 a=(1,2,3)

4 print(a)

5 print(1 in a)

6 print(4 not in a)

7 print("is not is在数字类型的用法:")

8 a=2

9 b=2

10 print("a的值为:",a)

11 print("b的值为:",b)

12 print(a is b)

13 print("is not is在列表类型的用法:")

14 c=[1,2,3]

15 d=[1,2,3]

16 print("c为:",c)

17 print("d为:",d)

18 print(c is d )

19 print(c is not d )

copycode.gif

运行效果如下:

1592114-20190122160129931-2020094838.png

6.位运算(&(与) |(或) ^(异或) ~(求反) >>(左移) <<(右移) )

注:

技巧:~求反(加负号再减1,也可以用补码计算)

a=10 二进制为:00001010

b=8 二进制为:00001000

&两个数的二进制位相"与"运算,都为1,结果为1

| 两个数的二进制位相"与"运算,只要有一个为1,结果为1

^两个数的二进制位相"与"运算,不同时为1,结果为1,同时为1或者0,结果为0

代码如下:

copycode.gif

1 #& | ^ ~ >> <<

2 a=10

3 print("请输一个数:",a)

4 b=8

5 print("请输一个数:",b)

6 c=a&b  #结果为8

7 d=a|b  #结果为10

8 e=a^b  #结果为2

9 f=~a  #结果为-11

10 g=a>>2  #右移2位,结果为2

11 h=a<<1  #左移1位,结果为20

12 print("a与b按位与运算:",c)

13 print("a与b按位或运算:",d)

14 print("a与b按位异或运算:",e)

15 print("a按位取反运算:",f)

16 print("a按位右移2位运算:",g)

17 print("a按位左移1位运算:",h)

copycode.gif

效果如下:

1592114-20190122162306871-1777051402.png

------------恢复内容结束------------

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值