提交 90d1528f 编写于 作者: 骆昊的技术专栏's avatar 骆昊的技术专栏

更新了部分代码

上级 e2a7bbde
...@@ -47,12 +47,14 @@ Linux环境自带了Python 2.x版本,但是如果要更新到3.x的版本, ...@@ -47,12 +47,14 @@ Linux环境自带了Python 2.x版本,但是如果要更新到3.x的版本,
安装依赖库(因为没有这些依赖库可能在源代码构件安装时因为缺失底层依赖库而失败)。 安装依赖库(因为没有这些依赖库可能在源代码构件安装时因为缺失底层依赖库而失败)。
```Shell ```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 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源代码并解压缩到指定目录。 下载Python源代码并解压缩到指定目录。
```Shell ```Shell
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
xz -d Python-3.6.1.tar.xz xz -d Python-3.6.1.tar.xz
tar -xvf Python-3.6.1.tar tar -xvf Python-3.6.1.tar
...@@ -61,14 +63,38 @@ tar -xvf Python-3.6.1.tar ...@@ -61,14 +63,38 @@ tar -xvf Python-3.6.1.tar
切换至Python源代码目录并执行下面的命令进行配置和安装。 切换至Python源代码目录并执行下面的命令进行配置和安装。
```Shell ```Shell
cd Python-3.6.1 cd Python-3.6.1
./configure --prefix=/usr/local/python3.6 --enable-optimizations ./configure --prefix=/usr/local/python3.6 --enable-optimizations
make && make install 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 ```Shell
ln -s /usr/local/python3.6/bin/python3 /usr/bin/python3 ln -s /usr/local/python3.6/bin/python3 /usr/bin/python3
``` ```
...@@ -84,11 +110,13 @@ MacOS也是自带了Python 2.x版本的,可以通过[Python的官方网站](ht ...@@ -84,11 +110,13 @@ MacOS也是自带了Python 2.x版本的,可以通过[Python的官方网站](ht
在终端或命令行提示符中键入下面的命令。 在终端或命令行提示符中键入下面的命令。
```Shell ```Shell
python --version python --version
``` ```
当然也可以先输入python进入交互式环境,再执行以下的代码检查Python的版本。 当然也可以先输入python进入交互式环境,再执行以下的代码检查Python的版本。
```Python ```Python
import sys import sys
print(sys.version_info) print(sys.version_info)
...@@ -100,6 +128,7 @@ print(sys.version) ...@@ -100,6 +128,7 @@ print(sys.version)
可以用文本编辑工具(推荐使用Sublime、Atom、TextMate、VSCode等高级文本编辑工具)编写Python源代码并将其命名为hello.py保存起来,代码内容如下所示。 可以用文本编辑工具(推荐使用Sublime、Atom、TextMate、VSCode等高级文本编辑工具)编写Python源代码并将其命名为hello.py保存起来,代码内容如下所示。
```Python ```Python
print('hello, world!') print('hello, world!')
``` ```
...@@ -108,6 +137,7 @@ print('hello, world!') ...@@ -108,6 +137,7 @@ print('hello, world!')
切换到源代码所在的目录并执行下面的命令,看看屏幕上是否输出了"hello, world!"。 切换到源代码所在的目录并执行下面的命令,看看屏幕上是否输出了"hello, world!"。
```Shell ```Shell
python hello.py python hello.py
``` ```
...@@ -119,6 +149,7 @@ python hello.py ...@@ -119,6 +149,7 @@ python hello.py
2. 多行注释 - 三个引号开头,三个引号结尾 2. 多行注释 - 三个引号开头,三个引号结尾
```Python ```Python
""" """
第一个Python程序 - hello, world! 第一个Python程序 - hello, world!
...@@ -212,8 +243,9 @@ PyCharm的安装、配置和使用我们在后面会进行介绍。 ...@@ -212,8 +243,9 @@ PyCharm的安装、配置和使用我们在后面会进行介绍。
1. 在Python交互环境中下面的代码查看结果并将内容翻译成中文。 1. 在Python交互环境中下面的代码查看结果并将内容翻译成中文。
```Python ```Python
import this import this
Beautiful is better than ugly. Beautiful is better than ugly.
Explicit is better than implicit. Explicit is better than implicit.
Simple is better than complex. Simple is better than complex.
...@@ -238,8 +270,9 @@ PyCharm的安装、配置和使用我们在后面会进行介绍。 ...@@ -238,8 +270,9 @@ PyCharm的安装、配置和使用我们在后面会进行介绍。
2. 学习使用turtle在屏幕上绘制图形。 2. 学习使用turtle在屏幕上绘制图形。
```Python ```Python
import turtle import turtle
turtle.pensize(4) turtle.pensize(4)
turtle.pencolor('red') turtle.pencolor('red')
turtle.forward(100) turtle.forward(100)
......
#!/usr/bin/python3 #!/usr/bin/python3
from datetime import datetime from datetime import datetime
import sys import sys
...@@ -7,34 +8,31 @@ def is_leap(year): ...@@ -7,34 +8,31 @@ def is_leap(year):
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0 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(): def main():
if len(sys.argv) > 1: if len(sys.argv) == 3:
month = int(sys.argv[1]) month = int(sys.argv[1])
year = int(sys.argv[2]) year = int(sys.argv[2])
else: else:
current_date = datetime.now() now = datetime.now()
year = current_date.year date = now.date
month = current_date.month month = now.month
year2 = year if month >= 3 else year - 1 year = now.year
c = year2 // 100 m, y = (month, year) if month >= 3 else (month + 12, year - 1)
y = year2 % 100 c, y = y // 100, y % 100
m = month if month >= 3 else month + 12 w = (y + y // 4 + c // 4 - 2 * c + 26 * (m + 1) // 10) % 7
w = y + y // 4 + c // 4 - 2 * c + 26 * (m + 1) // 10 month_words = [
w %= 7 'January', 'February', 'March', 'April', 'May', 'June',
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'
'July', 'August', 'September', 'October', 'November', 'December'] ]
print(f'{months[month - 1]} {year}'.center(20)) print(f'{month_words[month - 1]} {year}'.center(20))
print('Su Mo Tu We Th Fr Sa') print('Su Mo Tu We Th Fr Sa')
print(' ' * 3 * w, end='') print(' ' * 3 * w, end='')
total_days = get_days(year, month) days = [
for day in range(1, total_days + 1): [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
print(f'{day}'.rjust(2), end=' ') [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 w += 1
if w == 7: if w == 7:
print() print()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册