• Python模块练习


    import sys,pprint
    import math
    import copy

    pprint.pprint(sys.path)
    print(dir(copy))#查看模块包含的内容
    print([n for n in dir(copy) if not n.startswith('_') ])#过滤掉 _xxx_



    #-------------------------输出结果----------------------------------#
    """
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test8.py
    ['D:\Python-Test\qiubai\qiubai',
    'D:\Python-Test\qiubai',
    'C:\python3.7\python36.zip',
    'C:\python3.7\DLLs',
    'C:\python3.7\lib',
    'C:\python3.7',
    'C:\python3.7\lib\site-packages']
    ['Error', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_copy_dispatch', '_copy_immutable', '_deepcopy_atomic', '_deepcopy_dict', '_deepcopy_dispatch', '_deepcopy_list', '_deepcopy_method', '_deepcopy_tuple', '_keep_alive', '_reconstruct', 'copy', 'deepcopy', 'dispatch_table', 'error']
    ['Error', 'copy', 'deepcopy', 'dispatch_table', 'error']
    """

    print(help(print()))#获取帮助
    #-------------------------输出结果----------------------------------#
    """
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test8.py

    Help on NoneType object:

    class NoneType(object)
    | Methods defined here:
    |
    | __bool__(self, /)
    | self != 0
    |
    | __new__(*args, **kwargs) from builtins.type
    | Create and return a new object. See help(type) for accurate signature.
    |
    | __repr__(self, /)

    """
    print(range.__doc__)#查看range有哪些参数
    #-------------------------输出结果----------------------------------#
    """
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test8.py
    range(stop) -> range object
    range(start, stop[, step]) -> range object

    Return an object that produces a sequence of integers from start (inclusive)
    to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.
    start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.
    These are exactly the valid indices for a list of 4 elements.
    When step is given, it specifies the increment (or decrement).
    """


    print(copy.__file__)#查看copy的源码路径
    #-------------------------输出结果----------------------------------#
    """
    C:python3.7libcopy.py
    """

    #时间
    import time
    print(time.asctime())#将时间元组转为字符串
    print(time.localtime())#将秒数转为时间元组
    #-------------------------输出结果----------------------------------#
    """
    Mon Aug 14 14:06:27 2017
    time.struct_time(tm_year=2017, tm_mon=8, tm_mday=14, tm_hour=14, tm_min=6, tm_sec=27, tm_wday=0, tm_yday=226, tm_isdst=0)
    """


    #random随机数
    from random import *
    from time import *
    date1 = (2008,1,1,1,0,0,-1,-1,-1)
    time1 = mktime(date1)
    date2 = (2009,1,1,1,0,0,-1,-1,-1)
    time2 = mktime(date2)
    print(asctime(localtime(uniform(time1,time2))))#将数字转换为易读的日期

    #-------------------------输出结果----------------------------------#
    """
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test8.py
    Thu Oct 23 19:22:33 2008
    """

    #shelve模块 在文件中存储数据
    #open函数会返回一个shelf对象,可以用它来存储内容,只需将它当做普通字典操作
    import shelve
    shelf = shelve.open("shelve.txt")
    shelf['x']=['a','b','c']
    shelf['x'].append('d')#为什d没有添加进去
    print(shelf['x'])
    #需将临时变量绑定到获取的副本上,并且在它修改后重新存储这个副本
    tmp = shelf['x']
    tmp.append('z')
    shelf['x']=tmp
    print(shelf['x'])
    shelf.close()
    #-------------------------输出结果----------------------------------#
    """
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test8.py
    ['a', 'b', 'c']
    ['a', 'b', 'c', 'z']
    """
    #简单的数据库示例
    import shelve,sys

    def db_store(db):
    "存储"
    pid = input("please input your num: ")
    person={}
    person['name'] = input("please input your name: ")
    person['age']=input("please input your age: ")
    person['phone']=input("please input your phone: ")
    db[pid]= person

    def db_lookup(db):
    "查找"
    pid = input("please input your id num:")
    field = input("would you want to know name age or phone:")
    field = field.strip().lower()#用户可以随意输入大小写和空格
    print(field.capitalize() + ':' + db[pid][field])

    def prin_cmd():
    print("*"*15)
    print("store:存储")
    print("lookup:查找")
    print("quit:退出")
    print("?:帮助")
    print("*" * 15)

    def enter_cmd():
    cmd = input("? for help: ")
    cmd = cmd.strip().lower()
    return cmd

    def main():
    database = shelve.open("db_person.txt")#主程序打开数据库shelf然后作为参数传给其它函数
    try:
    while True:
    cmd = enter_cmd()
    if cmd == 'store':
    db_store(database)
    elif cmd == 'lookup':
    db_lookup(database)
    elif cmd == '?':
    prin_cmd()
    elif cmd == 'quit':
    return
    finally:#保证数据库关闭
    database.close()
    if __name__ == '__main__':
    main()

    #-------------------------输出结果----------------------------------#
    """
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test8.py
    ? for help:
    stroe
    ? for help:
    store
    please input your num:
    2
    please input your name:
    lisi
    please input your age:
    23
    please input your phone:
    132
    ? for help:
    store
    please input your num:
    3
    please input your name:
    adjf;as
    please input your age:
    23
    please input your phone:
    23423
    ? for help:
    lookup
    please input your id num:2
    would you want to know name age or phone:name
    Name:lisi
    ? for help:
    ?
    ***************
    store:存储
    lookup:查找
    quit:退出
    ?:帮助
    ***************
    ? for help:
     


  • 相关阅读:
    Java 入门 9 Random 类 随机数
    Java 入门16 面向对象实现之前的小案例购物车
    Java 入门10 java的内存分配,数组内存图( 方法区 栈内存 堆内存)
    JAVA 12 联系回顾之前知识
    Java 13 面向对象的编程 小案例 实现购物车
    面试泛型
    面试反射
    从HashMap的执行流程开始 揭开HashMap底层实现
    面试什么是面向对象?面向对象三大特性?
    面试JMM内存模型
  • 原文地址:https://www.cnblogs.com/acer-haitao/p/7364120.html
Copyright © 2020-2023  润新知