1.Linux Python环境
Linux系统一般集成Python,如果没有安装,可以手动安装,联网状态下可直接安装。Fedora下使用yum install,Ubuntu下使用apt-get install,前提都是root权限。安装完毕,可将Python加入环境变量,这样在使用Python时无须每次都指定安装路径。加入环境变量有两种方法:
方法1:直接在命令终端输入命令,立即生效,但重启后失效,如下。
export PATH="$PATH:/usr/bin/python"
方法2:在系统配置文件“/etc/profile”添加方法1中的命令,保存文件,重启生效,永久设置。
设置好环境变量后,在命令终端键入“python -v”可以查看Python的版本及相关信息,本人用的是Fedora18,已集成Python2.7,部分语句与Python3有差别,如下。
键入“python”即可进入Python解析器环境,可以直接执行Python语句,如下。
2.Python执行文件
与bash shell一样,可以直接执行Python程序文件。新建hello.py文件,键入如下内容并保存:
#!/usr/bin/pythonprint "Hello Acuity!"
修改hello.py执行权限:
chmod u+x hello.py
执行hello.py文件:
3.万年历
写个简易万年历,小试牛刀。
#!/usr/bin/python
# -*-coding:utf-8 -*-def leap_year(year):if year%4==0 and year%100!=0 or year%400==0:return Trueelse:return Falsedef get_month_days(year,month):days = 31if month == 2 :if leap_year(year):days=29else:days=28elif month==4 or month==6 or month==9 or month==11:days=30return daysdef get_total_days(year,month):total_days=0for i in range(1,year):if leap_year(i):total_days += 366else:total_days += 365for i in range(1,month):total_days +=get_month_days(year,i)return total_daysyear=input("The input query year:")
month = input("The input query month:")
i = 0
print "Sun\tMon\tTue\tWed\tThu\tFri\tSat"
j = 1
for j in range((get_total_days(year,month)%7)+1):print '\t',i += 1
for j in range(1,get_month_days(year,month)+1):print j,'\t',i +=1if i%7 == 0 :print ''
print '\t'
执行结果:



















