Analyzing circulation

if the judge

  • 1
if 判断条件1:
	执行语句1......
else:
    执行语句2……
  • Also it can be rewritten as a row
执行语句1...... if 判断语句1 else 执行语句2......

Here Insert Picture Description

  • 2
if 判断条件1:
    执行语句1……
elif 判断条件2:		#判断条件多,用多个elif
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
    执行语句4……
  • 3
a = 10

if a % 2 == 0:
    print('a是偶数')
    
else:
    print('a是奇数')
    
print('a = %d' % a)	#不论a对2取余等于什么,都执行
#a是偶数
 a = 10

and(&) or(|)

  • Judgment result is true, can be converted to 1, is false, 0 can be converted to
    • And is connected with, can be used multiplication operator, and that is really true = 1 (true), true and false = 0 (false), and false false = 0 (false), it can be seen there is a false end result is false, The first is a fake behind do not read, it must be false
    • Or connected with, the addition can be considered, i.e., true or real = 2> 0 (true), true or false = 1 (true), false or false = 0 (false), there can be seen the final result is a true true The first one is really behind do not read, it must be true

Example 1

a = 10
print(a)
if a > 0 and a % 2 == 0:	#1 * 1 = 1真
    print('a是大于0的偶数')
if a > 10 and a % 2 == 0:		#0 * 1 = 0假	#第一个为假,一定是假
    print('a是大于10的偶数')
else:	#if为假,执行else,即下行代码
    print('a不是大于10的偶数')
a = 9
print(a)
if a % 2 == 1 or a < 10:	#1 + 1 = 2>0真	#第一个为真,一定是真
    print('a小于10或者a是奇数')
if a % 2 == 0 or a < 10:	#0 + 1 = 1真  #有一个为真,一定是真
    print('a小于10或者a是偶数')
if a % 2 == 0 or a > 10:	#0 + 0 = 0假	#两个都假最终结果才为假
    print('a是偶数或者a大于10')
else:
    print('a既不是偶数又小于10')


#10
 a是大于0的偶数
 a不是大于10的偶数
 9
 a小于10或者a是奇数
 a小于10或者a是偶数
 a既不是偶数又小于10

Example 2

# 在不加括号时候, and优先级大于or
a = True | True & False
print(a)
# and中含0,返回0; 均为非0时,返回后一个值,
a = 5 and 7 and 6
"""
a = 5
a = 7
a = 6
"""
print(a)
# or中, 至少有一个非0时,返回第一个非0
a = 5 and 4 or 6 and 7
"""
a = (5 and 4) or (6 and 7)
a = 4 or 7
"""
print(a)

a = 4 or 7
print(a)

if not

  • if not a:
    • If the assignment is not a return value or None, following statement is executed
    • With a previously assigned, or can be used to determine whether the return value is null
a = ''
if not a:
    print('a不能为空或None')
else:
    print(a)
#a不能为空或None

cycle

  • Recycling ideas
    • The number of cycles (while the number of cycles determined by the conditions), where the design may not know how many times
    • What do recycle
    • How changes in variables

while

  • grammar
while 条件:
	条件满足时,做的事情1
	条件满足时,做的事情2
	......
  • From 1 to 100
i = 1
sum = 0
while i <= 100:
    sum += i
    i += 1
print(sum)
  • * Draw a rectangle with
y = 1
while y <= 10:  #为了输出十行
    x = 1
    while x <= 10:  #为了在一行输出十个星号
        print('*',end = '')
        x += 1
    print('')
    y += 1
print('矩形完成')

#**********
 **********
 **********
 **********
 **********
 **********
 **********
 **********
 **********
 **********
 矩形完成

for

  • grammar
for 临时变量 in 列表或字符串/元组/字典/set(集合)等:
	循环满足条件时执行的代码
else:
	循环不满足条件时执行的代码
  • Case
for i in 'xiaoge':
    print(i)
else:
    print('没有内容了')

Here Insert Picture Description

  • Case
for i in range(9):	#range(9)表示range(0,9),左闭右开,从[0,9)中依次取值
    print(i)

Multiplication table

while

i = 1
while i < 10:
    j = 1
    while j <= i:
        print('%d x %d = %d\t' % (j,i,i*j),end='')
        j += 1
    print('')
    i += 1

Here Insert Picture Description

for

for i in range(9):
	i += 1
	for j in range(1,i+1):
		print('%d x %d = %d\t' % (j,i,i * j),end = '')	#end = ''用于不换行
	print('\n')

Here Insert Picture Description

while True break continue

  • In the cycle, you may be forced to exit the loop early
age = int(input('请输入年龄:'))
i = 1
while True:
    if i == age:
        print('年龄为:%d' % age)
        break
    else:
        print('猜错了,继续猜')
    i += 1

Here Insert Picture Description

  • With this statement is the core idea is that if something goes wrong, you can continue to cycle
  • while True loop statement must have a break, otherwise it is infinite loop has been executed
  • continue to terminate the present cycle is, for the next cycle
    Here Insert Picture Description
i = 0
while i < 10:
    i += 1
    if i % 2 == 0:
        print('%d是偶数' % i)
        continue
    print('当前的i是:%d' % i)

else:   #i=10时,不满足while,执行else
    print('else表示不满足条件时调用的代码,这时i为%d'%i)

while and else may be used, similar effects and if else
Here Insert Picture Description

* Printing with inverted triangle

  • The first line, before the leftmost asterisk with no spaces, there is a second line, the third line has two ...
while True:
    i = int(input('请输入三角形行数(不超过5):'))
    if i > 5:
        print('输入的数不能大于5!请重新输入!')
        continue
    j = 1
    while i > 0:
        print(' ' * (j-1) + '* ' * i )
        j += 1
        i -= 1
    break

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/xiaogeldx/article/details/90489929