diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc3334c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +__pycache__/ +.DS_Store diff --git a/01/10-operator.py b/01/10-operator.py index 1ff98cb..6a93754 100644 --- a/01/10-operator.py +++ b/01/10-operator.py @@ -16,9 +16,9 @@ print("2<3:", 2 < 3) print("3>2:", 3 > 2) print("3=3:", 3 == 3) -print("5!=6:", 5 !=5) +print("5!=6:", 5 != 5) print("5>=4:", 5 >= 4) -print("5<=4:", 5 <= 4 ) +print("5<=4:", 5 <= 4) # 逻辑运算符:and or not print("not False = ", not False) print("False and True = ", False and True) diff --git a/01/14-type_dictionary.py b/01/14-type_dictionary.py new file mode 100644 index 0000000..da1ad28 --- /dev/null +++ b/01/14-type_dictionary.py @@ -0,0 +1,100 @@ +# 轮到字典了 +# 吐槽一下:第一部分终于结束了,不知道现在还剩几个人在听啊 +# 有了前面序列的基础,字典其实也就不难理解了,毕竟操作上有很多相似之处,只不过字典是一种映射 +# 看看用以前的方法弄一个电话簿 +from copy import deepcopy +names = ['mdzz0', 'mdzz1', 'mdzz2'] +phones = ['01275', '02336', '03997'] +for name in names: + print(name+':'+phones[names.index(name)]) + +# 再看看字典版本 +phone_dict = dict(zip(names, phones)) +# print(type(phone_dict)) +print(phone_dict) +# 上面的例子看出来,字典是更加接近于人类思维的数据类型,此外也展示了dict的功能 +# dict()是一个类型函数,可以从映射来创建字典 +print(dict([('mdzz0', '01'), ('mdzz1', '02')])) +print(dict(mdzz0='01', mdzz1='02')) # 键不要加引号 + +# 一些字典操作 +print(len(phone_dict)) +print('mdzz1:'+phone_dict['mdzz1']) # 字典索引方法 +phone_dict['mdzz1'] = '01111' +print('mdzz1:'+phone_dict['mdzz1']) +print('mdzz1' in phone_dict) # 检查字典中是否有该键 +phone_dict['mdzz3'] = '00000' # 字典可以动态添加项,而序列不可能那一个不存在的索引去访问 +print(phone_dict) + +# 依赖于字典的字符串格式化,之前说过了使用元组传入数据的字符串格式化 +print("**********************************formatted string*********************************") +str_origin = "mdzz0:%(mdzz0)s\nmdzz1:%(mdzz1)s\nmdzz2:%(mdzz2)s" +str_formate = str_origin % phone_dict +print(str_formate) + +template = '''\ + +%(title)s + +

%(title)s

+

%(text)s

