zoukankan      html  css  js  c++  java
  • python 学习_基础语法__练习

    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 = 0
    while 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 = 0
    while 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 = 2
    sum_1 = 0
    while count <= 100:
        if count %2 ==0:
            sum_1 += count
        else:
            sum_1 -= count
        count +=1
    print (sum_1)
    b. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12;
    count = 0
    while count < 12:
        count+=1
        if count ==6:
            pass
        else:
            print(count)
    count = 0
    
    while count < 12:
        count +=1
        if count == 6:
            continue
        print(count)

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

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

    e. 使用 while 循环实现输出 1-100 内的所有偶数;
    count = 0
    while count < 100:
        count +=1
        if count % 2 == 0:
            print( count )
    9. 简述 n 和n1 的关系
    n = 123456
    n1 = n

    n = 123456
    n1 = n
    print (n, n1)
    #结果 123456 123456
    
    n = 22222
    print (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:               %s
    age:                %s
    hometown:           %s
    love:               %s
    --------------------------------end------------------------------------
    """%(name,name,age,hometown,love)
    
    print (msg)

    结果:
    name: yy
    age:   18
    hometown: mb
    love: girl
    
    ------------------------info yy 的信息概要---------------------------
    name:               yy
    age:                18
    hometown:           mb
    love:               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 = 0
    money = 10000
    rate = 0.0325
    while money <20000:
        year +=1
        money = money*(1+rate)
    print(year)
    
    
    
    
    
    
    13. 打印出以下图形

    *
    **
    ***
    ****
    *****
    *****
    ****
    ***
    **
    *
    
    
    count = 0
    while 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= 3000
    if memory < 50000:
        print (salary)
    elif memory >=50000 and memory <100000:
        salary += memory*0.03
    elif memory >=100000 and memory <150000:
        salary += memory*0.05
    elif memory >=150000 and memory <250000:
        salary += memory*0.08
    elif memory >=250000 and memory <350000:
        salary += memory*0.1
    else:
        salary += memory * 0.15
    
    
    print(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_memory
    
    def 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_fare
    
    if __name__ =="__main__":
        memory = main()
        print( memory)
    
    
    
    
    
    
    

     

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


    count = 0
    length_len= 0
    high = 100
    
    while count < 10:
        count +=1
        length_len += high      #下落
        high = high/2           #高度
        length_len += high      #上升
    print("共经过%s米,第10次反弹高度%s米"%(length_len,high))
    
    
    
     

    17.编写登陆接口

    
    

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




    """
    基础需求:
      让用户输入用户名密码
      认证成功后显示欢迎信息
      输错三次后退出程序
    升级需求:
      可以支持多个用户登录 (提示,通过列表存多个账户信息)
      用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
    """
    import sys
    user_passwd="user_passwd.txt"
    user_lock = "user_lock.txt"
    def login(user,passwd):
        with open(user_passwd) as f:
            for line in f:
                username,password =line.strip().split("|")
                if user == username and passwd == password:
                    print ("你好, %s"%user)
                    sys.exit()
    
    def check_lock(user):
        with open(user_lock) as f:
            for line in f:
                username = line.strip()
                if user == username:
                    print("用户 %s 被锁定" % user)
                    sys.exit()
    def main():
        count = 0
        while count <3:
            user = (input("username:     ")).strip()
            check_lock(user)
            passwd = (input("passwd:     ")).strip()
            login(user,passwd)
            count += 1
        else:
            print("你被锁定",user)
            with open(user_lock, "a") as file:
                file.write(user)
                file.write("
    ")
    
    main()
    
    
    
     
    user_passwd.txt
    
    
    august|111111
    xuxuxu|222222
    yangyang|333333
    yangyang1|444444
    yangyang2|555555
    yangyang3|666666
    yangyang4|777777
    
    
    





























  • 相关阅读:
    golang版本管理工具gvm
    golang问题101
    服务器部署Jupyter Notebook
    Python 获取MySql某个表所有字段名
    Python自带HTTP文件传输服务
    log4net 配置
    GridView控件RowDataBound事件中获取列字段值的几种途径
    最简单去Button回车事件
    oracle 多条执行语句同时执行
    MERGE Into
  • 原文地址:https://www.cnblogs.com/augustyang/p/10411541.html
Copyright © 2011-2022 走看看