python 学习_基础语法__练习,,1. 简述编译型与解


1. 简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型;
   编译型语言在程序执行之前,需要有一个专门的编译过程,把源码编译成机器语言的文件
  解释型语言 不需要事先编译,直接将源码解释成机器码并立刻执行,所以只要某个平台提供解释器就可以运行
   编译型: c c++ c#
   解释性语言: Python php

2. 执行 Python 脚本的两种方式是什么
    1) ./run.py  直接调用Python脚本
2) Python run.py 调用Python解释器 来调用Python脚本


3. Python的单行注释 和多行注释分别用什么
    单行:   #
多行: """ """ ‘‘‘ ‘‘‘
4 布尔值分别有什么
布尔值有 True False
布尔值为False的有 [] () {} 0 False ""

5.声明变量需要注意事项有哪些
变量有全局变量和局部变量之分
变量名只能字母开头数字或下划线的任意组合
关键字不能为变量名

6 如何查看变量的内存地址
  a = 10  print (id(a))  # 结果 498775392

7写代码
a. 实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败;
user="seven"passwd = "123"username = (input("user: ")).strip()password = (input("passwd:")).strip()if user == username and passwd == password:    print( "登录成功")else:    print ("登录失败")

b. 实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次;
user="seven"passwd = "123"count = 0while count < 3:    username = (input("user: ")).strip()    password = (input("passwd:")).strip()    count += 1    if user == username and passwd == password:        print( "登录成功")        break    else:        print ("登录失败")



c. 实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次;
user=["seven","alex"]passwd = "123"count = 0while count < 3:    username = (input("user: ")).strip()    password = (input("passwd:")).strip()    count += 1    if username in  user and passwd == password:        print( "登录成功")        break    else:        print ("登录失败")

8. 写代码
a. 使用while循环实现输出2-3+4-5+6...+100 的和;
count = 2sum_1 = 0while count <= 100:    if count %2 ==0:        sum_1 += count    else:        sum_1 -= count    count +=1print (sum_1)
b. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12;
count = 0while count < 12:    count+=1    if count ==6:        pass    else:        print(count)

count = 0while count < 12:    count +=1    if count == 6:        continue    print(count)


c.使用while 循环输出100-50,从大到小,如100,99,98...,到50时再从0循环输出到50,然后结束;
test = 0count = 100while  count > 0:    print(count )    count -= 1    if  count ==50:        while test <count:            test +=1            print(test)        break


d. 使用while 循环实现输出 1-100 内的所有奇数;
count = 0while count <100:    count +=1    if count % 2==1:        print(count)


e. 使用 while 循环实现输出 1-100 内的所有偶数;
count = 0while count < 100:    count +=1    if count % 2 == 0:        print( count )

9. 简述 n 和n1 的关系
n = 123456
n1 = n

n = 123456n1 = nprint (n, n1)#结果 123456 123456n = 22222print (n, n1)# 结果: 22222 123456
n 指向 123456
n = n1 n 指向的是123456
当 n = 22222时, n 指向222222 n1 还指向123456

10. 制作趣味模板程序(编程题) 需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意显
  示: 如:敬爱可爱的xxx,最喜欢在xxx地方干xxx。

name = input("name: ")age = input("age:   ")hometown = input("hometown: ")love = input("love: ")msg = """------------------------info %s 的信息概要---------------------------name:               %sage:                %shometown:           %slove:               %s--------------------------------end------------------------------------"""%(name,name,age,hometown,love)print (msg)


结果:
name: yyage:   18hometown: mblove: girl------------------------info yy 的信息概要---------------------------name:               yyage:                18hometown:           mblove:               girl--------------------------------end------------------------------------



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

year = int(input ("year:    "))if year %4 ==0 and year %100 !=0:    print ("闰年",year)elif year %400 ==0:    print("闰年",year)

12. 假设一年期定期利率为3.25%,计算一下需要过多少年,一万元的一年定期存款连本带息能翻番?(编程题)
year = 0money = 10000rate = 0.0325while money <20000:    year +=1    money = money*(1+rate)print(year)

13. 打印出以下图形

*
**
***
****
*****
*****
****
***
**
*
count = 0while count < 5:    count +=1    print("*"*count)    if  count ==5:        while count >=1:            print("*"*count)            count -=1        break

14. 路飞决定根据销售额给员工发提成,提成为阶梯制,假设一个销售人员基本工资为3000元,每月业绩低于5万元,无提成;
  5万至10万,提成3%;
  10万至15万提成5%,15万-25万提成8%;
  25万至35万提成10%,35万以上,提成15%;
  从键盘获取用户当月业绩,计算其工资+提成的总额


memory = int(input("销售额:    "))salary= 3000if memory < 50000:    print (salary)elif memory >=50000 and memory <100000:    salary += memory*0.03elif memory >=100000 and memory <150000:    salary += memory*0.05elif memory >=150000 and memory <250000:    salary += memory*0.08elif memory >=250000 and memory <350000:    salary += memory*0.1else:    salary += memory * 0.15print(salary)

15. 北京地铁交通价格调整为:
6公里(含)内3元;6公里至12公里(含)4元;12公里至22公里(含)5元;22公里至32公里(含)6元;32公里以上部分, 每增加1元可乘坐20公里。

使用市政交通一卡通刷卡乘坐轨道交通,每自然月内每张卡支出累计满100元以后的乘次价格给予8折优惠;满150元以后的乘次给予5折优惠,假设每个月,小明都需要上20天班,每次上班需要来回1次,即每天需要乘坐2次同样路线的地铁,编写程序,从键盘获取距离,帮小明计算每月的总花费

import  math# 一趟的费用def termini(work_distance):    if work_distance <= 6:        memory = 3    elif work_distance > 6 and work_distance <=12:        memory = 4    elif work_distance > 12 and work_distance <=22:        memory = 5    elif work_distance > 22 and work_distance <=32:        memory = 6    else:        memory = math.ceil((work_distance -32)/20) +6    return  memory# 总费用def fare(memory,work_day):    sum_memory = memory * 2 * work_day    if sum_memory < 100:        sum_memory = 100    elif  sum_memory >= 100 and    sum_memory < 150:        sum_memory = 100 + (sum_memory - 100)*0.8    elif sum_memory >= 150:        sum_memory =  100 + 50*0.8 + (sum_memory -150)*0.5    return sum_memorydef main():    work_distance =  int(input("乘坐地铁上班的距离:   "))    work_day = int(input("一个月上班的天数:     "))    one_work_distance = termini(work_distance)    sum_fare = fare(one_work_distance,work_day)    return sum_fareif __name__ =="__main__":    memory = main()    print( memory)

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






17.编写登陆接口

基础需求:
  让用户输入用户名密码
  认证成功后显示欢迎信息
  输错三次后退出程序
升级需求:
  可以支持多个用户登录 (提示,通过列表存多个账户信息)
  用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)


































python 学习_基础语法__练习

评论关闭