python运算和流程控制

python 运算和流程控制


python 运算(布尔值,自增,比较,逻辑)
python 流程控制
python 推导式
深度拷贝

python运算
	布尔值:
		True 	非0的数字,非空的字符串,列表,元素,字典
		False	0 "" () [] {} None
	自增运算:
		var int i 
		i = 0 ,i < 10
		i++
		i += 1
		#其他语言出现的循环

	逻辑运算
		布尔值可以用逻辑运算符and(且);or(or);not(非)来运算
			and(与),所有条件符合才为True
					>>> True and True
					True
					>>> True and False
					False
					>>> False and False
					False
			or(或)运算,只要其中有一个为True都为True
					>>> True or False
					True
					>>> True or True
					True
					>>> False or False
					False
			not(非)运算
					>>> not True
					False
					>>> not False
			
>>> username = 'shuai'
>>> password = '123'
>>> username == 'shuai' and password == '123'
True

					True
			空值;
				空值为None
				None不能理解为0;0是有意义的,None是一个特殊的空值
			变量;
				变量必须是大小写字母,数字和_的组合

自增运算
	其他语言的自增运算
		var int i 
			i = 0
			i++		等同后面的i = i + 1
		printf(i)

	在python中
		i = 10
		i += 1 		等同 i = i + 1
		i += 2 		等同 i = i + 2
		i -= 1 		等同 i = i - 1
		i *= 1 		等同 i = i * 1
		i /= 1 		等同 i = i / 1

比较(关系)运算符
	python中的比较运算符如下表
	
	== 
	检查两个操作数的值是否相等,如果是则条件 变为真。 
	如a=3,b=3则( a == b) 为 True. 
	
	!= 
	检查两个操作数的值是否相等,如果值不相等, 则条件变为真。 
	如a=1,b=3则(a != b) 为 True. 
	 
	<> 	只在python2中存在,python中已经取消了
	检查两个操作数的值是否相等,如果值不相等, 
	则条件变为真。Python和在Pascal等特有方式,java和c没有,在Python3中废弃了 
	如a=1,b=3则(a <> b) 为 True。 这个类似亍 != 运算符 
	
	> 
	检查左操作数的值是否大亍右操作数的值,如 果是,则条件成立。 
	如a=7,b=3则(a > b) 为 True. 
	
	< 
	检查左操作数的值是否小亍右操作数的值,如 果是,则条件成立。 
	如a=7,b=3则(a < b) 为 False. 
	
	>= 
	检查左操作数的值是否大亍戒等亍右操作数的 值,如果是,则条件成立。 
	如a=3,b=3则(a >= b) 为 True. 
	
	<= 
	检查左操作数的值是否小亍戒等亍右操作数的 值,如果是,则条件成立。 
	如a=3,b=3则(a <= b) 为 True. 

比较运算
	>>> 1 > 0
	True
	>>> 1 < 0
	False
	>>> 1 == 1.0
	True
	>>> 1 >= 1.0
	True
	>>> 1 <= 1.0
	True
	>>> 1 != 1.0
	False
	>>> 1 <> 1.0
	SyntaxError: invalid syntax
	>>> 1 is 1.0
	False
	>>> print(id(1),id(1.0))
	140728753611408 2998816717992
	>>> 1 is 1
	True

逻辑运算符
	and 
	x and y 
	布尔"不" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。 
	那如果 x 总为 False 呢? 
	
	or
	 x or y 
	布尔"或" - 如果 x 是 True,它返回 True,否 则它返回 y 的计算值。 
	那如果 x 总为 True 呢? 
	
	not
	 not x 
	布尔"非" - 如果 x 为 True,返回 False 。如 果 x 为 False,它返回 True。 
		
		>>> True or False and False
		True
			如果先算or就会输出:False,所以and > or
		>>> not False and False
		False
			如果先not就会输出True
		>>> not False or True
		True
			如果先or就会输出False
		>>> True or False
		True
		>>> False or True
		True
运算顺序 not  and  or	非且或

python 流程控制
		流程控制 个别的指令(或是陈述,子程序)运行或求值的顺序
		python代码正常情况下执行的顺序是从左到右,从上到下的,但也有不是这样的
		语句A--->>语句B----->>语句C

	if 判断语句
	for循环语句
	while 循环语句
	python是靠冒号和缩进来确定代码的归属的

if 判断语句
	执行顺序
	if  elif  else

			a = float(input('输入一个数字:'))
			if a == 1:
			    print('shuai')
			elif a == 5 :
			    print('ge')
			else:
			    print('%d is not 5 and 1'%(a))

			输入一个数字:10
			10 is not 5 and 1

if 判断要注意的点
	注意判断的范围,判断条件范围大的要放在下面
			a = int(input('输入:'))
			if a < 10:
			    print('%d 小于10'%(a))
			elif a < 5:
			    print('%d 小于5'%(a))
			else:
			    print('%d 大于10'%(a))	注意%d 大于10'  中间不要,  %(a)


			输入:8
			8 小于10

	区分if elif 和 if if 的区别

		判断两种可能,
			a = int(input('shuru:'))
			if a < 15:
			    print(int(a),'< 15')
			elif a < 20:
			    print(int(a),'< 20')
			else:
			    print(none)
			 	

			 	shuru:19
				19 < 20

		判断两次,先判断是否满足a ,再判断是否满足B,
			>>> 
			'''
			a = 10
			if a < 15:
			    print('a < 15')
			if a < 20:
			    print('a < 20')
			    
			a < 15
			a < 20
for循环语句
	其他语言
			for (var int i,i < 3,i++){
				print("nice")
			}			
	python 当中采用的是for in结构

	for in 
	#i是一个变量
	#‘abcd’是可迭代对象
	#将‘abcd’依次赋值给i每次赋值作为一次循环
	
			for i in 'abcd':
		    	print(i,end='\t')
		    	print('hello word')
				

				a	hello word
				b	hello word
				c	hello word
				d	hello word
				 
	嵌套循环
		外层循环一次
		内层循环一遍
		for i in 'abcd':
   			 print(i)
   			 for j in range(2):
        		print(j)
			

				a
				0
				1
				b
				0
				1
				c
				0
				1
				d
				0        		
打印乘法口诀表
		for i in range(10):
		    for m in range(1,i+1):
		        sum = i*m
		        if m < i:
		            print(m,'*',i,'=',sum,end='')
		        else:
		            print(m,'*',i,'=',sum)

while循环语句
	while循环,while循环和for循环不同,for循环是一个广度遍历,而while循环式一个深度遍历
	while循环,指当满足while的条件的时候,就一直循环执行while的语句块,直到不满足
	a = 0
	while a < 10:
	    print (a)
	    a += 1
0
1
2
3
4
5
6
7
8
9


a = 0
while a < 10:
    print (a)
    a += 1
else:
    print('shutdown')



0
1
2
3
4
5
6
7
8
9
shutdown
>>> 	 


死循环会阻碍程序的运行
死循环多数用于图形化,游戏等可视化的主循环
列如:
a = 0
while a < 100:
    print('down')


三种流程控制语句
	
	pass ,pass 
	写在任何缩进的语句块部分,只是占位,什么事情都不做,只是未来满足python的语法要求

for i in range(5):		#不加pass会报错,
    if i == 2:
        pass
    print(i)

0
1
2
3
4

	break 跳出上一层循环
for i in range(5):
    if i == 2:
        break
    print(i)


0
1	


	continue跳出本次循环

for i in range(5):		#直接跳过了2这个循环
    if i == 2:
        continue
    print(i)


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值