输入输出

welcome_str = 'Welcome to matrix {prefix} {name}.'
welcome_dic = {
    'prefix': 'Mr.' if  gender == 'y' else 'Mrs',
    'name': name
}
print('authoroing')
print(welcome_str.format(**welcome_dic))

####输出
authoroing
Welcome to matrix Mrs fang.

a = input()
b = input()
print('a +b = {}'.format(a + b))

print('type of a is {},type of b is {}'.format(type(a),type(b)))
print('a + b = {}'.format(int(a) + int(b)))
####输出
1
2
a +b = 12
type of a is <class 'str'>,type of b is <class 'str'>
a + b = 3

实现一个文本文件中单词的统计数量:同时文本文件中的数量非常大

import re

CHUNK_SIZE = 100

def parse_to_word_list(text,last_word,word_list):
    text = re.sub(r'^[\w]','',last_word + text)
    text = text.lower()
    cur_word_list = text.split('')
    cur_word_list,last_word = cur_word_list[:-1],cur_word_list[-1]
    word_list += filter(None,cur_word_list)
    return last_word
def solve():
    with open('in.text','r') as fin:
        word_list,last_word = [],''
        while True:
            text = fin.read(CHUNK_SIZE)
            if not text:
                break
            last_word = parse_to_word_list(text,last_word,word_list)
        word_cnt = {}
        for word in word_list:
            if word not in word_cnt:
                word_cnt[word] = 0
            word_cnt[word] += 1
        sortd_word_cnt = sorted(word_cnt.items(),key = lambda kv:kv[1],reverse=True)
        return sorted_word_cnt
print(solve())

假设你把电脑里的有100G数据,想要传送到学校的电脑,假如你有百度网盘,但是空间有限,实现讲电脑里的数据通过百度网盘传到学校,故必须在家写一个server.py,在学校写一个client.py

server.py
#server上的电脑上所有文件都在BASR_DIR中,网盘路径在NET_DIR
import os
from shutil import copyfile
import time

BASR_DIR = 'server/'
NET_DIR = 'net/'

def main():
  filenames = os.listdir(BASE_DIR)
  for i ,filename in enumerate(filenames):
    print('copying {} into net dirve...{}/{}'.format(filename,i + 1,len(filenames)))
    copyfile(BASR_DIR + filename,NET_DIR + filename)
    print('copied {} into net drive,waiting client complete...{}/{}'.format(filename,i+1,len(filenames)))
    while os.path.exists(NET_DIR + filename):
        time.sleep(3)
    print('transferred {} into client.{}/{}'.format(filename,i+1,len(filenames)))
if _name_ == "_main_"
main()