第1章 Python基础-Python介绍&循环语句 练习题&作业

2023-05-26,,

1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型?

高级语言分为编译型与解释型两种,分别从执行速度、开发效率、跨平台性三个方面说它们的区别。
编译型语言因为执行的是机器码文件,所以执行速度快且不依赖解释器,但每次修改源代码都需要重新编译,所以导致开发效率低,不同的操作系统,调用的底层机器指令不同,所以跨平台性差。
解释型语言需要解释器边把源文件解释成机器指令边交给cpu执行,所以执行速度要比编译型慢很多,但是每次修改时立刻见效,所以开发效率很高,解释器已经做好了对不同操作系统的交互处理,天生跨平台。
C/C++/C#/Delphi/Go属于编译型,PHP/Java/JavaScript/Python/Perl/Ruby属于解释型。

2.执行 Python 脚本的两种方式?

(1).交互方式:启动python解释器,执行命令
(2).脚本方式:Python xxx.py 或者 chmod +x xxx.py && ./xxx.py

3.Python单行注释和多行注释分别用什么?

单行注释:#要注释内容
多行注释:"""要注释内容""" 或者'''要注释内容'''

4.声明变量注意事项有哪些?

(1).变量由数字、字母和下划线组成
(2).变量不能以数字开头
(3).变量不能使用Python关键字
(4).变量区分大小写
     模块名,包名 :小写字母, 单词之间用_分割。
类名:首字母大写。
全局变量: 大写字母, 单词之间用_分割。
普通变量: 小写字母, 单词之间用_分割。
函数: 小写字母, 单词之间用_分割。
实例变量: 以_开头,其他和普通变量一样 。
私有实例变量(外部访问会报错): 以__开头(2个下划线),其他和普通变量一样 。
专有变量: __开头,__结尾,一般为python的自有变量(不要以这种变量命名)。

5.如何查看变量在内存中的地址?

id(变量名)

6.写代码

a. 实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!

#!/usr/bin/env python
#-*- coding:utf-8 -*- import getpass
_username = "seven"
_password = "123" username = input("username:")
password = getpass.getpass("password:") if username == _username and password == _password:
print("hello,seven")
else:
print("error,input again")

b. 实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

#!/usr/bin/env python
#-*- coding:utf-8 -*- import getpass
_username = "seven"
_password = "123"
count = 0
while count < 3:
count += 1
username = input("username:")
password = getpass.getpass("password:") if username == _username and password == _password:
print("hello,seven")
break
else:
print("error,input again")
#!/usr/bin/env python
#-*- coding:utf-8 -*- import getpass
_username = 'seven'
_password = '123'
count = 0
def login():
username = input('username:')
password = getpass.getpass('password:')
return username,password
while count<3:
username,password = login()
if username == _username and password == _password:
print('hello,seven')
break
else:
count += 1
print ('error,input again')

c. 实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import getpass
_username = ['seven','alex']
_password = "123"
count = 0
while count < 3:
count += 1
username = input("用户名:")
# password = input("密码:")
password = getpass.getpass("密码:") if username in _username and password == _password:
print("登陆成功!")
break
else:
print("登陆失败!")

7.写代码

a. 使用while循环实现输出2-3+4-5+6...+100 的和

# 2+4+6...+100
# -3-5...-99 count = 1
sum = 0
while count < 100:
count += 1
if count % 2 == 0 :
sum += count
else:
sum -= count
print(sum)
sum = 0
for count in range(2,101):
# print(count)
if count % 2 == 0 :
sum += count
else:
sum -= count
print(sum)

b. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12

count = 0
while count <= 12:
if count == 6 or count == 10:
pass # 换成continue不行,因为会跳过本次循环,count不能+1,count永远==6,永远跳过本次循环。
else:
print(count)
count += 1
count = 0
while count < 12:
count += 1
if count == 6 or count == 10:
pass # 换成continue可以,因为虽然跳出了本次循环,但是下次循环的时候count可以+1。
else:
print(count)

c. 使用while 循环输出100-50,从大到小,如100,99,98...,到50时再从0循环输出到50,然后结束

count = 101
while count > 50:
count -= 1
print(count)
if count == 50:
count = 0
while count < 51:
print(count)
count += 1
break

d. 使用 while 循环实现输出 1-100 内的所有奇数

count = 0
while count < 100:
count += 1
if count % 2 == 1:
print(count)
for count in range(1,101,2):
print(count)

e. 使用 while 循环实现输出 1-100 内的所有偶数

count = 0
while count < 100:
count += 1
if count % 2 == 0:
print(count)
for count in range(2,101,2):
print(count)

8.现有如下两个变量,请简述 n1 和 n2 是什么关系?

n1 = 123456
n2 = n1

给数据123456起了另外一个别名n2,相当于n1和n2都指向该数据的内存地址

9.制作趣味模板程序(编程题)

需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意显示
如:敬爱可爱的xxx,最喜欢在xxx地方干xxx

#方法一:
name = input("请输入姓名:")
address = input("请输入地点:")
hobby = input("请输入爱好:") info = """
敬爱可爱的%s,最喜欢在%s地方干%s
"""% (name,address,hobby) print(info) #方法二:
name = input("请输入姓名:")
address = input("请输入地点:")
hobby = input("请输入爱好:") info = """
敬爱可爱的{0},最喜欢在{1}地方干{2}
""" print(info.format(name,address,hobby))

10.输入一年份,判断该年份是否是闰年并输出结果。(编程题)

注:凡符合下面两个条件之一的年份是闰年。 (1) 能被4整除但不能被100整除。 (2) 能被400整除。

year = int(input("Please input the year:"))
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
print(year,"is a leap year")
else:
print(year,"is not a leap year")

