num += 1 等价于 num = num + 1
num -= 1 等价于 num = num - 1num *= 2 等价于 num = num * 2num /= 2 等价于 num = num / 2num //= 2 等价于 num = num // 2num %= 2 等价于 num = num % 2num **= 2 等价于 num = num ** 2
##输入3个数,比较数值大小并打印
#第一种
num1 = int(input("Num1:"))
num2 = int(input("Num2:"))num3 = int(input("Num3:"))max_num =0if num1>num2: max_num= num1 if max_num > num3: print("Max NUM is",max_num) else: print("Max NUM is",num3)else: max_num = num2 if max_num > num3: print("Max NUM is",max_num) else: print("Max NUM is",num3)
##第二种
num1=int(input("Num1:"))
num2=int(input("Num2:"))num3=int(input("Num3:"))a=num1-num2
b=num1-num3c=num2-num3if a>=0 and b>=0:
print("The MAX number is","num1:",num1)elif a<=0 and c>=0: print("The MAX number is","num2:",num2)else : print("The MAX number is","num3:",num3)if a<=0 and b<=0: print("The MIN number is","num1:",num1)elif a>=0 and c<=0: print("The MIN number is","num2:",num2)else : print("The MIN number is","num3:",num3)
##while循环 语句
格式:
while 条件:
命令
##while语句当条件为真时执行
'''age=50
flag=true ####--错误---,真值赋值首字母必须大写 flag=True
while flag=true: ####--错误---,while循环判断条件是判断真假,不用等式,应该为while flag:
user_age=int(input("Guess my age:"))
if user_age=age: ###---错误---判断中==表示等于
print ("YES")
flag=false ####--错误---,真值赋值首字母必须大写 ,flag=False
elif user_age<age:
print("Pls try bigger.")
else:
print("Pls try smaller.")
print("END")'''
####while语句 执行命令需要首行缩进,不能在写字本上写代码!!!!缩进符格式不一样,报错"invalid character indentifier"
num=1
while num<=10: num+=1 if num==11: ##while中加入if判断,if条件成立,continue 跳过后面的while循环语句.continue表示返回到while循环开始的地方. continue print(num)##输出结果2 3 4 5 6 7 8 9 10 没有11
###while......:
........
else:
.......
####表示如果while循环正常结束,则执行else后面的语句,continue不会破坏while循环.非正常结束为break或者报错
print("Hello world.",end='')
print("Hello world.",end='')
print("Hello world.",end='')
#####输出3行变为1行 end=''
##九九乘法表
a=1b=1while a<=9: c=a*b print(a,'x',b,'=',c,end=' ') b+=1 if b==10: print('') a+=1 b=a continue1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9
2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 8 x 8 = 64 8 x 9 = 72 9 x 9 = 81###错误示范
a=1
b=1while a<=9: c=a*b print(a,'x',b,'=',c,end=' ') b+=1 if b==9: print('') a+=1 b=a continue ########此程序在执行时,a=9以后,b不停累加,b>9,不会执行if嵌套,不执行a累加,则循环无限重复.1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8
2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 7 x 7 = 49 7 x 8 = 56 8 x 8 = 64 9 x 9 = 81 9 x 10 = 90 9 x 11 = 99 9 x 12 = 108 9 x 13 = 117 9 x 14 = 126 9 x 15 = 135 9 x 16 = 144 9 x 17 = 153 9 x 18 = 162 9 x 19 = 171 9 x 20 = 180 9 x 21 = 189 9 x 22 = 198 9 x 23 = 207 9 x 24 = 216 9 x 25 = 225 9 x 26 = 234 9 x 27 = 243 9 x 28 = 252 9 x 29 = 261 9 x 30 = 270 9 x 31 = 279 9 x 32Traceback (most recent call last): File "D:/WPy64-3720/ZZ/0524-3.py", line 6, in <module> print(a,'x',b,'=',c,end=' ')KeyboardInterrupt
###金字塔形
a=1
b=1while b<=9: c=a*b print(a,'*',b,'=',c,end='\t') a+=1 while a>b: print() b+=1 a=1