+ + +''' +data = template % {'title':'My Page', 'text':'Welcome to my home page!'} +print(data) + +print('************************************字典方法*************************************') +dict_test = {'a': '1'} +dict_test1 = dict_test # 目前二者关联到同一个字典对象 +print("dict_test:", dict_test) +print('dict_test1: ', dict_test1) +dict_test = {} # 给dict_test关联到一个新字典,这时候两者就不再一样了 +print('dict_test: ', dict_test) +print('dict_test1: ', dict_test1) +dict_test = {'a': '1'} +dict_test1 = dict_test # 目前二者关联到同一个字典对象 +dict_test.clear() # 这样会清除掉原始字典,两个对象都变为空 +print('dict_test: ', dict_test) +print('dict_test1: ', dict_test1) + +# 浅拷贝,理解难度变大了哦 +d = {'name': ['jrt', 'JRT']} # 字典某个键对应的值又是一个新的容器,这样对于浅拷贝来说就是简单复制对象的值 +c = d.copy() +d['name'].append('copy') +print('origin', d['name']) +print('copy:', c['name']) + +d = {'name': ['jrt', 'JRT']} + +dc = deepcopy(d) +d['name'].append('deepcopy') # 对于深拷贝,当对象的值是引用时,会递归的复制,得到的拷贝和原始对象完全一致且独立 +print('origin', d['name']) +print('deepcopy', dc['name']) +print(phone_dict.keys()) +print(phone_dict.items()) +print(phone_dict.values()) + +a = phone_dict.pop('mdzz0') +print('弹出的值:'+a) +print('弹出项后字典:', phone_dict) + + +phone_dict.popitem() +print('随机弹出一项后:', phone_dict) +phone_dict.popitem() +print('随机弹出一项后:', phone_dict) +phone_dict.popitem() +print('随机弹出一项后:', phone_dict) + +# the end +# 是不是看到了希望呢? + + + + + + + + + diff --git a/01/Git-Tutorial.md b/01/Git-Tutorial.md new file mode 100644 index 0000000..2ab411e --- /dev/null +++ b/01/Git-Tutorial.md @@ -0,0 +1,57 @@ +Git使用 +===== + +为什么要把Git 和 Github 分开呢。应为本身就是两个东西。 + +Git 是版本控制软件。类似的还有svn + +Github 是代码托管平台。类似的有gitlib, gitee, bitbucket, Coding........ + +> 为什么要用Github? Github 重新定义是社会化编程这一概念 + +这里不想说千篇一律的东西。比如分支管理,打标签啊之类的 + + +[git中文文档](https://git-scm.com/book/zh/v2) + +[如果你看不懂版本分支图,请看这篇](https://github.com/SB-IM/developer-center/wiki/Guidelines-for-Developers) + +[如何写一个优秀的commit](https://github.com/linuxdeepin/developer-center/wiki/Git-Commit-Message-Style) + + +### git 常用命令(请根据此表单看文档,按使用频率排序) + +> 这里是天天用git的人总结的 + +- 查看系列(不对仓库造成任何变化的) + - git status + - git diff + - git log + - git branch +- 日常用 + - git commit -am "我是描述" + - git pull + - git push + - git checkout 分支名 + - git add . +- 版本控制用 + - git merge + - git checkout -b 分支名 + - git reset --hard 版本哈希值 + - git rebase -i 版本哈希值 +- 发布版本用 + - git tag -am "我是描述" + +### 关于git 配置 + +> 分成两部分,一部分是git 的用户全局配置,一部分是git 仓库配置。(注:我没听说过git 有系统全局配置,做git 服务器会有) + +#### git 的用户全局配置 +一般都在这个里`~/.gitconfig`,用`git config --global user.name "John Doe"` 的配置会存在这里 + +#### git 仓库配置 +这个在每个git 仓库里都有,在`.git/config`,这里记录着远程仓库的名称和地址。(注:`origin` 是仓库的默认值。不指定仓库名会使用`origin`仓库) + +#### .gitignore +用于不想被控制版本的文件,比如编译后的产物,中间的tmp。。。但要注意已经加入版本控制的文件不受这个文件影响。(可以理解这个是git add 时会发生作用的) + diff --git a/01/GithubTutorial.md b/01/GithubTutorial.md index 83343cb..b943650 100644 --- a/01/GithubTutorial.md +++ b/01/GithubTutorial.md @@ -14,7 +14,7 @@ - Mac: 默认安装了Git - Linux: - 对于大多数发行版,通过软件包获取即可~(目测不需要展开)~ + 对于大多数发行版,通过软件包获取即可~~(目测不需要展开)~~ - debian && Ubuntu && LinuxMint && deepin ```sh apt install git @@ -38,7 +38,7 @@ 最后不管你是什么系统,都要注册GitHub账号哦,注册方式很简单这里略过,温馨提示一点,请认真填写您的用户名和邮箱地址,他们真的很有用! -***用户名很重要。请不要乱取!!!***。这里很重要!!!。用户名以后可以更改。所以不用太在意 +**用户名很重要。请不要乱取!!!**。这里请千万注意!!!。(虽然用户名和邮箱一样是可以后期修改的,只不过并不建议。) github的用户名会作为url的一部分,所以对大小写不敏感。如:http://github.com/:你的用户名/:你的仓库名 @@ -71,6 +71,13 @@ https://github.com/longtian/gpg-howto # Github +### 关于 watch, star, fork +watch 是订阅这个仓库的信息。建议每个人都订阅JRT-PythonTutorial 和 JRT-discuss + +star 可以理解为收藏夹一类的东西 + +fork 你如果想要对某一仓库提改善意见。但没有权限时。要先fork到自己的仓库提交,然后提Pull request。等待对方的merge + ### 可以申请学生包哦!(学生包用途自己去挖掘) [https://education.github.com/](https://education.github.com/) diff --git a/02/condition.py b/02/condition.py new file mode 100644 index 0000000..9d8fd1d --- /dev/null +++ b/02/condition.py @@ -0,0 +1,120 @@ +from math import sqrt +# 现在开始来看看程序的流程控制,有了这些之后理论上就什么都可以做了,突出服务于使用的目的,这里真的很简单 +# 先来看一看语法糖 +# 序列解包1 +dictionary = {'a': 1, 'b': 2, 'c': 3} +for key, value in dictionary.items(): # 这里就是一种unpacking + print(key, value) +values = 1, 2, 3 +# 序列解包1 +x, y, z = values +print(x, y, z) +# 序列解包2 +x, y, z = 1, 2, 3 +print(x, y, z) +# 序列解包3 +x, y = y, x +print(x, y, z) +# 链式赋值 +x = y = 1 +print(x, y) +# 增量赋值 +x += 1 +y **= 2 +z /= 2 +print(x, y, z) + +# 分支结构 +num = float(input("please input a number between 0~100\n>")) +assert(0 < num < 100) # 使用断言 +if num > 90: + print("congratulations!") +elif num > 60: + print("emmmmm") +else: + print("sorry!") + +name = input('whats your name?\n>') +if name.endswith('jrt'): + if name.startswith('Mr.'): + print('hello Mr. jrt') + if name.startswith('Mrs.'): + print('hello Mrs. jrt') + else: + print('hello jrt') +else: + print("hello") + + +# 来算算数吧,输出10!,下面这两种循环都可以 +i = 1 +res = 1 +while(i <= 10): + res *= i + i += 1 +print(res) + +res = 1 +for i in range(1, 11): # xrange()函数是更高效的,他返回的是一个可迭代对象,而range()返回的直接就是一个列表 + res *= i +print(res) + +words = ['life', 'is', 'short', 'I', 'use', 'python'] +for word in words: + print(word) + +d = {'x': 1, 'y': 2, 'z': 3} +for key in d: + print(key + ':', d[key]) + +# 并行迭代的实现,很常见的用法 +names = ['a', 'b', 'c'] +scores = [90, 80, 70] +for name, score in zip(names, scores): + print(name, ':', score) + +print('********************************************************************') +strings = ['asfsfsf', 'bldfjgld', 'slfgsdfhgsidf', 'oertuwoehnccxvnm'] +for string in strings: + if 'fj' in string: + print(string) + index = strings.index(string) + strings[index] = '' + +strings = ['asfsfsf', 'bldfjgld', 'slfgsdfhgsidf', 'oertuwoehnccxvnm'] +index = 0 +for string in strings: + if 'sid' in string: + print(string) + strings[index] = '' +index += 1 + + +strings = ['asfsfsf', 'bldfjgld', 'slfgsdfhgsidf', 'oertuwoehnccxvnm'] +# dict是具有(key,value)的 而类似的enumerate()函数可以返回序列的(index,value) +for index, string in enumerate(strings): + if 'nm' in string: + print(string) + strings[index] = '' + + + + +for n in range(99,81,-1): + res = sqrt(n) + if res == int(res): # 判断是否是整数 + print(n) + break +else: + print("nothing find") + +# 列表推导式,小小的重点一下 +print([x**2 for x in range(0, 100)]) +print([x for x in range(0, 100) if x % 2 == 0]) +# 下面这句话注意一点,这个可不是并行迭代,而是实现了多重循环的效果,找找笛卡尔积的感觉 +print([x+y for x in range(0, 100) for y in range(100, 0, -1)]) + + +# ok, that's all + + diff --git a/04/decorator/01.py b/04/decorator/01.py new file mode 100644 index 0000000..b9873b9 --- /dev/null +++ b/04/decorator/01.py @@ -0,0 +1,11 @@ +# 随便写的一个求和函数 +def calc(a, b): + res = 0 + for i in range(a,b): + res += i + return res + + +if __name__ == '__main__': + a = calc(1, 101) + print(a) diff --git a/04/decorator/02.py b/04/decorator/02.py new file mode 100644 index 0000000..471f48f --- /dev/null +++ b/04/decorator/02.py @@ -0,0 +1,14 @@ +# 修改需求,现在每个函数在完成自己的功能之前,要先行打印字符串JRT +# 最简单的,修改函数定义即可 + + +def calc(a, b): + print('JRT') + res = 0 + for i in range(a, b): + res += i + return res + + +if __name__ == "__main__": + print(calc(1, 101)) diff --git a/04/decorator/03.py b/04/decorator/03.py new file mode 100644 index 0000000..1694866 --- /dev/null +++ b/04/decorator/03.py @@ -0,0 +1,24 @@ +# 可是刚才那么做是不是有点麻烦呀,函数多的话总不能一个一个修改 +# 所以我们可以写一个新函数 + + +def calc(a, b): + res = 0 + for i in range(a,b): + res += i + return res + + +def add(a, b): + return a + b + + +def func(f, a, b): + print("JRT") + return f(a, b) + + +if __name__ == '__main__': + print(func(calc, 1, 101)) + print(func(add, 1, 100)) + diff --git a/04/decorator/04.py b/04/decorator/04.py new file mode 100644 index 0000000..9547847 --- /dev/null +++ b/04/decorator/04.py @@ -0,0 +1,24 @@ +# 然而上次的做法本质上已经是定义新函数了,并不是我们想要的做法 + + +def calc(a, b): + res = 0 + for i in range(a, b): + res += i + return res + +# 定义装饰器 + + +def func(f): + def newF(a, b): + print('JRT') + return f(a, b) + return newF + + +# 应用装饰器 +calc = func(calc) + +if __name__ == '__main__': + print(calc(1, 101)) diff --git a/04/decorator/05.py b/04/decorator/05.py new file mode 100644 index 0000000..4dc2050 --- /dev/null +++ b/04/decorator/05.py @@ -0,0 +1,23 @@ +# Python也提供了自动装饰的语法糖 +# 定义装饰器 + + +def func(f): + def newF(a, b): + print('JRT') + return f(a, b) + return newF + + +# 使用Python为装饰器提供的语法糖,@后面是定义的装饰器,下一行紧跟要装饰的函数 +# Python会自动把待修饰函数做参数给装饰器,然后用装饰器返回的新函数覆盖待装饰的函数 +@func +def calc(a, b): + res = 0 + for i in range(a, b): + res += i + return res + + +if __name__ == '__main__': + print(calc(1, 101)) diff --git a/04/tkinter/01.py b/04/tkinter/01.py new file mode 100644 index 0000000..e73d78d --- /dev/null +++ b/04/tkinter/01.py @@ -0,0 +1,22 @@ +import tkinter as tk +status = False +window = tk.Tk("my first window") +window.geometry('200x150') +var = tk.StringVar() +lb = tk.Label(window, textvariable=var, bg='red', font=('Arial', 12), width=15, height=2) +lb.pack() + + +def on_click(): + global status + if not status: + status = True + var.set("I am clicked") + else: + status = False + var.set("") + +bt = tk.Button(window, text='click me', width=15, height=2, command=on_click) +bt.pack() +window.mainloop() # 进入主循环 + diff --git a/04/tkinter/02.py b/04/tkinter/02.py new file mode 100644 index 0000000..c71490e --- /dev/null +++ b/04/tkinter/02.py @@ -0,0 +1,23 @@ +# 想法来自网络 +import tkinter as tk +window = tk.Tk('My Window') +window.geometry('700x500') +entry = tk.Entry(window, show=None, width=30) # 可以通过show=''来指定掩码 +entry.pack() + + +def add2point(): + t.insert('insert', entry.get()) # insert模式用于在光标处插入 + + +def add2end(): + t.insert('end', entry.get()) # end模式用于在尾部插入, 可以通过m.n模式来指定插入的行和列 + +bt1 = tk.Button(window, text='add to the point', bg='blue', width=15, height=2, command=add2point) +bt1.pack() +bt2 = tk.Button(window, text='add to the end', bg='green', width=15, height=2, command=add2end) +bt2.pack() +t = tk.Text(window, width=30, height=10) +t.pack() +window.mainloop() + diff --git a/04/tkinter/03.py b/04/tkinter/03.py new file mode 100644 index 0000000..15119eb --- /dev/null +++ b/04/tkinter/03.py @@ -0,0 +1,31 @@ +import tkinter as tk +window = tk.Tk("My Window") +window.geometry('200x300') +var1 = tk.StringVar() +label = tk.Label(window, textvariable=var1, bg='red', width=30, height=2) +label.pack() +# print(type(var1)) + + +# 下面是按钮的回调函数 +def print2label(): + global var1 # 使用全局作用域的变量 + res = lb.get(lb.curselection()) # 获取光标指向的item + var1.set(res) +button = tk.Button(window, text='print to the label', command=print2label, width=30, height=2) +button.pack() +var2 = tk.StringVar() # var2是放在列表框当中的 +var2.set((1, 2, 3, 4)) # 元组参数 + + +lb = tk.Listbox(window, listvariable=var2) +lb.pack() +tuple_test = ['J', 'R', 'T'] +for item in tuple_test: + lb.insert('end', item) +lb.insert(1, 'JRT') +lb.insert(2, 'abc') +lb.delete(2) +window.mainloop() + + diff --git a/04/tkinter/04.py b/04/tkinter/04.py new file mode 100644 index 0000000..9eed928 --- /dev/null +++ b/04/tkinter/04.py @@ -0,0 +1,19 @@ +import tkinter as tk +window = tk.Tk("My window") +window.geometry('500x400') +lb = tk.Label(window, width=30, height=2, bg='green') +lb.pack() +var = tk.StringVar() + + +def print2label(): + lb.config(text="%s was selevted" % var.get()) # 重新配置lb的属性 + +rb1 = tk.Radiobutton(window, command=print2label, variable=var, value='button1', text='button1') +rb1.pack() +rb2 = tk.Radiobutton(window, command=print2label, variable=var, value='button2', text='button1') +rb2.pack() +rb3 = tk.Radiobutton(window, command=print2label, variable=var, value='button3', text='button1') +rb3.pack() +window.mainloop() + diff --git a/04/tkinter/05.py b/04/tkinter/05.py new file mode 100644 index 0000000..a909da8 --- /dev/null +++ b/04/tkinter/05.py @@ -0,0 +1,7 @@ +# 尚未完成 +from tkinter import * +# from math import * +window = Tk() +window.title("计算器") +window.geometry('1200x260') +window.mainloop() diff --git a/README.md b/README.md index 0b7da0d..7112ae5 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,9 @@ 这个文件了定义了换行格式字符编码。一般的代码编辑器都可以识别 -[GitHub用法](./blob/master/01/GithubTutorial.md) +[Git用法](./01/Git-Tutorial.md) + +[GitHub用法](./01/GithubTutorial.md) ##### 关于比较常见的python 代码头部 ```python @@ -57,7 +59,7 @@ or *** ## 目录 -1. :常量,变量,运算符,类型,内置函数 -2. :流程控制,表达式,语句,模块,导入 -3. :函数,面向对象 -4. 欢迎贡献 +1. 常量,变量,运算符,内置函数,数据类型 +2. 流程控制,导入 +3. 函数和面向对象基础 +4. 模块和高级专题 diff --git "a/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/data_init.py" "b/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/data_init.py" new file mode 100644 index 0000000..bdb158b --- /dev/null +++ "b/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/data_init.py" @@ -0,0 +1,5 @@ +# 初始化数据库 +def data_init(data): + data['first_name'] = {} + data['last_name'] = {} + diff --git "a/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/data_sort.py" "b/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/data_sort.py" new file mode 100644 index 0000000..9116aa9 --- /dev/null +++ "b/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/data_sort.py" @@ -0,0 +1,9 @@ +def data_sort(data, label, name): + """ + :param data: 数据库名称 + :param label: 查找方式,姓还是名 + :param full_name: 对应于查找方式,输入要查找的名字中的字段 + :return: nothing + + """ + return data[label].get(name) # dict.get(key),返回对应的value diff --git "a/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/data_store.py" "b/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/data_store.py" new file mode 100644 index 0000000..c762ce6 --- /dev/null +++ "b/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/data_store.py" @@ -0,0 +1,18 @@ +# 数据存储 +# 包导入的路径问题很坑啊 +# pycharm把根目录当成项目所在路径 +from data_sort import data_sort + + +def data_store(data, full_name): + labels = ['first_name', 'last_name'] + names = full_name.split() + # 注意存储名字到数据库的时候,要在姓和名的两个键下面都存储好相应的全名 + for label, name in zip(labels, names): + result = data_sort(data, label, name) # 获取全名列表 + if result: + result.append(full_name) + else: + data[label][name] = [full_name] + + pass diff --git "a/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/main.py" "b/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/main.py" new file mode 100644 index 0000000..7aaae44 --- /dev/null +++ "b/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/main.py" @@ -0,0 +1,16 @@ +from data_init import data_init +from data_store import data_store +from data_sort import data_sort +data = {} +data_init(data) +# print(data) +data_store(data, "关键字 参数") +data_store(data, "J RT") +data_store(data, "J AA") +data_store(data, "B RT") +full_name = input("输入全名,注意空格分隔开姓和名\n") +data_store(data, full_name) +label = input("输入要first_name 或者 last_name来指定查找的字段\n") +name = input("输入要查找字段:%s的值?\n" % label) +result = data_sort(data, label, name) +print(result) diff --git "a/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/readme.md" "b/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/readme.md" new file mode 100644 index 0000000..4fd4a29 --- /dev/null +++ "b/project/\345\247\223\345\220\215\346\243\200\347\264\242\347\263\273\347\273\237/readme.md" @@ -0,0 +1 @@ +# 这是一个稍微复杂的项目 diff --git a/test/.DS_Store b/test/.DS_Store new file mode 100644 index 0000000..a00871c Binary files /dev/null and b/test/.DS_Store differ diff --git a/test/UBWprayer/calculator2.py b/test/UBWprayer/calculator2.py new file mode 100644 index 0000000..21a135f --- /dev/null +++ b/test/UBWprayer/calculator2.py @@ -0,0 +1,17 @@ +a = float(input("Please input the first number:\n>")) +b = input("Please input operator:\n>") +if b == '^': + c = int(input("Please input a integer:\n>")) + print("The result is:", a ** c) +else: + c = float(input("Please input the second number:\n>")) + if b == '+': + print("The result is:", a + c) + elif b == '-': + print("The result is:", a - c) + elif b == '*': + print("The result is:", a * c) + elif b == '/': + print("The result is:", a / c) + else: + print("Please input +,-,*,/or^as an operator") diff --git a/test/UBWprayer/xingming.py b/test/UBWprayer/xingming.py new file mode 100644 index 0000000..3ce0b8c --- /dev/null +++ b/test/UBWprayer/xingming.py @@ -0,0 +1,66 @@ +# 变量定义部分 +xing_ming = {} +ming_xing = {} + +# 函数定义部分 +def menu(): + """显示主菜单""" + print('#' * 60) + print(' ' * 20,"欢迎使用姓名检索系统",' ' * 20) + print(' ' * 10,"1 .输入姓名",' '*18,"2 .查找姓名",' '*9) + print(' ' * 10,"3 .浏览姓名",' ' * 18,"4 .关闭程序",' ' * 9) + a = input(">") + if int(a) == 1: + append() + elif int(a) == 2: + find() + elif int(a) == 3: + show() + elif int(a) == 4: + return + +def append(): + print('#' * 60) + a = input(" 请输入姓氏(输入q退出):") + b = input(" 请输入名字(输入q退出):") + if a == 'q' or b == 'q': + menu() + elif str(a) not in xing_ming and str(b) not in ming_xing: + xing_ming[str(a)] = str(b) + ming_xing[str(b)] = str(a) + print("输入成功!") + else: + print("您输入的名或姓已经与某一已经存在的姓名重复了哦。") + input("请按回车键返回上一级") + menu() + return + +def find(): + print('#' * 60) + print(' ' * 20,"您要查找名还是姓?",' ' * 20) + print(' ' * 11,"1 .姓氏", ' ' * 22, "2 .名字", ' ' * 11) + print("输入q返回上一级") + a = input(">") + if int(a) == 1: + b = input("请输入您要查找的姓氏:") + if str(b) in xing_ming: + print("查找结果:",b,xing_ming[b]) + if int(a) == 2: + b = input("请输入您要查找的名字:") + if str(b) in ming_xing: + print("查找结果:",ming_xing[b],b) + input("请按回车键返回上一级") + menu() + return + +def show(): + print('#' * 60) + print(' ' * 20 ,"姓氏" , ' ' * 12,"名字") + for i in xing_ming: + print(' '*20,i,' '*14,xing_ming[i]) + input("按回车键返回上一级") + menu() + return + +# 函数调用 +menu() diff --git a/test/calculator-1(no condition).py b/test/calculator-1(no condition).py new file mode 100644 index 0000000..47d4591 --- /dev/null +++ b/test/calculator-1(no condition).py @@ -0,0 +1,2 @@ +cal_string = input("") +print(cal_string + ' =', eval(cal_string)) diff --git "a/test/caoshaun/caculator1 - \345\211\257\346\234\254.py" "b/test/caoshaun/caculator1 - \345\211\257\346\234\254.py" new file mode 100644 index 0000000..462a440 --- /dev/null +++ "b/test/caoshaun/caculator1 - \345\211\257\346\234\254.py" @@ -0,0 +1,19 @@ +x = float(input('请输入第一个数字')) +form = input('请输入运算符') +y = float(input('请输入第二个数字')) +if form == '+': + ans = x + y +elif form == '-': + ans = x - y +elif form == '*': + ans = x * y +elif form == '/': + if y == 0: + print('无法计算') + input() + exit() + else: + ans = x/y +elif form == '^' or '**': + ans = x ** y +print('答案为',ans) diff --git a/test/guono912/caculator.py b/test/guono912/caculator.py new file mode 100644 index 0000000..79c9239 --- /dev/null +++ b/test/guono912/caculator.py @@ -0,0 +1,20 @@ +print("输入算式,不同元素用空格隔开\n") +x,form,y=input().split() +x=float(x) +y=float(y) +if form is '+': + res=x+y +if form is '-': + res=x-y +if form is '*': + res=x*y +if form is '^': + res=x**y +if form is '/': + if y!=0: + res=x/y + else: + print("除数不能为零") + exit() +print(x,form,y,"=",res) +input() diff --git a/test/guono912/name_v1.py b/test/guono912/name_v1.py new file mode 100644 index 0000000..371a6ee --- /dev/null +++ b/test/guono912/name_v1.py @@ -0,0 +1,39 @@ +#读入txt文件,分姓名保存 +xing = [] +ming = [] +with open('name.txt') as f: + for line1 in f: + x , m = line1.split() + xing.append(x) + ming.append(m) +#遍历查找 +print('输入查找类型') +print('按姓查找输入1,按名输入2,按姓名查找输入3') +i = input() +print('请输入查找目标') +ser = input() +num = 0 +if i == '1': + for j in range(0,len(xing)): + if ser == xing[j]: + print(xing[j],ming[j]) + num = num+1 + if num == 0: + print('没有结果') +elif i == '2': + for j in range(0,len(ming)): + if ser == ming[j]: + print(xing[j],ming[j]) + num = num+1 + if num == 0: + print('没有结果') +elif i == '3': + x , m=ser.split() + for j in range(0,len(xing)): + if x == xing[j] and m == ming[j]: + print(xing[j],ming[j]) + num = num+1 + if num == 0: + print('没有结果') +else: + print('输入不合法') diff --git a/test/huge-star(fanxilai)/Caculator_By_fxl.py b/test/huge-star(fanxilai)/Caculator_By_fxl.py new file mode 100644 index 0000000..d7f1a0e --- /dev/null +++ b/test/huge-star(fanxilai)/Caculator_By_fxl.py @@ -0,0 +1,77 @@ +# exp = raw_input("Please input your expression >>> ") +operator = ['^', '/', '*', '-', '+'] +# exp = str(input("Please input your expression >>> ")) +# exp = exp.split(' ') +# j = 0 +# for i in range(len(exp)): +# if exp[i].isdigit(): +# dgt[j] = exp.pop(0) +# j += 1 +# elif exp[i] in operator: +# op[k] = exp.pop(0) +# k += 1 +# +# +# exp = str(input("Please input your expression >>> ")).split() +# while (len(exp) - 1): +# while '^' in exp: +# index = exp.index('^') +# exp.pop(index) +# exp.insert(index - 1, int(exp.pop(index - 1)) ** int(exp.pop(index - 1))) + +# 以上都是废代码 +operator = ['^', '/', '*', '-', '+'] # 定义一个列表来存储符号,方便下面用循环。 +def compute(exp): + ''' + 以处理好的已经把运算符和数字分割好的、并以他们作为元素的列表作为输入,计算出所给表达式的值(不带括号) + :param exp: + :return: + ''' + while (len(exp) - 1): # 当表达式的值没有被撇的只剩一个的时候(见列表的pop()方法) + for op in iter(operator): # 从operator列表里按顺序取元素,注意operator列表中的元素顺序应是运算符的运算顺序。 + while op in exp: + index = exp.index(op) # 存储下要计算的预算符的位置 + exp.pop(index) + if op == '^': + exp.insert(index - 1, float(exp.pop(index - 1)) ** float(exp.pop(index - 1))) + # 这四个插入(insert)是同样的道理。拿出运算符左边的(exp.pop(index - 1))和右边的(exp.pop(index - 1)\ + # 并把他们强制转化为float(精度高)然后运算得出结果并把它插入到被撇掉的那三个的位置。 + if op == '/': + exp.insert(index - 1, float(exp.pop(index - 1)) / float(exp.pop(index - 1))) + if op == '*': + exp.insert(index - 1, float(exp.pop(index - 1)) * float(exp.pop(index - 1))) + if op == '-': + exp.insert(index - 1, float(exp.pop(index - 1)) - float(exp.pop(index - 1))) + if op == '+': + exp.insert(index - 1, float(exp.pop(index - 1)) + float(exp.pop(index - 1))) + # 这里的四个判断是迫不得已才写的,本来想直接用一个循环了事,但是那样的话,比如"float(exp.pop(index - 1)) + # + float(exp.pop(index - 1)"中的运算符就没办法更改,所以最后我只想到了判断这一种方法。 + return exp[0] + +def recur_parenth(exp): + ''' + 通过递归来一层层(从最外层到最里层)去括号,然后再一层层(从最里层到最外层)把值返回回来。 + :param exp: + :return: + ''' + if '(' in exp: + index1 = exp.index('(') + exp.pop(index1) + exp.reverse() + index2 = len(exp) - exp.index(')') - 1 + exp.reverse() + exp.pop(index2) # 这些步骤主要是找到一对儿括号并用index1和index2标记出他们的位置,然后从exp中撇掉'('和')',其中reverse()也是\ + # 想不出别的好方法的无奈之举。 + exp[index1:index2] = [recur_parenth(exp[index1:index2])] # 截取原来包含在括号中的部分,并计算它们的值 + return compute(exp) + return compute(exp) # 递归出口,当exp没有括号只有表达式时,返回该表达式之值。 + + + +exp = str(input("Please input your expression with spaces to separate your numbers from operator >>> ")).split() +# 输入表达式并用字符串的split()方法把他们分隔开(split()方法的默认参数是split(' '),故省略) +print(recur_parenth(exp)) + +# 这个程序有一个巨大的缺陷,就是必须要用空格来区分开符号与数字,这样的话就十分的僵硬,慢慢儿改进把。 +# 还有一个缺陷是算不了太大的数 +# 另一个缺陷是没有错误提醒 diff --git a/test/huge-star(fanxilai)/Index_S.py b/test/huge-star(fanxilai)/Index_S.py new file mode 100644 index 0000000..c94e794 --- /dev/null +++ b/test/huge-star(fanxilai)/Index_S.py @@ -0,0 +1,9 @@ +def index_sort(l): + o = [] + m = list(zip(l, range(3))) + m.sort() + for (i, value) in m: + o.append(value) + return o + +print(index_sort([12,56,33,78])) diff --git a/test/huge-star(fanxilai)/Name_Store.py b/test/huge-star(fanxilai)/Name_Store.py new file mode 100644 index 0000000..52b2a1f --- /dev/null +++ b/test/huge-star(fanxilai)/Name_Store.py @@ -0,0 +1,74 @@ +def name_input(): + ''' + Enter white space seoarated name and return the tuple (first name, last name) + :return: tuple (first name, last name) + ''' + flag = 1 + while flag: + try: + [fn, ln] = input("\nPlease enter the name >>> ").split() + flag = 0 + except: + print("\nPlease check your name") + return fn.lower(), ln.lower() # 用于不区分大小写 + + +def choose_action(): + ''' + print instructions on the screen and return the action user chooose + :return: int + ''' + while 1: + tag = int(input("\nWhat would you like to do?\n0. quit\n1. record\n2. search\n>>> ")) + if tag == 0 or tag == 1 or tag == 2: + return tag + else: + print("\nPlease enter the right number!") + + +def search_input(): + ''' + 用来输入要搜索的名字 + :return: tuple (name, tag) + ''' + tag = 1 # 用来标记动作 + while tag: + tag = int(input("\nWhich name do you want to search?\n0. quit\n1. first name\n2. last name\n>>> ")) + if not tag: + return '', tag + name = input("\nPlease enter the name >>> ") + return name, tag + + +def search_name(name, tag): + result = set({}) # 新建一个空集合来存放结果 + global name_book + for n in name_book: + if n[tag-1] == name.lower(): # 不区分大小写 + result.add(n) + return result + + +def print_set(result): + ''' + 搜索结果是一个set,用来打印搜索结果(set) + :param result: set + :return: void + ''' + for i in result: + print("%s %s" % (i[0], i[1])) + + +def start_program(): + flag = 1 # 用来标记程序结束 + name_book = set() # 这里用set是因为我想不到什么用字典的好方法了,就蒙了一个集合,在网上一找,发现python还真有这个内建数据类型,于是就很开心的使用了 + while flag: + flag = choose_action() # 同时用来标记应该进行的动作 + if flag == 1: + name_book.add(name_input()) # add是set类型的一个方法,添加元素到集合 + elif flag == 2: + i = search_input() + if i[1] == 0: + continue; + print_set(search_name(i[0], i[1])) + diff --git a/test/indexSort.py b/test/indexSort.py new file mode 100644 index 0000000..cdd0369 --- /dev/null +++ b/test/indexSort.py @@ -0,0 +1,24 @@ +def index_sort1(seq): + seq = enumerate(seq) # 返回列表的索引-值对 + seq_index = [] + seq_value = [] + for i, j in seq: + seq_index.append(i) + seq_value.append(j) + return sorted(seq_index, key=lambda x: seq_value[x]) + + + +def index_sort2(seq): + seq = enumerate(seq) + seq_sorted = sorted(seq, key=lambda x: x[1]) + dict_sorted = dict(seq_sorted) + dict_key = dict_sorted.keys() + dict_value = dict_sorted.values() + return dict_key, dict_value +if __name__ == '__main__': + print(index_sort1((1, -2, 3, 99, 0, 22))) + # print(index_sort2((1, -2, 3, 99, 0, 22))) + a, b = index_sort2((1, -2, 3, 99, 0, 22)) + print(a) + print(b) diff --git "a/test/map\347\232\204\351\252\232\347\224\250\346\263\225.py" "b/test/map\347\232\204\347\224\250\346\263\225.py" similarity index 100% rename from "test/map\347\232\204\351\252\232\347\224\250\346\263\225.py" rename to "test/map\347\232\204\347\224\250\346\263\225.py"