一起學Python吧~練習篇

**

跟着學的小夥伴們肯定都沒背會之前的知識點吧,所以今天阿坤帶大家一起練習敲代碼練手速.

**

#!/bin/env python3
#-*- conding:utf8 -*-
#學python3的第七天
#自由練習:目標代碼3000斬!
#實現linux系統中unix2dos功能
# import  sys
#
# def unix2doc(fname):
#     dst_fname = fname + '.txt'
#
#     with open(fname)as src_fobj:
#         with open(dst_fname,'w')as dst_fobj:
#             for line in src_fobj:
#                 line = line.rstrip() + '\r\n'
#                 dst_fobj.write(line)
#
#
# if __name__ == '__main__':
#     unix2doc(sys.argv[1])
#動畫程序:@從一行#中穿過
# import time
#
# length = 19
# count = 0
#
# while True:
#     print('\r%s@%s'% ('#'*count,'#'*(length-count)),end='') #\r回車不換行
#     try:
#         time.sleep(0.3)
#     except KeyboardInterrupt:
#         print('\nBye-bye')
#         break
#     if count == length:
#         count = 0
#     count += 1
#字典基礎用法
# adict = dict() #{}
# dict(['ab','cd'])
# bdict = dict([('name','bob'),('age','25')])
# print({}.fromkeys(['zhangsan','lisi','wangwu'],11))
#
# for key in bdict:
#     print('%s:%s'% (key,bdict[key]))
#
# print('%(name)s: %(age)s'% bdict)
#
# bdict['name'] = 'tom'
# bdict['email'] = '[email protected]'
#
# del bdict['email']
# bdict.pop('age')
# bdict.clear() #清除
# #字典常用方法
# adict = dict([('name','bob'),('age',25)])
# print(adict)
# hash(10) #判斷給定的數據是不是不可變的,不可變的數據才能作爲key
# adict.keys()
# adict.values()
# adict.items()
# #get方法常用,重要
# adict.get('name') #取出字典中name對應的value,如果沒有則返回None
# print(adict.get('qq','not found')) #沒有qq 返回指定內容
# print(adict.get('age','not found'))
# adict.update({'phone:' '18712929120'})
#集合相當於是無值的字典,所以也用{}表示
# myset = set('hello')
# print(len(myset))
# for ch in myset:
#     print(ch)
#
# aset = set('abc')
# bset = set('cde')
# print(aset & bset) #交集
# aset.intersection(bset) #交集
# print(aset|bset) #並集
# aset.union(bset) #並集
# aset -bset #差補
# print(aset.difference(bset)) #差補
# aset.add('new')
# aset.update(['aaa','bbb'])
# print(aset)
# aset.remove('bob')
# cset = set('abcde')
# dset = set('bcd')
# cset.issuperset() #cset是dset的超集麼?
# cset.issubset() #dset是cset的子集麼?
# #對比昨天和今天訪問日誌 取出第二個文件有 第一個文件沒有的url
# #cp /etc/passwd .
# #cp /etc/passwd mima
# #vim mima ->修改,與passwd有些許別
#
# with open('passwd')as fobj:
#     aset = set(fobj)
#
# with open('mima')as fobj:
#     bset = set(fobj)
#
# with open('diff.txt','w')as fobj:
#     fobj.writelines(bset -aset)
# import getpass
#
# userdb = {}
#
# def register():
#     username = input('username: ')
#     if username in userdb:
#         print('s already exists.'% username)
#     else:
#         password = input('password: ')
#         userdbp[username] = password
#
# def login():
#     username = input('username: ')
#     password = getpass.getpass('password: ').strip()
#     if userdb.get(username) != password:
#         print('login failed')
#     else:
#         print('login successful')
#
#
# def show_menu():
#     cmds = {'0':register,'1':login}
#     prompt = """(0)register
# (1)login
# (2)exit
# PLease input your choice(0/1/2): """
#
#     while True:
#         choice = input(prompt).strip()[0]
#         if choice not in '012':
#             print('Invalid input.Try again.')
#             continue
#         if choice == '2':
#             break
#
#         cmds[choice]()
#
# if __name__ == '__main__':
#     show_menu()
#
# #計算千萬次加法運算時間
# import  time
#
# result = 0
# start = time.time() #返回運算前時間戳
# for i in range(1000000):
#     result += i
# end = time.time() #返回運算後時間戳
# print(result)
# print(end-start)
# import getpass #導入模塊
#
# username = input('username: ')
# #getpass 模塊中,有一個方法也叫getpass
#
# if username == 'bob' and password == '123456':
#     print('Login successful')
# else:
#     print('Login incorrect')
#石頭剪刀布
# import  random
#
# all_choices = ['石頭','剪刀','布']
# computer = random.choice(all_choices)
# player = input('請出拳頭: ')
#
# #print('Your choice:',player,"Computer's choice:",computer)
# print("Tour chocie:%s,Computer's chocie: %s"% (player,computer))
# if player == '石頭':
#     if computer == '石頭':
#         print('平局')
#     elif computer == '剪刀':
#         print('You WIN!!!')
#     else:
#         print('You LOSE!!!')
# elif player == '剪刀':
#     if computer == '石頭':
#         print('You LOSE!!!')
#     elif computer == '剪刀':
#         print('平局')
#     else:
#         print('You WIN!!!')
# else:
#     if computer == '石頭':
#         print('You WIN!!!')
#     elif computer == '剪刀':
#         print('You LOSE!!!')
#     else:
#         print('平局')
# #改進的石頭剪刀布
# import  random
#
# all_choices = ['石頭','剪刀','布']
# win_list = [['石頭','剪刀'],['剪刀','布'],['布','石頭']]
# prompt = """(0)石頭
# (1)剪刀
# (2)布
# 請選擇(0/1/2): """
# computer = random.choice(all_choices)
# ind = int(input(prompt))
# player = all_choices[ind]
#
# print("Your chocie: %s,Computer's"% (player,computer))
# if player == computer:
#     print('\033[32;1m平局\033[0m')
# elif [player,computer] in win_list:
#     print('\033[31;1mYou WIN!!!\033[0m')
# else:
#     print('\033[31;1mYou LOSE!!!\033[0m')
# import random
#
# num = random.randint(1,10)
# running = True
#
# while running:
#     answer = int(input('guess the number: '))
#     if answer >num:
#         print('猜大了')
#     elif answer < num:
#         print('猜小了')
#     else:
#         print('猜對了')
#         running = False
# #猜數
# import  random
#
# num = random.randint(1,10)
# counter = 0
#
# while counter < 5:
#     answer = int(input('guess the number: '))
#     if answer > num:
#         print('猜大了')
#     elif answer < num:
#         print('猜小了')
#     else:
#         print('猜對了')
#         break
#     counter +=1
# else: #循環被brak就不執行了,沒有被break才執行
#     print('the number is: ',num)
#
# #while循環
# sum100 =0
# counter = 1
#
# while counter < 101:
#     sum100 += counter
#     counter +=1
#
# print(sum100)
# 石頭剪刀布三局兩勝
# import random
#
# all_choices = ['石頭','剪刀','布']
# win_list = [['石頭','剪刀'],['剪刀','布'],['布','石頭']]
# prompt = """(0)石頭
# (1)剪刀
# (2)布
# 請選擇(0/1/2): """
# cwin = 0
# pwin = 0
#
# while cwin < 2 and pwin <2:
#     computer = random.choice(all_choices)
#     ind = int(input(prompt))
#     player = all_choices[ind]
#
#     print("Your choice: %s,Computer's choice:%s"% (player,computer))
#     if player == computer:
#         print('\033[32;1m平局\033[0m')
#     elif [player,computer]in win_list:
#         pwin +=1
#         print('\033[31;1mYou WIN!!!\033[0m')
#     else:
#         cwin +=1
#         print('\033[31;1mYou LOSE!!!')
# #拷貝文件
# f1 = open('/bin/ls','rb')
# f2 = open('/root/ls','wb')
#
# data =f1.read()
# f2.write(data)
#
# f1.close()
# f2.close()
# #斐波那契額數列
# def gen_fib(l):
#     fib = [0,1]
#
#     for i in range(l-len(fib)):
#         fib.append(fib[-1] + fib[-2])
#
#     return fib #返回列表,不返回變量fib
#
# a = gen_fib(10)
# print(a)
# print('-'*50)
# n = int(input('length: '))
# print(gen_fib(n)) #不會把變量n傳入,是吧n代表的賦值給形參
# #拷貝文件
# import sys
#
# def copy(src_fname,dst_fname):
#     src_fobj = open(src_fname,'rb')
#     dst_fobj = open(dst_fname,'wb')
#
#     while True:
#         data = src_fobj.read(4096)
#         if not data:
#             break
#         dst_fobj.write(data)
#
#     src_fobj.close()
#     dst_fobj.close()
#
# copy(sys.argv[1],sys.argv[2])
# #執行方式
# #cp_func.py /etc/hosts /tmp/zhuji.txt
# #函數九九乘法表
# def mtable(n):
#     for i in range(1,n+1):
#         for j in range(1,i + 1):
#             print('%s*%s=%s'%(j,i,j*i),end='')
#         print()
#
# mtable(6)
# mtable(9)
# #生成密碼驗證密碼
# from random import  choice
# import string
#
# all_chs = string.ascii_letters + string.digits #大小寫字母加數字
#
# def gen_pass(n=8):
#     result = ''
#
#     for i in range(n):
#         ch = choice(all_chs)
#         result += ch #切記python裏沒有n++只有++n
#
#     return result
#
# if __name__ == '__main__':
#     print(gen_pass())
#     print(gen_pass(4))
#     print(gen_pass(10))
# #生成文本文件
# import  os
#
# def get_fname():
#     while True:
#         fname = input('filename: ')
#         if not os.path.exists(fname):
#             break
#         print('%s already exists.Try again'% fname)
#
#     return  fname
#
# def get_content():
#     content = []
#     print('輸入數據,輸入end結束')
#     while True:
#         line = input('>')
#         if line == 'end':
#             break
#         content.append(line)
#
#     return  content
#
# def wfile(fname,conten):
#     with open(fname,'w')as fobj:
#         fobj.writelines(content)
#
# if __name__ == '__main__':
#     fname = get_fname()
#     content = get_content()
#     content = ['%s\n'% line for line in content]
#     wfile(fname,content)
# #檢查合法標識符
# import sys
# import keyword
# import string
#
# first_chs = string.ascii_letters + '_'
# all_chs = first_chs + string.digits
#
# def check_id(idt):
#     if keyword.iskeyword(idt):
#         return '%s is keyword' % idt
#
#     if idt[0] not in first_chs:
#         return  '1st invalid'
#
#     for ind,ch in enumerate(idt[1:]):
#         if ch not in all_chs:
#             return 'char in postion #%s invalid' % (ind + 2)
#
#     return '%s is valid' % idt
#
# if __name__ == '__main__':
#     print(check_id(sys.arvg[1])) #python3 checkid.py abc@123
#創建用戶設置隨機密碼
import subprocess
import sys
from randpass2 import  randpass

