标签:
python的介绍
?
????
?
????
?
????
?
python2与3的区别
Old:?print "The answer is",?2*2?
New:?print ("The answer is",?2*2)
?
Old:
New:
?
?
windows添加python2.7 和3.5环境变量
##2.7
?
OK
?
?
##3.5
##python.exe 重命名为python35.exe
?
OK
?
centos 6.6安装python2.7和3.5
yum groupinstall ‘Development Tools‘
tar xf Python-3.5.0.tgz
cd Python-3.5.0
./configure --prefix=/usr/local/python3
make all
make install
echo $?
make clean
make distclean
?
yum?groupinstall?‘Development?Tools‘
yum?install?zlib-devel?bzip2-devel??openssl-devel?ncurses-devel
2 下载 Python3.5代码包
wget??https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tar.xz
3 编译
tar?Jxvf??Python-3.5.0.tar.xz
cd?Python-3.5.0
./configure?--prefix=/usr/local/python3
make?&&?make?install
?
4.此时,新版本的python没有覆盖原来的版本,先将原来的python重命名
# mv /usr/bin/python ?/usr/bin/python_old
?
5 替换系统默认的2.6版本的python
ln -sv /usr/local/python3/bin/python3.5 /usr/bin/python
##这样做的目的是在系统任意目录敲入python调用的是python3的命令,而非系统默认2.6.6的
但是这样同时这会导致依赖python2.6的yum不能使用,因此还要修改yum配置。
6 更新yum配置。
vim?/usr/bin/yum
通过vim修改yum的配置
#!/usr/bin/python
改为
#!/usr/bin/python2.6
?
保存退出。完成了python3的安装。
?
?
7 安装pip
pip是根据官网wiki安装的。
1,安装setuptools。pip安装前需要先安装setuptools,在上面的页面中给出了下载地址。下载并执行即可:
wget?https://pypi.python.org/packages/source/s/setuptools/setuptools-5.7.zip?--no-check-certificate
unzip?setuptools-5.7.zip?
cd setuptools-5.7
python setup.py install
?
wget https://pypi.python.org/packages/source/p/pip/pip-7.1.2.tar.gz#md5=3823d2343d9f3aaab21cf9c917710196
tar xvf pip-7.1.2.tar.gz
cd pip-7.1.2
python setup.py install
?
OK 大功告成
?
?
python简单示例
?
1.变量,字符集,头文件
?
变量定义的规则
?
????
?
?
定义字符集
????
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: liwx
?
?
?
#..最多不能超过80个字符每行...
?
?
自动添加头文件
?
2.菜单内引用变量
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: liwx
name = input("input your name:")
age = int(input("input your age:") )
job = input("input your job:")
msg = ‘‘‘
Infomation of user %s:
-------------------
Name: %s
Age : %f
Job : %s
--------END--------
‘‘‘ % (name,name,age,job)
print(msg)
?
执行结果
[root@liwx python]# python caidan_bianliang.py
input your name:liwx
input your age:28
input your job:IT
?
infomation of user liwx:
---------------------------
name: liwx
age : 28
job : IT
--------------END----------
?
?
导入模块
[root@liwx python]# vim 2.py
import getpass
username = input("please input username:")
passwd = getpass.getpass("please input passwd:")
?
print(username,passwd)
?
?
执行结果(密码看不见,是密文)
?
?
?
?
[root@liwx python]# cat 3.py
import os
os.system("ls")
os.system("df -h")
os.mkdir("/root/1234444")
print ("---------------------")
os.system("ls /root/")
?
结果
[root@liwx python]# python 3.py
2.py 3.py caidan_bianliang.py
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 7.1G 1.8G 5.0G 26% /
tmpfs 242M 0 242M 0% /dev/shm
/dev/sda1 190M 27M 153M 15% /boot
---------------------
123 install.log.syslog setuptools-5.7
1234444 nux-dextop-release-0-2.el6.nux.noarch.rpm setuptools-5.7.zip
anaconda-ks.cfg Python-3.5.0 youhua.sh
install.log Python-3.5.0.tgz
?
把返回的结果赋值给变量
>>> cmd_res = os.popen("df -h").read()
?
>>> print cmd_res
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 7.1G 1.4G 5.4G 21% /
tmpfs 242M 0 242M 0% /dev/shm
/dev/sda1 190M 27M 153M 15% /boot
?
>>> print(cmd_res)
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 7.1G 1.4G 5.4G 21% /
tmpfs 242M 0 242M 0% /dev/shm
/dev/sda1 190M 27M 153M 15% /boot
?
?
##查看python的环境变量
>>> import sys
>>> print(sys.path)
[‘‘, ‘/usr/lib64/python26.zip‘, ‘/usr/lib64/python2.6‘, ‘/usr/lib64/python2.6/plat-linux2‘, ‘/usr/lib64/python2.6/lib-tk‘, ‘/usr/lib64/python2.6/lib-old‘, ‘/usr/lib64/python2.6/lib-dynload‘, ‘/usr/lib64/python2.6/site-packages‘, ‘/usr/lib/python2.6/site-packages‘]
?
?
?
?
Tab 补全
?
# python startup file
import sys
import readline
import rlcompleter
import atexit
import os
# tab completion
readline.parse_and_bind(‘tab: complete‘)
# history file
histfile = os.path.join(os.environ[‘HOME‘], ‘.pythonhistory‘)
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter
?
?
?
?
>>> import tab.py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python3/lib/python3.5/site-packages/tab.py", line 4, in <module>
import readline
ImportError: No module named ‘readline
?
?
?
?
?
?
?
if 判断
passwd = "liwx123"
?
username = raw_input("username:")
password = raw_input("passwd:")
?
if user == username:
print("username is OK")
if passwd == password:
print("Welcome login .....")
else:
print("passwd is NONONO")
else:
print("username is NONONONO")
?
优化版
[root@liwx ~]# python if_user.py
username:liwx
passwd:liwx123
username is OK
Welcome login .....
?
[root@liwx ~]# python if_user.py
username:liwx
passwd:sfdsf
username is OK
passwd is NONONO
?
[root@liwx ~]# python if_user.py
username:sdf
passwd:sdf
username is NONONONO
?
猜年龄
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: liwx
age = 22
counter = 0
for i in range(10):
if counter <3:
guess_num = int(input("input your guess num:"))
if guess_num == age:
print("you guess is OK")
break #跳出整个循环
elif guess_num > age:
print("you guess is big")
else:
print("you guess is small")
else:
please_confirm = input("NUM > 3,do you want continue (y/n) :")
if please_confirm == ‘y‘:
counter = 0
continue #跳出当次循环,不计数
else:
print("bye bye")
break
counter += 1 #counter = counter + 1
?
?
?
?
?
?
while true
?
if 用户名不在白名单
????????没有这个用户
else
????通过
????if 用户名在白名单 and 密码相同
????确实登录
????else:
????????
????????
????
if 用户名在白名单
????确认登录
或者 用户名相同 and 密码不同
????密码错误,
????请重新登录
????if 登录三次失败
????????拒绝访问
?
?
?
?
?
?
???? ?
?
?
?
?
?
?
?
????python的基本数据类型有5种
????????整型:????????int
????????浮点型:????????float
????????字符型:????????string
????????布尔型:????????bool
????????空值:????????None
?
运算
?
?
?
格式化
>>> ‘Hi, %s, you have $%d.‘
‘Hi, %s, you have $%d.‘
>>> ‘Hi, %s, you have $%d.‘ %(‘Michael‘,10000)
‘Hi, Michael, you have $10000.‘
?
?
补齐
>>> ‘%2d-%02d‘ % (3, 1)
‘ 3-01‘
>>> ‘%d-%03d‘ % (3, 1)
‘3-001‘
>>> ‘%d-%d‘ % (3, 1)
‘3-1‘
?
?
>>> ‘%.2f‘ % 3.1415926
‘3.14‘
>>> ‘%.f‘ % 3.1415926
‘3‘
>>> ‘%.4f‘ % 3.1415926
‘3.1416‘
?
如果你不太确定应该用什么, %s 永远起作用,它会把任何数据类型转换为字符串
>>> ‘Age: %s. Gender: %s‘ % (25, True)
‘Age: 25. Gender: True‘
?
>>> u‘Hi, %s‘ % u‘Michael‘
u‘Hi, Michael‘
?
>>> ‘growth rate: %d %%‘ % 7
‘growth rate: 7 %‘
?
?
?
程序时一定要
尽最大的可能避免出现重复代码, 这是专业程序员需具备的基本道德之一, 为什
么呢?重复代码有以下害处:
1. 使程序变的冗长、笨重
2. 使程序变的不易维护,当你的程序出现多处重复代码时,如果突然你需要对
一个功能做更改,但这个功能又在程序中重复了多次,那你就必须在各个重
复的地方把修改这个功能,太恶心了。计算机语言支持函数、类功能的原因
之一就是要减少代码的冗余。
?
?
标签:
原文地址:http://www.cnblogs.com/xuege/p/5491885.html