#学习笔记
#每日练习和企业面试题
学习笔记
# Python表达式 # 由数字,运算符,数字分组符号(括号),标量等,求的结果的有意义排列的组合 # a = 10 # a # 3.14+a # (x+y)*z+100
# 数据类型和分类 # Numbers(数字)(int long float complex) # 布尔类型(True False) # String(字符串) # List(列表) # Tuple(元组) # Dictionary(字典)
# 格式化输出 # 占位符:%S 字符占位符,%d数字占位符 # %s可以接收字符串,也可以接收数字 # print('my is %s,age id %s'%('zhangsan',18)) # # %d只能接收数字 # print('my is %d,age id %d'%(18,18)) # print('my is %s,age id %d'%('zhangsan',18)) # # 报错: print('my is %s,age id%d'%('zhangsan','18')) # # # 接收用户输入 用户输入:会存储成字符串 # name = input('your name:') # age = input('your age:') # print('my is %s,age id %s'%(name,age))
# while循环 # 为什么使用循环 # 比如使用if else 能解决的问题 # 比如猜年龄 猜三次 # age_of_boy = 24 # # guess = int(input('猜测年龄为:') ) # if guess > age_of_boy : # print('猜的太大了大了') # elif guess < age_of_boy : # print('猜的太小了') # else: # print('猜对了') # guess = int(input('猜测年龄为:') ) # if guess > age_of_boy : # print('猜的太大了大了') # elif guess < age_of_boy : # print('猜的太小了') # else: # print('猜对了') # guess = int(input('猜测年龄为:') ) # if guess > age_of_boy : # print('猜的太大了大了') # elif guess < age_of_boy : # print('猜的太小了') # else: # print('猜对了')
# while循环 # while 条件: # 循环体 # 如果条件为真,则执行循环体,执行完毕后再次循环,如果条件为假,循环体不执行,结束循环
# 打印0-10 # count = 0 # while count <= 10: # print(count) # count += 1 # # # 打印 0-10 之间的偶数 # count = 0 # while count <= 10: # if count % 2 == 0: # print(count) # count += 1 # 打印0-10之间的奇数 # count = 0 # while count <= 10: # if count % 2 != 0: # print(count) # count += 1 # # 死循环 # import time # num = 0 # while True # print('count',num) # time.sleep(1) # num +=1 # 注意:死循环,需要手动停止 # while 1 == 1 # print('a')
# 练习:要求如下 # 1.循环验证用户输入的用户名和密码 # 2.认证通过后,运行用户重复执行命令 # 3.当用户输入命令为quit时,则退出这个程序 # name = 'aaa' # password = '123456' # while True: # inp_name = input('用户名:') # inp_pwd = input('密码:') # if inp_name == name and inp_pwd == password: # while True: # cmd = input('>>:') # if not cmd: # continue # if cmd == 'quit': # break # print('%s'%cmd) # else: # print('用户名或密码错误') # continue # break # # # 实现二 # name = 'aaa' # password = '123456' # tag = True # while tag: # inp_name = input('用户名:') # inp_pwd = input('密码:') # if inp_name == name and inp_pwd == password: # while tag: # cmd = input('>>:') # if not cmd: # continue # if cmd == 'quit': # tag = False # break # print('%s'%cmd) # else: # print('用户名或密码错误') # continue # break
# break 用于退出本层循环 # while True: # print('123') # break # print('456')
# continue 用于退出本层循环,进行下一次循环 # while True: # print('123') # continue # print('456')
# # 练习:1.从1数到10,到5的时候,就停止 # count = 1 # while count <= 10: # if count == 5: # break # else: # print(count) # count +=1 # # 练习:2.打印100以内的5个3的倍数的数字 # count = 1 # num = 0 # while count <= 100: # if count % 3 == 0: # print(count) # num += 1 # if num == 5: # break # count += 1 # # 练习:3.输出100到200之间不能被3整除的数字 # count = 100 # while count >= 100 and count <= 200: # if count % 3 != 0: # print(count) # count += 1 # # 练习:4.使用while循环,输出1.2.3.4.5.6.8.9.10 # count = 1 # while count <=10: # if count != 7 : # print(count) # count += 1 # 练习:5.输出1-100以内所有的奇数 # count = 1 # while count <= 100: # if count % 2 != 0: # print(count) # count += 1 # 练习:6.计算100以内所有正整数的和 # count = 1 # num = 0 # while count <= 100: # num = num + count # count += 1 # print(num) # 练习:7.求1-2+3-4+5-6....+99-100的和
# while...else: # while循环正常执行,中间没有break终止的话,就会执行else后面的语句 # 如果执行过程中被break终止了,就不执行else的语句
count = 0 while count <= 5: count +=1 if count == 3: break print('llop',count) else: print('循环正常执行结束') print('------------out---------')
''' # while循环课下练习: # 练习一:用户登录(三次机会重试) # 允许用户多尝试3次,3次都没正确的情况下,就直接退出,如果对了,打印恭喜信息并退出 # 初始化用户名密码 # name = 'aaa' # password ='123456' # # 接收用户键盘输入信息 # i = 1 # while i <= 3: # user_name = input('请输入用户名:') # user_password = input('请输入密码:') # if user_name == name and user_password == password: # print('信息正确,登录成功!') # else: # print('用户名或密码错误,请重试') # i += 1 # 练习二:猜年龄游戏 # 要求:允许用户最多尝试3次 # 每尝试3次后,如果还没猜到,就问用户还想继续玩吗?如果回答Y或y,就让其 # 猜3次,以此往复,如果回答N或n,就退出程序 # 如果猜对了,就直接退 # i = 0 # age = 24 # while True: # guess_age = int(input("请输出猜测的年龄:")) # if guess_age > age: # print('猜大了') # elif guess_age < age: # print('猜小了') # else: # print('恭喜你,猜对啦!') # break # i += 1 # if i % 3 ==0: # p = input('还想继续玩吗?') # if p == 'N' or p == 'n': # print('游戏结束!') # break # elif p == 'Y' or p == 'y': # i = 0 # 练习三:使用while循环,打印正方形* # j = 0 # while j <= 4: # i = 0 # while i <= 4: # print('*',end=' ') # i += 1 # print() # j += 1 # # 练习四:使用for循环,完成猜年龄游戏 # def game(num,g): # for i in range(3): # if num == g : # print('恭喜你答对了',g) # break # elif num > g: # print('猜大了') # elif num < g: # print('猜小了') # if i != 2 : # num = int(input('请输入猜测年龄:')) # if i == 2 : # choice = input('是否要继续作答Y/N:') # # choice = int(choice) # if choice == 'Y' or choice == 'y': # num = int(input('请输入猜测年龄:')) # game(num,g) # elif choice == 'N' or choice == 'n': # print('再见,欢迎下次再玩') # break # # # if __name__ == '__main__': # g = 24 # num = int(input('请输入猜测年龄:')) # game(num,g) '''
# while 循环嵌套,就是一个while循环嵌套一个while的写法 # j = 0 # while j < 3: # i = 0 # while i < 3: # print('去买菜') # i += 1 # print("刷碗") # print('---家务干完了----') # j += 1
# Python pass语句 # 是空语句,不做任何事情,一般用于占位 # for i in 'python': # if i == 'h': # pass # print('这是pass块') # print('当前字符是:',i) # print('结束')
# for循环 # for 临时变量 in 序列 # 重复执行代码1... # str = 'python' # for i in str: # print(i)
# # 结合break # str1 = 'zhangsan' # for i in str1: # if i == 'a': # print('遇到a了') # break # print(i)
# 结合continue # str2 = 'zhangsan' # for i in str2: # if i == 'a': # print('遇到a不打印') # continue # print(i)
# while...else for ....else # 女朋友生气了,5次道歉的机会 # i = 0 # while i < 5: # print('我错了,你别生气了') # i += 1 # print('女朋友原谅了...') # i = 0 # while i < 5: # print('我错了,你别生气了') # i += 1 # else: # print('女朋友原谅了,很开心...') # 更生气了,不打算原谅 # i = 0 # while i < 5: # if i == 3: # print('不真诚...') # i += 1 # continue # print('我知道我错哪了') # i += 1 # else: # print('女朋友原谅了,很开心.....') # for 临时变量 in 序列: # 重复执行的代码... # else: # 循环正常结束后要执行的代码... # str1 = 'python' # for i in str1: # print(i) # else: # print('循环正常结束之后执行的代码') # break 不执行else缩进的代码 # str1 = 'python' # for i in str1: # if i == 't': # print('遇到t了') # break # print(i) # else: # print('循环正常结束之后执行的代码') # continue 退出当前循环继续执行循环,执行完循环后,执行else后的代码 # str1 = 'python' # for i in str1: # if i == 't': # print('遇到t了') # continue # print(i) # else: # print('循环正常结束之后执行的代码')
每日练习题和企业面试题
# 题目1 # 题干 # 使用while循环计算1~100的累加和(包含1和100) # 训练目标 # whlie循环的基本使用 sum=0 count=1 while count<=100: sum=sum+count count=count+1 print(sum) # 题干 # 使用while嵌套循环打印如下图形 # * # * * # * * * # * * * * # * * * * * # 训练目标 # while嵌套循环的使用 i = 0 while i <= 5: j = 1 while j <= i: print('*',end=' ') j=j+1 i = i+1 print() # 题干 # 编写代码模拟用户登陆。要求:用户名为 python,密码 123456,如果输入正确,打印“欢迎光临”,程序结束,如果输入错误,提示用户输入错误并重新输入 # 训练目标 # while中的break的使用 name = 'python' password = '123456' while True: user_name = input('请输入用户名:') user_password = input('请输入密码:') if user_name == name and user_password == password: print('欢迎光临') break else: print('输入错误请从新输入') # 题干 # 设计“过7游戏”的程序, 打印出1-100之间除了7和7的倍数之外的所有数字,如果遇见7和7的倍数则打印“哈~”跳过本次循环。 # 训练目标 # while中的continue的使用 count = 0 while count <= 100: count=count+1 if count % 7 == 0 or count % 10 == 7: print('哈~') continue print(count) # 题干 # 请用户输入一个数,使用while计算是否为素数 # 训练目标 # while...else的使用 # i = 1 number = int(input('请输入一个数字:')) k = 0 while i<= number: if number % i ==0: k=k+1 i=i+1 if k>=3 or k == 1: print(f'{number}不是素数') else: print(f'{number}是素数') # 题干 # 要求用户输入一个字符串,遍历当前字符串并打印,如果遇见“q”,则跳出循环。如果遇见“ ”(空格)则跳过当前输出。 # 训练目标 # for循环的基本使用 # break的作用 # continue的作用 str = input('请输入一个字符串:') for i in str: if i == 'q': break elif i == ' ': continue print(i) # 题干 # 使用for循环计算用户输入值的累加和 # 训练目标 # range()的使用 sum = 0 number = int(input('请输入一个数字:')) for i in range(number+1): sum = sum+i print(sum) # 题干 # 请用户输入一个数,使用for循环计算是否为素数 # 训练目标 # for...else是使用 num = int(input("请输入一个数字:")) for i in range(2,num): if num % i == 0 and num != i: print(f"{num},不是素数") break else: print(f"{num},是素数") # 题干 # 分别使用for循环和while循环,求100~200的所有素数 # 训练目标 # 循环嵌套的使用 # if条件判断 # while...else for...else的使用场景 for num in range(100,201): for i in range(2,num): if num % i == 0 and num != i: break else: print(num) i = 100 while i <= 200: j = 1 k = 0 while j <= i: if i % j == 0 : k += 1 j += 1 if k == 2: print(i) i += 1 # # 1. 阅读下面的代码,写出A0,A1至An的最终值。 # # \1. A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5))) # A0={'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4} # \2. A1 = range(10) # A1=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # \3. A2 = [i for i in A1 if i in A0] # A2 = [] # \4. A3 = [A0[s] for s in A0] # A3=[1,3,2,5,4] # \5. A4 = [i for i in A1 if i in A3] # A4=[1,2,3,4,5] # \6. A5 = {i:i*i for i in A1} # A5={0:0,1:1,2:4,3:9,4:16,5:25,6:36,7:49,8:64,9:81} # \7. A6 = [[i,i*i] for i in A1] # A6=[[0,0],[1,1],[2,4],[3,9],[4,16],[5,25],[6,36],[7,49],[8.64],[9,81]] # 2. range和xrange的区别? # rang返回的是一个列表,而xrang是一个生成器,在使用很长的列表时,xrang性能更好 # 3. 考虑以下Python代码,如果运行结束,命令行中的运行结果是什么? # # \1. l = [] # # \2. for i in xrange(10): # # \3. l.append({‘num’:i}) # # \4. print l # [{'num':0},{'num':1},{'num':2},{'num':3},{'num':4},{'num':5},{'num':6},{'num':7},{'num':8},{'num':9}] # 再考虑以下代码,运行结束后的结果是什么? # \1. l = [] # # \2. a = {‘num’:0} # # \3. for i in xrange(10): # # \4. a[‘num’] = i # # \5. l.append(a) # # \6. print l # [{'num':9},{'num':9},{'num':9},{'num':9},{'num':9},{'num':9},{'num':9},{'num':9},{'num':9},{'num':9}] # 以上两段代码的运行结果是否相同,如果不相同,原因是什么? # 第二段代码中,字典是可变对象,l.append(a)相当于把a的引用传入到列表中,所以当修改a[‘num’]的值时,l中的值也会跟着改变,相当于浅拷贝。 # 4. 以下Python程序的输出? # \1. for i in range(5,0,-1): # # \2. print(i) # 5 4 3 2 1