def adduser(username,password,fname):
    data= """user information:
%s:%s
""" #subprocess.run('useradd %s' % username,shell=True) #3.0以前沒有run只能用call
    subprocess.call('useradd %s'% username,shell=True)
    subprocess.call(
        'echo %s |passwd --stdin %s'% (password,username),
        shell=True
    )
    with open(fname,'a')as fobj:
        fobj.write(data % (username,password))

if __name__ == '__main__':
    username = sys.argv[1]
    password = randpass()
    adduser(username,password,'/tmp/user.txt')
#python3 adduser.py john
# #模擬棧操作
# stack = []
#
# def push_it():
#     item = input('item to push: ')
#     stack.append(item)
#
# def pop_it():
#     if stack:
#         print('from stack poipped %s'% stack.pop())
#
# def view_it():
#     print(stack)
#
# def show_menu():
#     cmds = {'0':push_it,'1':pop_it,'2':view_it} #將函數存入字典
#     prompt = """(0)push it
# (1)pop it
# (2)view it
# (3)exit
# Please input your choice(0/1/2/3): """
#
#     while True:
#         #input()將得到字符串 用strip()取出兩端空白 再去下標爲0的字符
#         choice = input(prompt).strip()[0]
#         if choice not in '0123':
#             print('Invalid input.Try again.')
#             continue
#
#         if choice =='3':
#             break
#
#         cmds[chocie]()
#
# if __name__ == '__main__':
#     show_menu()
# #實現linux系統中unix2dos功能
# import sys
#
# def unix2doc(fname):
#     dst_fname = fname + '.txt'
#
#     with open(fname)as src_fobj:
#         with open(dst_fname,'w')as dst_fobj:
#             for line in src_fobj:
#                 line = line.rstrip() + '\r\n'
#                 dst_fobj.write(line)
#
#
# if __name__ == '__main__':
#     unix2dos(sys.argv[1])
# import time
#
# length = 19
# count = 0
#
# while True:
#     print('\r%s@%s'% ('#'*count,'#'*(length -count)),end='')
#     try:
#         time.sleep(0.3)
#     except KeyboardInterrupt:
#         print('\nBye-bye')
#         break
#     if count == length:
#         count = 0
#     count += 1
#
# #cp /etc/passwd .
# #cp /etc/[asswd mima
# #vim mima ->修改,與passwd有些區別
#
# with open('passwd')as fobj:
#     aset = set(fobj)
#
# with open('mima')as fobj:
#     bset = set(fobj)
#
# with open('diff.txt','w')as fobj:
#     fobj.writelines(bset-aset)
# #模擬註冊/登錄
# import getpass
#
# userdb = {}
#
# def register():
#     username = input('username: ')
#     if username ion userdb:
#         print('%s already exists.'% username)
#     else:
#         password = input('password: ')
#         userdb[username] = password
#
# def login():
#     username = input('username: ')
#     password = getpass.getpass('password: ')
#     if userdb.get(username) != password:
#         print('login failed')
#     else:
#         print('login successful')
#
# def show_menu():
#     cmds = {'0':register,'1':login}
#     prompt = """(0)register
# (1)login
# (2)exit
# Please input your choice(0/1/2): """
#
#     while True:
#         choice = input(prompt).strip()[0]
#         if choice not in '012':
#             print('Invalid input.Try again.')
#             continue
#         if choice == '2':
#             break
#
#         cmds[choice]()
#
#
# if __name__ == '__main__':
#     show_menu()
# #計算千萬次加法運算時間
# 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 exists.'% username)
#     else:
#         password = input('password: ')
#         userdb[username]= password
#
# def login():
#     username = input('username: ')
#     password = getpass.getpass('password: ')
#     if userdb.get(username) != password:
#         print('login failed')
#     else:
#         print('login succeessful')
#
# def show_menu():
#     cmds = {'0':register,'1':login}
#     prompt = """(0)register
# (1)login
# (2)exit
# Please input your choice(0/1/2): """
#
#     while True:
#         choice = input(prompt).strip()[0]
#         if choice not in '012':
#             print('Invalid input.Try again.')
#             continue
#         if choice =='2':
#             break
#
#         cmds[choice]()
# #模擬棧操作
# 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 view_it():
#     print(stack)
#
# def show_menu():
#     cmds = {'0':push_it,'1':pop_it,'2':view_it} #將函數存入字典
#     prompt = """(0)push it
# (1)pop it
# (2)view it
# (3)exit
# Please input your choice(0/1/2/3): """
#
#     while True:
#         #input()得到字符串,用strip()去除兩端空白,在取下標爲0的字符
#         chocie = input(prompt).strip()[0]
#         if chocie not in '0123':
#             print('Invalid input.Try again.')
#             continue
#
#         if choice =='3':
#             break
#
#         cmdsp[chocie]()
#
# if __name__ == '__main__':
#     show_menu()
#pickle存儲器
import pickle
"""以前的文件寫入,只能寫入字符串,如果希望吧任意數據對象(數字,列表等)寫入文件,
取出來的時候數據類型不變,就用到了pickle了
"""

