python运算符小游戏(全局变量/异常处理);奇数偶数;猜年龄(while/break/continue/);while + continue + else vs while + break + else
输出三个数字中的最大值/最小值;倒三角形;99乘法表;
python运算符
True 真 正确的
False 假 错误的
num += 1 等价于 num = num + 1
num -= 1 等价于 num = num - 1
num *= 2 等价于 num = num * 2
num /= 2 等价于 num = num / 2
num //= 2 等价于 num = num // 2
num %= 2 等价于 num = num % 2
num
= 2 等价于 num = num 2
>>> 5/2
2.5
>>> 5//2 # 地板除/整除
2
>>> 9%2 # 取余
1
// 取整除 返回商的整数部分
9//2 输出结果 4 , 9.0//2.0 输出结果 4.09//2.0
4.0Python位运算符:
按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:
1. & 按位与运算符:参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0
2. l 按位或运算符:只要对应的二个二进位有一个为1时,结果位就为1。
3. ^ 按位异或运算符:当两对应的二进位相异时,结果为1
4. ~ 按位取反运算符:对数据的每个二进制位取反,即把1变为0,把0变为1。~x 类似于 -x-1
5. << 左移动运算符:运算数的各二进位全部左移若干位,由"<<"右边的数指定移动的位数,高位丢弃,低位补0。
6. >> 右移动运算符:把">>“左边的运算数的各二进位全部右移若干位,”>>"右边的数指定移动的位数
• # 下面是二进制运算
• = 0011 1100
• = 0000 1101
• &b = 0000 1100
• |b = 0011 1101
• ^b = 0011 0001
• ~a = 1100 0011
Python成员运算符
• in 如果在指定的序列中找到值返回 True,否则返回 False。
x 在 y 序列中 , 如果 x 在 y 序列中返回 True。
• not in 如果在指定的序列中没有找到值返回 True,否则返回 False。
x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。
Python身份运算符
• is is是判断两个标识符是不是引用自一个对象
x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 Falseis not
• is not 是判断两个标识符是不是引用自不同对象
x is not y , 类似 id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False
id() 函数用于获取对象内存地址
Python运算符优先级
• ** 指数 (最高优先级)
• ~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
• * / % // 乘,除,取模和取整除
• + - 加法减法
• >> << 右移,左移运算符
• & 位 ‘AND’
• ^ l 位运算符
• <= < > >= 比较运算符
• <> == != 等于运算符
• = %= /= //= -= += = *= 赋值运算符
• is is not 身份运算符
• in not in 成员运算符
• not or and 逻辑运算符
and 且,并且
只有两个条件全部为True(正确)的时候, 结果才会为True(正确)
条件1 and 条件2
5>3 and 6<2 True
or 或,或者
只要有一个条件为True,则结果为Ture,
5>3 or 6<2
真 或 假
not 不,雅蠛蝶
not 5>3 == False
not 5<3 == True
短路原则
对于and 如果前面的第一个条件为假,那么这个and前后两个条件组成的表达式 的计算结果就一定为假,第二个条件就不会被计算
对于or
如果前面的第一个条件为真,那么这个or前后两个条件组成的表达式 的计算结果就一定为真,第二个条件就不会被计算
True or True and False => True
前面为真 后面(包括and后面)就不计算了
not not True or True and False => True
同理
and or 无先后顺序,即从左往右算(遵循短路原则)
小游戏(全局变量/异常处理);奇数偶数;猜年龄(while/break/continue/);while + continue + else vs while + break + else
- 1,return # 可以让一个函数结束
- 2,break # 可以让一个循环结束
- 3,exit() # 可以让一个程序结束
global a_ # 定义全局变量
# 一直执行程序
while True:
a_ = input('>>') # 同级缩进,否则报错
# 异常处理
try:
a_ = int(a_)
except:
print('您输入非数字类型')
a_ = 0
b_ = 100
c_ = 1000
if b_ <= a_ <= c_:
print("True")
else:
print("False")
>>12
False
>>123
True
>>fgh
您输入非数字类型
False
>>
# 奇数偶数
num = 1
while num <= 100: # num<=100 等价于 True
# while num<=100: 等价于 while True:
if num % 2 == 0:
print(num)
num += 1
num = 1
while num <= 100:
if num % 2 == 1:
print(num)
num += 1
# 猜年龄
age = 50
flag = True
while flag:
user_input_age = int(input("Age is :"))
if user_input_age == age:
print("Yes")
flag = False
elif user_input_age > age:
print("Is bigger")
else:
print("Is smaller")
print("End")
# break # 终止
age = 50
while True:
user_input_age = int(input("Age is :"))
if user_input_age == age:
print("Yes")
break
elif user_input_age > age:
print("Is bigger")
else:
print("Is smaller")
print("End")
continue 继续
# continue
# 总共10次循环, 当执行continue,结束当次循环
num = 1
while num <= 10:
num += 1
if num == 3:
continue
print(num)
# continue
# 总共10次循环, 当执行continue,结束当次循环
num = 1
while num <= 10:
num += 1
if num == 3:
continue
print(num)
else:
print('This is else statement')
This is else statement
num = 1
while num <= 10:
num += 1
if num == 6:
break
print(num)
else:
print('This is else statement')
while + continue + else vs while + break + else
• else 在程序正常循环的情况下执行,例如continue,
• else 在程序被中断的情况下不执行,例如break 或者程序前面错误
回顶部
3
输出三个数字中的最大值/最小值;倒三角形;99乘法表;
- 输出三个数字中的最大值/最小值
num1 = int(input("Num1:"))
num2 = int(input("Num2:"))
num3 = int(input("Num3:"))
if num1 >= num2 >= num3:
print(num1) # num1最大
elif num1 >= num3 >= num2:
print(num1) # num1最大
elif num2 >= num1 >= num3:
print(num2) # num2最大
elif num2 >= num3 >= num1:
print(num2) # num2最大
elif num3 >= num2 >= num1:
print(num3) # num3最大
else:
print(num3)
num1 = int(input("Num1:"))
num2 = int(input("Num2:"))
num3 = int(input("Num3:"))
if 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)
- 嵌套
while 条件:
....
else:
....
statement 语句
while 条件1:
.....
while 条件2:
....
print("hello world.", end="__") # \n(linux) \r\n(Windows) \r 光标回到当前行最前面,\n 换行 \r(mac)
print("hello world.", end="__")
print("hello world.", end="__")
hello world.__hello world.__hello world.__
num1 = 0
while num1<=5:
print(num1,end="_")
num2 = 0
while num2<=7:
print(num2,end="-")
num2+=1
num1+=1
print() # print(end="\n")
0_0-1-2-3-4-5-6-7-
1_0-1-2-3-4-5-6-7-
2_0-1-2-3-4-5-6-7-
3_0-1-2-3-4-5-6-7-
4_0-1-2-3-4-5-6-7-
5_0-1-2-3-4-5-6-7-
height = int(input('高>>'))
width = int(input('宽>>'))
num_h = 1
while num_h <= height:
num_w = 1
while num_w <= width:
print('#', end='')
num_w += 1
print()
num_h += 1
高>>5
宽>>3
###
###
###
###
###
width = int(input("width:"))
num_width = 1
while num_width <= width:
print("#", end="")
num_width += 1
print()
num_width = 1
while num_width <= width:
print("#", end="")
num_width += 1
print()
num_width = 1
while num_width <= width:
print("#", end="")
num_width += 1
print()
num_width = 1
while num_width <= width:
print("#", end="")
num_width += 1
width:3
###
###
###
###
print("#", end="")
print("#", end="")
print("#", end="")
print("#", end="")
print()
####
num = 4
while num > 0:
print("#", end="")
num -= 1
print()
####
num2 = 4
while num2 > 0:
print('####')
num2 -= 1
####
####
####
####
height = 2
width = 2
num2 = height # 第一步: 赋值
while num2 > 0: # 第二步 :num2 == 2
num1 = width # 第三步: 赋值
while num1 > 0: # 第四步:num1==2 # 第七步:num1 = 1
print("#", end="") # 第五步: 不换行 打印一个# 第八步: 不换行 打印一个#
num1 -= 1 # 第六步: num1 = 1 第九步: num1 = 0
print() # 第十步 : 只是换行
num2 -= 1 # 第十一步 : num2=1
##
##
print("1*1=",1)
# "1*1=",1 == str(m)+"*"+str(n)+"=",1
m = 2
n = 2
print( str(m)+ "*" + str(n) + "=" , m*n )
num = 1
while num <= 10:
print(num)
num += 1
1
2
3
4
5
6
7
8
9
10
- 倒三角形
line = 5 # 第一步 : 赋值
while line > 0: # 第二步 line=5
tmp = line # 第三步 : tmp =5 tmp=4
while tmp > 0: # 第四步 : tmp =5 #第七步 tmp=4 #第十步: tmp=3 第十三步 tmp=2
print("*", end="") # 第五步 #第八步 #第十一步 #第十四步
tmp = tmp - 1 # 第六步 tmp = 4 # 第九步 tmp=3 # 第十二步 tmp=2 第十五步 tmp= 1
print()
line -= 1
*****
****
***
**
*
- 正99乘法表
first = 1
while first <= 9:
sec = 1
while sec <= first:
print(str(sec) + "*" + str(first) + "=" + str(sec * first), end="\t") # \t 制表符
sec += 1
print()
first += 1
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
- 倒99乘法表
first = 9
while first > 0:
sec = 1
while sec <= first:
print(str(sec) + "*" + str(first) + "=" + str(sec * first), end="\t") # \t 制表符
sec += 1
print()
first -= 1
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*4=4 2*4=8 3*4=12 4*4=16
1*3=3 2*3=6 3*3=9
1*2=2 2*2=4
1*1=1