python全栈开放实践第三版第一章的练习题完成情况

练习题:

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

编译型:只须编译一次就可以把源代码编译成机器语言,后面的执行无须重新编译,直接使用之前的编译结果就可以;因此其执行的效率比较高。

解释型:源代码不能直接翻译成机器语言,而是先翻译成中间代码,再由解释器对中间代码进行解释运行

编译型语言有:C、C++、Pascal/Object Pascal(Delphi)

解释型语言有:Python、JavaScript、Shell、Ruby、MATLAB

2.执行python脚本的两种方式是什么?

第一种:交互式,在cmd中运行

第二种:命令行式,通过cmd输入python3文本

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

单行用#

多行用‘’‘或“”“框起来

4.bool值分别有什么?

true 和 false

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

变量名必须是大小写英文字母、数字或下划线的组合。

变量名不能用数字开头。

变量名对大小写敏感。

变量名不能是关键字,例如and、as、class等等。

变量名不能使用特殊符号,例如:!、@、#、$、% 等

变量在使用前必须对其赋值。

变量没有明显的变量声明,而且类型不是固定的。

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

在python中可以用id()函数获取对象的内存地址。

object=1+2
print(id(object))
7.写代码
1.实现用户输入用户名和密码,当用户名为seven且密码为123是,显示登陆成功,否则登陆失败!
usnm = "seven"
pswd=123
username = input("please enter your nickname!")
password = int(input("please enter your password!"))
if username==usnm and password==pswd:

        print("ID comfirmed welcome!")
else:
        print("login fault")

2.实现用户输入用户名和密码,当用户名为seven且密码为123是,显示登陆成功,否则登陆失败,失败时允许重复输入三次
i=0
usnm = "seven"
pswd=123
i=0
while i<=2:
 username = input("please enter your nickname!")
 password = input("please enter your password!")
 if username==usnm and password==pswd:
     print("ID comfirmed welcome!")
     i=2
     break
 else:
        print("login fault")
        i+=1
        print("you can try",3-i,"times")


3.实现用户输入用户名和密码,当用户名为 seven 或 alex 且密码为123是,显示登陆成功,否则登陆失败,失败时允许重复输入三次
usnm1 = "seven"
usnm2 = "alex"
pswd = 123
i = 0
count=2
while i <=count :
 username = input("please enter your nickname!")
 password = int(input("please enter your password!"))
 if username == usnm1 and password == pswd:
     print("ID comfirmed welcome!", usnm1)
     i =4
     break
 elif username == usnm2 and password == pswd:
     print("ID comfirmed welcome!", usnm2)
     i = 4
     break
 else:
     print("login fault")
     i+=1
     print("you can try",count+1-i,"times")

8.写代码
1.使用while循环实现输出2-3+4-5+6.....+100的和
num=2
total=0

while num<=100:
        print(num)
        if num%2==0:
          total+=num

        if num%2==1:
          total-=num
        num+=1

print("sum=",total)
2.使用while循环实现输出1,2,3,4,5,7,8,9,11,12
i=1
while i<=12:
    i+=1
    if i == 6 or i == 10:
     continue
    print(i)
3.使用while循环实现输出1-100内的所有偶数
i=1
while i<=100:
    if i%2==0:
        print(i)
        i+=1
    else :
        i+=1    
4.使用while循环实现输出1-100内所有的奇数
i=1
while i<=100:
    if i%2==1:
     print(i)
     i+=1
    else:
     i+=1
9.现有如下两个变量,请简述n1和n2是什么关系?
n1=123456
n2=n1

答:n2的值是由n1赋予的,当前两个变量的值皆为123456

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

需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意显示

如:敬爱可爱的xxx,最喜欢在xxx地方干xxx

name=input("")
place=input("")
hobby=input("")
print("敬爱可爱的",name,"最喜欢在",place,"干",hobby)
11.输入一年份,判断该年份是否是闰年并输出结果。(编程题)

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

year=int(input("please enter a year"))
if year%4==0 and year%100!=0 or year%400==0:
    print("该年是闰年")
else:
    print("该年不是闰年")
12.假设一年期定期利率为3.25%,计算一下需要过多少年,一万元的一年定期存款连本带息能翻倍?(编程题)
principal=10000
year=0
while principal<=20000:
    principal*=1.0325
    year+=1
print(year)
原文地址:https://www.cnblogs.com/jimGraymane/p/11390654.html