result = 0
m = 0
b = 3
while m <=99:#会加一个数字所以提前减去一个数 且 m是不是符合实际情况?
#m += 1
if m%2==1:
result += m
else:
result -= m
#m += 1
print(result)
i = 1
while i <6:
j = 0
while j
print('*',end='')
j+=1
i+=1
print('')
result = 0
m = 0
while m <10:#会加一个数字所以提前减去一个数 且 m是不是符合实际情况?
if m%2==1:
result += m
else:
result -= m
m += 1
print(result)
for i in range(1,6):
for j in range(0,i):
print('*',end='')
print("")
重点九数乘法表n种形状
for i in range(1,10):
for j in range(0,i+1):
当i=1 的时候 j取值范围为(0,2),j取值分别为 0,1
当i=2 的时候 j取值范围为(0,3),j取值分别为 0,1,2
当i=3 的时候 j取值范围为(0,4),j取值分别为 0,1,2,3
当i=4 的时候 j取值范围为(0,5),j取值分别为 0,1,2,3,4
当i=5 的时候 j取值范围为(0,6),j取值分别为 0,1,2,3,4,5,
当i=6 的时候 j取值范围为(0,7),j取值分别为 0,1,2,3,4,5,6,
当i=7 的时候 j取值范围为(0,8),j取值分别为 0,1,2,3,4,5,6,7,
当i=8 的时候 j取值范围为(0,9),j取值分别为 0,1,2,3,4,5,6,7,8,
当i=9 的时候 j取值范围为(0,10),j取值分别为0,1,2,3,4,5,6,7,8,9
加深理解
for i in range(1,i+1):
for j in range(1,i):
print('%d * %d = %d ' %(i,j,i*j),end='')
print("")
# print('%d * %d = %d '%(1,3,3))
for i in range(100,1001):
if (i%10)**3+(i//100)**3+(i//10%10)**3==i:
print(i)
for i in range(1,100,2):
i+=i
for j in range(0,100,2):
j+=j
s=i-j
print(s)
a = 0
for i in range(10000,100000):
if (i%10)==(i//10000) and (i//10%10)==(i//1000%10):
a+=1
print(a)
冒泡排序
小--大
相邻的两个元素进行比较大小,把较小的那个交换到前面的位置,大的放在后面,
lt=[2,3,6,9,-20,8,0]
for i in range(len(lt)-1):
for j in range(len(lt)-i-1):
if lt[j]
lt[j+1],lt[j]=lt[j],lt[j+1]
print(lt)
for i in range(1,10):
for j in range(i,10):
print('%d * %d = %d \t' %(i,j,i*j),end='')
print("")
lt = []
for i in range(1,11):
lt.append(i)
print(lt)
b = [i for i in range(1,11)]
print(b)
import time
a6 =[]
t0 = time.clock()
for i in range(1,2000):
a6.append(i)
print(time.clock()-t0)
t0 = time.clock()
b6= [i for i in range(1,20000)]
print(time.clock()-t0)
a = [i**2 for i in range(1,10)]
b = [j+1 for j in range(1,10)]
c = [n for n in range(1,10) if n%2==0]
d = [letter.upper() for letter in 'abcdefgjklmn'] #lowerlower upper
e={i:i+1 for i in range(4)}
f = {i:j for i,j in zip(range(1,6),'abcde')} #必须得看得懂字典遍历
print(e)
a *列表*推导式改成圆括号 b yield生成器信号 生成器 迭代器
lt1 = (i for i in range(1,5))
print(next(lt1))
def fib
a =fib(10)
print(next(a))
if a >1:
pass
for i in 'python':
if i == 't':
continue
print(i)
var =10
while var >0:
var -=1
if var = 5:
continue
print(var)
for letter in 'python'
if letter == 'h':
pass
print('我是pass,我就是詹哥位子')
print(letter) #generator
一 不能完全表达的例子
int(input('a','b'))
e = 0
while a='rongda' and b='123':
e+=1
if e<3:
print('登陆成功')
else:
e==3
break
print('限制登录')
二 很规范全面的例子
a = 'rongda'
b = 123
cc = 0
while cc <= 3:
name1 = input('请输入用户名: ')
password1 = int(input('请输入密码: '))
cc+=1
if name1==a and password1 ==b:
print('登陆成功')
break
else :
print('输入第 %d 次错误'%(cc))
#t=3-cc
if 3-cc==0: #t==0
print('输入超过最大次数')
break
# a = 0
# for i in range(1,5):
#
# for j in range(1, 5):
# for k in range(1, 5):
# i!j and j!=k
# print(i,j,k)
# i=1
# s=1
# while i<6:
# i=i+2 #12345
# s=2*s
# print(s)
# a = 0
# for i in range(1,8):
# print('*', end='\n')
# for j in range(0,i):
# print('*',end='\n')
for i in range(1,10):
for j in range(i,10):
print('%d * %d = %d ' % (i, j, i * j), end='')
print('')
#第一行的值变量 第二行的变量范围决定了宽度
i = 0
while(i<3):
username = input('输入用户名:')
passname=int(input('请输入密码:'))
if username == 'rongda' and passname ==123:
print('登陆成功')
else:
print('用户名或者密码不对重新输入:')
i+=1
print('您一输入超过最大次数请明天再来')
a = 'rongda'
b = 123
cc = 0
while cc <= 3:
name1 = input('请输入用户名: ')
password1 = int(input('请输入密码: '))
cc += 1
if name1 == a and password1 == b:
print('登陆成功')
break
else:
print('输入第 %d 次错误' % (cc))
# t=3-cc
if 3 - cc == 0: # t==0
print('输入超过最大次数')
break
a = 0
b = 0
while b <=99:
b += 1 #终于明白此位置的重要性和b= b<一一对应!
if b %2 ==0:
a-=b
else:
a+=b
#b += 1
print(a)
进阶多级for
for a in range(1,10):
for b in range(0,10):
for c in range(0, 10):
for d in range(0, 10):
for e in range(0, 10):
if a ==e and b ==d:
print("{}{}{}{}{}".format(a,b,c,d,e),end="")
print("")
# (-3,0)(-2,1) y=x+3 y=-x+3 y=-x-3 y=x-3
for y in range(3, -4, -1):
for x in range(-3, 4):
if y <= x+3 and y <= -x+3 and y >= -x-3 and y >= x-3:
print("*", end="")
else:
print(" ", end="")
print("")
for i in range(1,10):
for j in range(i,10):
#print("%d*%d=%d" %(i,j,i*j))
#print("{}*{}".format(i,j)=i*j) #常见错误 !!!
print('%d * %d = %d ' % (i, j, i * j), end='')
print('')