###############################python###########################
1,安装
yum install python -y
2,查看python版本
[root@pxesever ~]# python -V
Python 2.7.5
3,应用
1)进入python交互界面
[root@pxesever iso]# python
Python 2.7.5 (default, Feb 11 2014, 07:46:25)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-13)] on linux2
Type "help", "copyright", "credits" or"license" for more information.
>>> a=1
>>> print a
1
>>>
-------->ctrl+D (退出)
2)脚本的方式
vim hello.py
#!/usr/bin/python
# coding:utf-8
# coding=utf-8
# encoding:utf-8
# encoding=utf-8
# 定义变量a,值为1
a=1
print a
执行方式:(2种)
###python hello.py
###chmod +x hello.py
./hello.py
4,具体知识点(查看编码类型:)
字符编码:
ASCII:1字节=8位,2^8=256
Unicode:2字节=16位,2^16=65536
a
-> 2字节
中文-> 2字节
utf-8:a -> 1字节,
中文 -> 3字节
GB2312:2字节
#####字符整形
int
##整型
long
##长整型
float
##浮点型
\t
tab
\n
换行
索引
切片 a[从哪里:到哪里:步长]
例:a[0:4:1]
内存读取:Unicode
存在硬盘:utf-8
编码<----->解码
字符的编码(enicode):unicode
->utf-8
字符的解码(decode):utf-8->unicode
示例:
[root@pxesever ~]# python
Python 2.7.5 (default, Feb 11 2014, 07:46:25)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-13)] on linux2
Type "help", "copyright", "credits" or"license" for more information.
>>> name_unicode = u"高手"
##进行定义字符
>>> type(name_unicode)
##查看字符类型
<type 'unicode'>
>>> name_utf8 = name_unicode.encode('utf-8')
##进行编码(在存储到硬盘时,为节省磁盘空间)
>>> type(name_utf8)
<type 'str'>
>>> name_unicode1 = name_utf8.decode('utf-8')
##进行解码(从磁盘中调到内存中需要解码)
>>> type(name_unicode1)
<type 'unicode'>
>>>
##编辑器:
vim
pycharm
markdown
git
svn
记事本:0xefbbbf<16>
3)软件Pycharm
添加头文件
#!/usr/bin/env python
#coding:utf-8
_author_ = "dwly"
'''
@author:dwly
@file:hello.py.py
@contact:xc_dwly@163.com
@time:6/24/1711:58 PM
@desc:
'''
######if else 语句#############
#!/usr/bin/env python
#coding:utf-8
_author_ = "dwly"
'''
@author:dwly
@file:hello.py.py
@contact:xc_dwly@163.com
@time:6/24/1711:58 PM
@desc:
'''
x = -1;
if x > 0:
print 'x是正数\n'
print x-10
elif x < 0:
print 'x是负数\n'
print x+10
else:
print 'x为零'
4)按格式输出
a = 1
b = 2
c = '''
hostname
a = %s
b = %s
'''%(a,b)
print c
示例:
主机信息:
#!/usr/bin/env python
#coding:utf-8
_author_ = "dwly"
'''
@author:dwly
@file:hostname.py.py
@contact:xc_dwly@163.com
@time:6/25/172:51 AM
@desc:
'''
hostname = raw_input('hostname:')
Ip = raw_input('Ip:')
used_year = input('user_year:')
CPU = raw_input('CPU:')
Memory = input('Memory:')
manager_name = raw_input('manger_name:')
if used_year>10:
print ('该服务器使用年限太久!')
else:
print '''
主机信息
hostname :%s
Ip :%s
used_year :%d
CPU :%s
Memory :%d
manager_name :%s
''' %(hostname,Ip,used_year,CPU,Memory,manager_name)
5)运算
赋值运算符:=,
+=, -=, /=,
%=, *=, **=
算术运算符:+, -,
*, /, //,
%, **
关系运算符:>, <, >=, ==, !=
返回值为布尔值
示例:
计算器:
#!/usr/bin/env python
#coding:utf-8
from __future__ import division
_author_ = "dwly"
'''
@author:dwly
@file:size.py.py
@contact:xc_dwly@163.com
@time:6/25/173:47 AM
@desc:
'''
num1 = input('num1:')
operate = raw_input('operate:')
num2 = input('num2:')
if operate == '+':
print 'num1 + num2'
print num1 + num2
elif operate == '-':
print 'num1 - num2'
print num1 - num2
elif operate == '*':
print 'num1 * num2'
print num1 * num2
elif operate == '/':
print 'num1 / num2'
print num1 / num2
else:
print 'please input rightvalue!'