随笔分类 - python 基础
python base
摘要:from functools import partial def foo(a, b, c, d, f): return a + b + c + d + f if __name__ == '__main__': print(foo(10, 20, 30, 40, 5)) print(foo(10, 20, 30, 40, 25)) print(foo(10, ...
阅读全文
摘要:def get_age(name, age): print('%s is %s years old' % (name, age)) get_age('bob', 25) # 参数按顺序传递 get_age(25, 'bob') # 没有语法错误,但是语义不对 get_age(age=25, name='bob') # get_age() # Error,少参数 # get_age...
阅读全文
摘要:def set_age(name,age): if not 0 set_age2('xiaohong', 90) File "F:/20180731桌面/CMDB/CMDB/test/Python_test.py", line 979, in set_age2 assert 0 < age < 80, '年龄超过范围' # 断言异常 AssertionError:...
阅读全文
摘要:try: n = int(input("number: ")) result = 100 / n except (ValueError, ZeroDivisionError): print('invalid number') except (KeyboardInterrupt, EOFError):
阅读全文
摘要:try: # 把有可能发生异常的语句放到try里执行 n = int(input("number: ")) result = 100 / n print(result) except ValueError: print('invalid number') except ZeroDivisionErr
阅读全文
摘要:import pickle """以前的文件写入,只能写入字符串,如果希望把任意数据对象(数字、列表等)写入文件, 取出来的时候数据类型不变,就用到pickle了 """ # shop_list = ["eggs", "apple", "peach"] # with open('/tmp/shop.
阅读全文
摘要:import os os.getcwd() # 显示当前路径 os.listdir() # ls -a os.listdir('/tmp') # ls -a /tmp os.mkdir('/tmp/mydemo') # mkdir /tmp/mydemo os.chdir('/tmp/mydemo'
阅读全文
摘要:import time t = time.localtime() # 返回当前时间的九元组 time.gmtime() # 返回格林威治0时区当前时间的九元组 time.time() # 常用,与1970-1-1 8:00之间的秒数,时间戳 time.mktime(t) # 把九元组时间转成时间戳 time.sleep(1) time.asctime() # 如果有参数,是九元组形式...
阅读全文
摘要:import time result = 0 start = time.time() # 返回运算前时间戳 for i in range(10000000): result += i end = time.time() # 返回运算后时间戳 print(result) print(end - start) 结果输出:
阅读全文
摘要:import getpass userdb = {} def register(): username = input('username: ') if username in userdb: print('%s already exits.' % username) else: password = input('password: ...
阅读全文
摘要:with open('./passwd') as fobj: aset = set(fobj) print(aset) with open('./mima') as fobj: bset = set(fobj) print(bset) with open('diff.txt', 'w') as fobj: fobj.writelines(aset -...
阅读全文
摘要:# 集合相当于是无值的字典,所以也用{}表示 myset = set('hello') len(myset) for ch in myset: print(ch) aset = set('abc') bset = set('cde') aset & bset # 交集 aset.intersection(bset) # 交集 aset | bset # 并集 aset.union...
阅读全文
摘要:adict = dict([('name', 'bob'),('age', 30)]) len(adict) hash(10) # 判断给定的数据是不是不可变的,不可变数据才能作为key adict.keys() adict.values() adict.items() # get方法常用,重要 adict.get('name') # 取出字典中name对应的value,如果没有返回None...
阅读全文
摘要:adict = dict() # {} dict(['ab', 'cd']) bdict = dict([('name', 'bob'),('age', 25)]) {}.fromkeys(['zhangsan', 'lisi', 'wangwu'], 11) for key in bdict: print('%s: %s' % (key, bdict[key])) print("%...
阅读全文
摘要:import time length = 19 count = 0 while True: print('\r%s@%s' % ('#' * count, '#' * (length - count)), end='') try: time.sleep(0.3) except KeyboardInt
阅读全文
摘要:stack = [] def push_it(): item = input('item to push: ') stack.append(item) def pop_it(): if stack: print("from stack popped %s" % stack.pop()) def vi
阅读全文
摘要:import sys def unix2dos(fname): dst_fname = fname + '.txt' with open(fname) as src_fobj: with open(dst_fname, 'w') as dst_fobj: for line in src_fobj:
阅读全文
摘要:randpass模块参见 https://www.cnblogs.com/hejianping/p/10881293.html 脚本名字:adduser.py
阅读全文

浙公网安备 33010602011771号