Python3 Study Day One 课后练习

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

  编译型:执行速度快,不依赖语言环境运行,跨平台性差。如C、C++、Delphi等

  解释类:执行速度慢,依赖解释器运行,跨平台性好。如Python、JavaScript 、Java、PHP、Shell等

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

  通过文本写入代码后保存为后缀.py的文件,并通过命令行的方式进行执行

  通过进入交互模式输入并执行,但是只能输入一行执行一行

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

  单行注释使用“#”

  多行注释使用“'''...'''”

4.布尔值分别有什么?

  布尔值分别为True和False

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

  变量名只能是字母、数字或下划线的任意组合

  变量名开头不得为数字

  部分关键词不得声明为变量,如‘and’,'as','break'等

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

  id(x)得到x变量的内存地址(10进制)

7.写代码

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

1 username = 'seven'
2 password = '123'
3 
4 usernames = input('Login:')
5 passwd = input('password:')
6 if usernames == username and passwd == password:
7     print('welcome,{}!'.format(usernames))
8 else:
9     print('User or password error')
View Code

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

 1 username = 'seven'
 2 password = '123'
 3 
 4 count = 0
 5 while count < 3:
 6     usernames = input('Login:')
 7     passwd = input('password:')
 8     if usernames == username and passwd == password:
 9         print('Welcome,{}!'.format(usernames))
10     else:
11         print('User or password error')
12     count += 1
View Code

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

 1 username = 'seven'
 2 username2 = 'alex'
 3 password = '123'
 4 
 5 count = 0
 6 while count < 3:
 7     usernames = input('Login:')
 8     passwd = input('password:')
 9     if usernames or username2 == username and passwd == password:
10         print('Welcome,{}!'.format(usernames))
11     else:
12         print('User or password error')
13     count += 1
View Code

8.写代码

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

1 i = 2
2 sum = 0
3 while i <= 100:
4     if i % 2 == 0:
5         sum += i
6     else:
7         sum -= i
8     i += 1
9 print('sum:',sum)
View Code

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

1 count = 1
2 while count <= 12:
3     if count != 6 and count != 10:
4         print(count)
5     count += 1
View Code

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

 1 count = 100
 2 num = 0
 3 
 4 while count >= 0:
 5     if count >= 50:
 6         print(count)
 7         count -= 1
 8         continue #当count循环到50时,结束本次循环并进行下一个循环
 9     if num <= 50:
10         print(num)
11         num += 1
View Code

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

1 count = 0
2 
3 while count <= 100:
4     if count % 2 != 0:
5         print(count)
6     count += 1
View Code

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

1 count = 1
2 
3 while count <= 100:
4     if count % 2 == 0:
5         print(count)
6     count += 1
View Code

9.现有如下两个变量,请简述n1和n2的关系

  n1 = 123456

  n2 = n1

  n1和n2共同指向同一个内存地址

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

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

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

1 content = input('请输入您的姓名:')
2 site = input('请输入您喜欢的地点:')
3 matter = input('请输入您喜欢做的事情:')
4 print('敬爱可爱的{},最喜欢在{}地方做{}'.format(content,site,matter))
View Code

11.输入一年份,判断该年份是否是闰年并输出结果。(编程题)
  注:凡符合下面两个条件之一的年份是闰年。 (1) 能被4整除但不能被100整除。 (2) 能被400整除。

1 year = int(input('请输入年份:'))
2 if year % 4 == 0 and year % 100 != 0:
3     print('{}年为闰年'.format(year))
4 elif year % 400 == 0:
5     print('{}年为大闰年'.format(year))
6 else:
7     print('{}为平年'.format(year))
View Code

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

1 money = 10000
2 
3 year = 0
4 while year >= 0:
5     if money < 20000:
6         money = money + money * 0.0325
7         year += 1
8         print(year,money)
View Code

13.一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

 1 height = 100
 2 count = 0
 3 meter = 0
 4 
 5 while count < 10:
 6     meter += height
 7     height /= 2
 8     meter += height
 9     count += 1
10     print('第{}次,总计经过{}米,反弹高度为{}'.format(count,meter,height))
View Code

猜你喜欢

转载自www.cnblogs.com/rlunepika/p/9089355.html