#shop_list = ['eggs','apple','peach']
#with open('/tmp/shop.data','wb')as fobj:
#  pickle.dump(shop_list,fobj)
#
# with open('/tmp/shop.data','rb')as fobj:
#     mylist = pickle.load(fobj)
#
# print(mylist[0],mylist[1],mylist[2])
#os模塊常用方法
import os
#
# os.getcwd()#顯示當前路徑
# os.listdir() #ls -a
# os.listdir('/tmp') #ls -a /tmp
# os.mkdir('/tmp/mydemo') #mkdir /tmp/mt\\mydemo
# os.chdir('/tmp/mydemo') #cd /tmp/mydemo
# os.listdir()
# os.listdir()
# os.mknod('test.txt') #touch test.txt
# os.symlink('/etc/hosts','zhuji') #ln -s /etc/hosts zhuji
# os.path.islink('test.txt') #判斷test.txt是不是文件
# os.path.isdir('/tmp') #判斷tmp是不是目錄
# os.path.exists('/tmp') #判斷是否存在
# os.path.basename('/tmp/abc/aaa.txt')
# os.path.dirname('/tmp/abc/aaa.txt')
# os.path.split('/tmp/abc/aaa.txt')
# os.path.join('/home/tom','xyz,txt')
# os.path.abspath('test.txt') #返回當前目錄test.txt的絕對路徑
# #集合常用方法
# #集合相當於是無值的字典,所以也用{}表示
# myset =set('hello')
# print(len(myset))
# for ch in myset:
#     print(ch)
#
# aset =set('abc')
# bset = set('cde')
# aset & bset #交集
# aset.intersection(bset) #交集
# aset | bset #並集
# aset.union(bset) #並集
# aset - bset #差補
# aset.difference(bset) #差補
# aset.add('new')
# aset.update(['aaa','bbb'])
# aset.remove('bob')
# cset =set('abcde')
# dset =set('bcd')
# cset.issuperset(dset) #cset是dset的超集麼?
# cset.issubset(dset) #cset 是dset的子集麼?
# adict = dict() #{}
# dict(['ab','cd'])
# bdict = dict([('name','bob'),('age',25)])
# print({}.fromkeys(['zhangsan','lisi','wangwu']),11)
#
# for key in bdict:
#     print('%s:%s'% (key,bdict[key]))
#
# print('%(name)s:%(age)s' %bdict)
#
# bdict['name']='tom'
# bdict['email']='[email protected]'
#
# del bdict['email']
# bdict.pop('age')
# bdict.clear()
# import sys
# import keyword
# import string
#
# first_chs = string.ascii_letters + '_'
# all_chs = first_chs + string.digits
#
# def check_id(idt):
#     if keyword.iskeyword(idt):
#         return "%s is keyword" % idt
#
#     if idt[0] not in first_chs:
#         return "1st invalid"
#
#     for ind,ch in enumerate(idt[1:]):
#         if ch not in all_chs:
#             return "char in postion #%s invalid" % (ind +2)
#
#     return  '%s is valid'% idt
#
# if __name__ == '__main__':
#     print(check_id(sys.argv[1])) #python3 checkid.py abc@123
#
# import os
#
# def get_fname():
#     while True:
#         fname =input('filename: ')
#         if not os.path.exists(fname):
#             break
#         print('%s already exists.Try again'% fname)
#
#     return fname
#
# def get_content():
#     content = []
#     print('輸入數據,輸入end結束')
#     while True:
#         line = input('>')
#         if line == 'end: '
#             break
#         content.append(line)
#
#     return content
#
# def wfile(fname,content):
#     with open(fname,'w')as fobj:
#         fobj.writelines(content)
#
# if __name__ == '__main__':
#     fname = get_fname()
#     content = get_content()
#     content = ['%s\n'% line for line in content]
#     wfile(fname,content)
#
# from random import  choice
# import string
#
# all_chs = string.ascii_letters + string.digits
#
# def gen_pass(n=8):
#     result = ''
#
#     for i in range(n):
#         ch = choice(all_chs)
#         result += ch
#
#     return result
#
# if __name__ == '__main__':
#     print(gen_pass())
#     print(gen_pass(4))
#     print(gen_pass(10))
#----
# def mtable(n):
#     for i in range(1,n+1):
#         for j in range(1,i + 1):
#             print('%s*%s=%s'%(j,i,j*i),end="")
#         print()
#
# mtable(6)
# mtable(9)
# def gen_fib(l):
#     fib = [0,1]
#
#     for i in range(l - len(fib)):
#         fib.append(fib[-1]+fib[-2])
#
#     return  fib #返回列表,不反悔變量fib
#
# a = gen_fib(10)
# print(a)
# print('-'*50)
# n = int(input('length:'))
# print((gen_fib(n))) #不會把變量n傳入,是把n代表的賦值給形參
# import  sys
#
# def copy(src_fname,dst_fname)
#     src_fobj = open(src_fname,'rb')
#     dst_fobj = open(dst_fname,'wb')
#
#     while True:
#         data = src_fobj.read(4096)
#         if not data:
#             break
#         dst_fobj.write(data)
#
#     src_fobj.close()
#     dst_fobj.close()
#
# copy(sys.argv[1],sys.argv[2])
# #執行方式
# import random
#
# all_choices = ['石頭', '剪刀', '布']
# win_list = [['石頭', '剪刀'], ['剪刀', '布'], ['布', '石頭']]
# prompt = """(0) 石頭
# (1) 剪刀
# (2) 布
# 請選擇(0/1/2): """
# cwin = 0
# pwin = 0
#
# while cwin < 2 and pwin < 2:
#     computer = random.choice(all_choices)
#     ind = int(input(prompt))
#     player = all_choices[ind]
#
#     print("Your choice: %s, Computer's choice: %s" % (player, computer))
#     if player == computer:
#         print('\033[32;1m平局\033[0m')
#     elif [player, computer] in win_list:
#         pwin += 1
#         print('\033[31;1mYou WIN!!!\033[0m')
#     else:
#         cwin += 1
#         print('\033[31;1mYou LOSE!!!\033[0m')
# fib = [0,1]
#
# for i in range(8):
#     fib.append(fib[-1]+fib[-2])
#
# print(fib)
#
# while True:
#     yn = input('Continue(y/n)')
#     if yn in ['n','N']:
#         break
#     print('running...')
# sum100 = 0
# counter = 0
#
# while counter < 100:
#     counter += 1
#     #if counter %2:
#     if counter %2 == 1:
#         continue
#     sum100 += counter
#
# print(sum100)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章