打印乘法表
for循环
#99乘法
for i in range(1,10):
for j in range(1,i+1):
print('%d*%d=%d'%(i,j,i*j),end=' ')
j += 1
i += 1
print()
while循环
#99乘法
i = j = 1
while i <= 9:
j = 1
while j <= i:
print('%d*%d=%d'%(i,j,i*j),end=' ')
j += 1
i += 1
print()
趣味练习:100元,买五元一只公鸡,三元一只母鸡,一元三只小鸡,共买100只:
g = m = x = 0
while g <= 20:
m = 0
while m <= 33:
if g*5+m*3+(100-g-m)/3 == 100 :
print('公鸡=',g,'母鸡=',m,'小鸡=',100-g-m)
m += 1
g += 1
打印出以下图形
'''
*
**
***
****
*****
****
***
**
*
'''
i = -4
j = 1
while i<=9:
j=1
while j<=5-abs(i):
print('*',end=' ')
j += 1
print()
i += 1
'''
*
***
*****
*******
*********
'''
i = j = 1
while i <= 5:
j = 1
while j <= 5-i:
print(' ',end=' ')
j += 1
while j <= 4+i:
print('*', end=' ')
j += 1
print()
i += 1
1234任意三个数字组合情况,不允许有相同的情况
#方法一
num = 123
while num <=432:
b = int(num / 100)
s = int(num / 10)% 10
g = num % 100
if b != s and s != g and b != g and 1 <= b <= 4 and 1 <= s <= 4 and 1 <= g <= 4:
print(num)
num += 1
#法二
b = s = g = 1
while b <= 4:
s = 1
while s <= 4:
g = 1
while g <= 4:
if b != s and s != g and b != g:
print(b,s,g)
g += 1
s += 1
b += 1
#法三
'''
#位运算:二进制
#bit位 计算机最小数据单位 1bit为1或0
num1 = 11
num2 = 13
print(num1 & num2)#且(与)
print(num1 | num2)#或
print(num1 ^ num2)#异或
print(num1 >> 1)#右移
print(num1 << 1)#左移
'''
'''
1,2,3,4
00 01 10 11
000110=6
111001=57
'''
for num in range(6,58):
b = num >> 4
s = num >> 2 & 3
g = num & 3
if b ^ s and s ^ g and g ^ b:
print(b+1,s+1,g+1)