From 90d1528f30763cdabf742896ce66cd7c3d51b228 Mon Sep 17 00:00:00 2001 From: jackfrued Date: Wed, 25 Jul 2018 12:28:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Day01-15/Day01/初识Python.md | 39 ++++++++++++++++++++++++++++--- Day31-35/code/mycal.py | 42 ++++++++++++++++------------------ 2 files changed, 56 insertions(+), 25 deletions(-) mode change 100644 => 100755 Day31-35/code/mycal.py diff --git a/Day01-15/Day01/初识Python.md b/Day01-15/Day01/初识Python.md index 44f048b..e9dcbea 100644 --- a/Day01-15/Day01/初识Python.md +++ b/Day01-15/Day01/初识Python.md @@ -47,12 +47,14 @@ Linux环境自带了Python 2.x版本,但是如果要更新到3.x的版本, 安装依赖库(因为没有这些依赖库可能在源代码构件安装时因为缺失底层依赖库而失败)。 ```Shell + yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel ``` 下载Python源代码并解压缩到指定目录。 ```Shell + wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz xz -d Python-3.6.1.tar.xz tar -xvf Python-3.6.1.tar @@ -61,14 +63,38 @@ tar -xvf Python-3.6.1.tar 切换至Python源代码目录并执行下面的命令进行配置和安装。 ```Shell + cd Python-3.6.1 ./configure --prefix=/usr/local/python3.6 --enable-optimizations make && make install ``` -创建软链接,这样就可以直接通过python3直接启动Python解释器。 +配置PATH环境变量并使其生效,这需要修改用户主目录下名为.bash_profile的文件。 ```Shell + +cd ~ +vim .bash_profile +``` + +```Shell + +# 此处省略上面的代码 + +PATH=$PATH:/usr/local/python3.6/bin + +# 此处省略下面的代码 +``` + +```Shell + +source .bash_profile +``` + +最后还可以创建一个符号链接(如果不知道为什么也可以暂时不管这个问题啦)。 + +```Shell + ln -s /usr/local/python3.6/bin/python3 /usr/bin/python3 ``` @@ -84,11 +110,13 @@ MacOS也是自带了Python 2.x版本的,可以通过[Python的官方网站](ht 在终端或命令行提示符中键入下面的命令。 ```Shell + python --version ``` 当然也可以先输入python进入交互式环境,再执行以下的代码检查Python的版本。 ```Python + import sys print(sys.version_info) @@ -100,6 +128,7 @@ print(sys.version) 可以用文本编辑工具(推荐使用Sublime、Atom、TextMate、VSCode等高级文本编辑工具)编写Python源代码并将其命名为hello.py保存起来,代码内容如下所示。 ```Python + print('hello, world!') ``` @@ -108,6 +137,7 @@ print('hello, world!') 切换到源代码所在的目录并执行下面的命令,看看屏幕上是否输出了"hello, world!"。 ```Shell + python hello.py ``` @@ -119,6 +149,7 @@ python hello.py 2. 多行注释 - 三个引号开头,三个引号结尾 ```Python + """ 第一个Python程序 - hello, world! @@ -212,8 +243,9 @@ PyCharm的安装、配置和使用我们在后面会进行介绍。 1. 在Python交互环境中下面的代码查看结果并将内容翻译成中文。 ```Python + import this - + Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. @@ -238,8 +270,9 @@ PyCharm的安装、配置和使用我们在后面会进行介绍。 2. 学习使用turtle在屏幕上绘制图形。 ```Python + import turtle - + turtle.pensize(4) turtle.pencolor('red') turtle.forward(100) diff --git a/Day31-35/code/mycal.py b/Day31-35/code/mycal.py old mode 100644 new mode 100755 index 5738e87..8a5fc25 --- a/Day31-35/code/mycal.py +++ b/Day31-35/code/mycal.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 from datetime import datetime + import sys @@ -7,34 +8,31 @@ def is_leap(year): return year % 4 == 0 and year % 100 != 0 or year % 400 == 0 -def get_days(year, month): - days = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], - [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]] - return days[is_leap(year)][month - 1] - - def main(): - if len(sys.argv) > 1: + if len(sys.argv) == 3: month = int(sys.argv[1]) year = int(sys.argv[2]) else: - current_date = datetime.now() - year = current_date.year - month = current_date.month - year2 = year if month >= 3 else year - 1 - c = year2 // 100 - y = year2 % 100 - m = month if month >= 3 else month + 12 - w = y + y // 4 + c // 4 - 2 * c + 26 * (m + 1) // 10 - w %= 7 - months = ['January', 'February', 'March', 'April', 'May', 'June', - 'July', 'August', 'September', 'October', 'November', 'December'] - print(f'{months[month - 1]} {year}'.center(20)) + now = datetime.now() + date = now.date + month = now.month + year = now.year + m, y = (month, year) if month >= 3 else (month + 12, year - 1) + c, y = y // 100, y % 100 + w = (y + y // 4 + c // 4 - 2 * c + 26 * (m + 1) // 10) % 7 + month_words = [ + 'January', 'February', 'March', 'April', 'May', 'June', + 'July', 'August', 'September', 'October', 'November', 'December' + ] + print(f'{month_words[month - 1]} {year}'.center(20)) print('Su Mo Tu We Th Fr Sa') print(' ' * 3 * w, end='') - total_days = get_days(year, month) - for day in range(1, total_days + 1): - print(f'{day}'.rjust(2), end=' ') + days = [ + [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], + [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + ][is_leap(year)][month - 1] + for day in range(1, days + 1): + print(str(day).rjust(2), end=' ') w += 1 if w == 7: print()