11.假设一年期定期利率为3.25%,计算一下需要过多少年,一万元的一年定期存款连本带息能翻番?(编程题)

money = 10000
years = 0
rate = 0.0325
while money <= 20000:
years += 1
money = money * (1+rate)
print(str(years)+"年以后,一万元的一年定期存款连本带息能翻番")

作业

编写登陆接口

基础需求:

让用户输入用户名密码
认证成功后显示欢迎信息
输错三次后退出程序

#!/usr/bin/env python3
# -*- encoding: utf8 -*-
# 输错用户名和输错密码的次数总共最多为3次
import getpass exit_flag = False
count = 0
while count < 3 and not exit_flag:
user = input('\n请输入用户名:')
if user != "wss":
count += 1
print("\n用户名错误")
else:
while count < 3 and not exit_flag:
pwd = getpass.getpass('\n请输入密码:')
# pwd = input('\n请输入密码:')
if pwd == "123":
print('\n欢迎登陆')
print('..........')
exit_flag = True
else:
count += 1
print('\n密码错误')
continue
if count >= 3: # 尝试次数大于等于3时锁定用户
if user == "":
print("\n您输入的错误次数过多,且用户为空")
elif user != "wss":
print("\n您输入的错误次数过多,且用户 %s 不存在" % user)
else:
print("\n您输入的错误次数过多")
#!/usr/bin/python3
#-*- coding:utf-8 -*-
# 输错用户名和输错密码的次数分别最多为3次
# import getpass _username = "wss"
_password = "123" count = 0
exit_flag = False
while count < 3 and not exit_flag:
count += 1
username = input("\nPlease input your username:")
if username == _username:
exit_flag = True # 当密码正确时,跳过第一层循环,不再询问用户名
count = 0
while count < 3:
count += 1
password = input("\nPlease input your password:")
# password = getpass.getpass("\nPlease input your password:")
if password == _password:
print("\nhello,%s" % username)
break # 密码正确,跳过第二层循环,不再询问密码
else:
print("\nYour password is wrong")
else:
print("\nYour username is wrong")
if count >= 3: # 尝试次数大于等于3时强制退出
print("\nYou try more than 3 times,be forced to quit")

升级需求:

可以支持多个用户登录 (提示,通过列表存多个账户信息)
用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)

#!/usr/bin/env python3
# -*- encoding: utf8 -*-
# 输错用户名和输错密码的次数总共最多为3次
import getpass user_list = {
"wss": "123",
"alex": "456",
"jay": "789"
} f = open("deny_user_list.txt", "a", encoding="utf-8") # 没有此文件时创建
f.close()
with open("deny_user_list.txt", "r", encoding="utf-8") as deny_user_list_file:
deny_user_list = deny_user_list_file.readlines() exit_flag = False
count = 0
while count < 3 and not exit_flag:
user = input('\n请输入用户名:')
if user not in user_list:
count += 1
print("\n用户名错误")
elif user + "\n" in deny_user_list:
print("\n用户已被锁定,请联系管理员解锁后重新尝试")
break
else:
while count < 3 and not exit_flag:
pwd = getpass.getpass('\n请输入密码:')
# pwd = input('\n请输入密码:')
if pwd == user_list[user]:
print('\n欢迎登陆')
print('..........')
exit_flag = True
else:
count += 1
print('\n密码错误')
continue
if count >= 3: # 尝试次数大于等于3时锁定用户
if user == "":
print("\n您输入的错误次数过多,且用户为空")
elif user not in user_list:
print("\n您输入的错误次数过多,且用户 %s 不存在" % user)
else:
with open("deny_user_list.txt", "a", encoding="utf-8") as deny_user_list_file:
if user + "\n" not in deny_user_list:
deny_user_list_file.write(user + "\n")
print("\n您输入的错误次数过多,%s 已经被锁定" % user)
#!/usr/bin/env python
# -*- coding:utf-8 -*- # 输错用户名和输错密码的次数分别最多为3次 # import getpass user_dict = dict([("wss", "123"), ("alex", "456"), ("jay", "789")]) # 存储用户信息
deny_user_list = [] # 初始化拒绝用户列表
f = open("deny_user_list.txt", "a", encoding="utf-8") # 没有此文件时创建
f.close() with open("deny_user_list.txt", "r", encoding="utf-8") as deny_user_list_txt:
for deny_user in deny_user_list_txt.readlines(): # 将文件内容转换为列表
deny_user = deny_user.strip() # 去掉换行符
deny_user_list.append(deny_user)
count = 0
exit_flag = False
while count < 3 and not exit_flag:
count += 1
username = input("\nPlease input your username:")
if username in user_dict and username not in deny_user_list:
exit_flag = True # 当用户名正确时,跳过第一层循环,不再询问用户名
count = 0
while count < 3:
count += 1
password = input("\nPlease input your password:")
# password = getpass.getpass("\nPlease input your password:")
if password == user_dict[username]:
print("\nhello,%s" % username)
break # 当密码正确时,跳过第二层循环,不再循环密码
else:
print("\nYour password is wrong")
elif username in deny_user_list:
print("\n%s is locked,please contact the administrator" % username)
break
else:
print("\nYour username is wrong")
if count >= 3: # 当尝试次数大于等于3时强制退出并锁定用户
if username not in user_dict or username == "":
print("\nYou try more than 3 times,be forced to quit")
else:
with open("deny_user_list.txt", "a", encoding="utf-8") as deny_user_list_file:
if username not in deny_user_list:
deny_user_list_file.write(username + "\n")
print("\nYou try more than 3 times,%s has been locked" % username)

第1章 Python基础-Python介绍&循环语句 练习题&作业的相关教程结束。

《第1章 Python基础-Python介绍&循环语句 练习题&作业.doc》

下载本文的Word格式文档,以方便收藏与打印。