一.安装ipython

1.升级pyton到2.7.0以上

[root@python ~]# yum install gcc gcc-c++ make -y
[root@python ~]# yum install zlib-devel openssl-devel readline-devel sqlite-devel ncurses-devel -y
[root@python ~]# wget https://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
[root@python ~]# tar xf Python-2.7.5.tar.bz2
[root@python ~]# cd Python-2.7.5
[root@python Python-2.7.5]# ./configure --prefix=/usr/local/python2.7.5
[root@python Python-2.7.5]# make && make install
[root@python Python-2.7.5]# mv /usr/bin/python /usr/bin/python2.6.6.old
[root@python ~]# ln -s /usr/local/python2.7.5/bin/python2.7 /usr/bin/python

#修改yum配置
[root@python ~]# vim /usr/bin/yum
#!/usr/bin/python2.6            #改为python2.6
[root@python ~]# python 
Python 2.7.5 (default, Jul 10 2015, 10:34:08) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

2.安装ipython

[root@python ~]# wget https://pypi.python.org/packages/source/i/ipython/ipython-2.3.1.tar.gz#md5=2b7085525dac11190bfb45bb8ec8dcbf --no-check-certificate 
[root@python ~]# tar xf ipython-2.3.1.tar.gz 
[root@python ~]# cd ipython-2.3.1
[root@python ipython-2.3.1]# python setup.py install
[root@python ipython-2.3.1]# ln -s /usr/local/python2.7.5/bin/ipython /usr/bin/
[root@python ipython-2.3.1]# which ipython
/usr/bin/ipython
[root@python ~]# ipython 
Python 2.7.5 (default, Jul 10 2015, 10:34:08) 
Type "copyright", "credits" or "license" for more information.

IPython 2.3.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:

二.用户交互raw_iput

[root@python day1]# cat raw_input.py 
#!/usr/bin/env python
name = raw_input('Please input your name: ')
print 'your name is %s' % name

#执行脚本
[root@python day1]# python raw_input.py 
Please input your name: tom
your name is tom

if条件判断语句例子

#脚本内容如下
[root@python day1]# cat if.py 
#!/usr/bin/env python

name = raw_input('Please input your name: ').strip()    #这里的strip()的作用是去除前后的空格
age = raw_input('How old are you?: ').strip()
job = raw_input('What is your job?: ').strip()

#这里的"""是多行的写法
msg = """            
Information of %s as bellow:
	Name:\033[42;1m%s\033[0m        #添加相应的颜色
	Age: %s
	Job: %s """ % (name,name,age,job)        #引用多个变量
if int(age) >= 50:       #这里是由于上面的raw_input输入的是字符串,需要做转换才能正常比较
	print "You are too old!"
elif int(age) >= 30:
	print 'you are now in the middle age.'
else:
	if int(age) >= 20:
		print 'you are still very young!'
	else:
		print 'not adult'
print msg

#执行脚本
[root@python day1]# python if.py 
Please input your name: lyao  
How old are you?: 33
What is your job?: IT
you are now in the middle age.

Information of lyao as bellow:
	Name:lyao
	Age: 33
	Job: IT 

[root@python day1]# python if.py 
Please input your name: lyao
How old are you?: 19
What is your job?: IT
not adult

Information of lyao as bellow:
	Name:lyao
	Age: 19
	Job: IT 

[root@python day1]# python if.py 
Please input your name: lyao
How old are you?: 77
What is your job?: IT          
You are too old!

Information of lyao as bellow:
	Name:lyao
	Age: 77
	Job: IT

for循环语句例子

continue#跳出当前循环,提前进入下一轮循环,只能在循环中使用
break#跳出整个循环,只能在循环中使用

例子1:

#脚本内容
[root@python day1]# cat for1.sh 
#!/usr/bin/env python

for i in xrange(100):    #xrange(100)表示从0开始打印到99,需要哪个值的时候就将值加载到内存
	if i == 50:
		print '\nI have got to the round 50th!'    #\n换行
	else:
		print i,    #逗号表示不换行

#执行脚本		
[root@python day1]# python for1.sh 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 
I have got to the round 50th!
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

例子2:

#脚本内容
[root@python day1]# cat for2.py 
#!/usr/bin/env python

for i in xrange(100):
	if i == 50:
		print '\nI have got to rounnd 50th!'
		continue        #当i=50的时候,提前进入下一轮循环,不执行后面的print i语句
	elif i > 70:
		break        #当i>70的时候终止循环
	print i,
[root@python day1]# python for2.py 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 
I have got to rounnd 50th!
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

while循环语句

例子1:

[root@python day1]# cat while1.py 
#/usr/bin/env python

i = 0
while True:
	i = i + 1
	if i == 50:
		print 'I have got to the round 50th!'
		continue
	elif i > 70:break
	print i,
[root@python day1]# python while1.py 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 I have got to the round 50th!
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

例子2:

#脚本内容如下
[root@python day1]# cat login.py 
#/usr/bin/env python

import sys

username = 'lyao'
password = '123456'
locked = 1
retry_counter = 0

while True:
	user = raw_input('Username: ').strip()    #去除前后的空格
	if len(user) == 0:    #判断用户名是否为空,如果为空提前进入下一轮循环
		print '\033[31;1mUsername can not be empty!\033[0m'
		continue
	passwd = raw_input('Password: ').strip()
	if len(passwd) == 0:
		print '\033[31;1mPassword can not be empty!\033[0m'
		continue
	
	if locked == 0:        #判断用户是否锁定,这段代码在这里有点多余
		print 'Your username is locked!'
		sys.exit()
	else:
		if user == username and passwd == password:        #判断用户名和密码是否正确
			sys.exit('Welcome %s to login to our system' % user)
		retry_counter += 1    #计数器,达到3次会有相应动作
		if retry_counter == 3:    #达到3次退出
			sys.exit('\033[31;1mYour username is locked ,please try again tomorrow!\033[0m')
		else:    #打印还有几次机会
			print '\033[31;1mWrong username or password,you have %s name chances!\033[0m' % (3 - retry_counter)