3.进入python交互界面
[root@desktop31 ~]# 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.
>>> print "hello world!" ##输出hello world!
hello world!
>>> exit() ##退出,也可以安CTRL+D
4.定义变量
### 临时定义 (存放在内存)###
交互式环境中执行动作
[root@desktop31 ~]# 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
>>> exit() ##退出
[root@desktop31 ~]# 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.
>>> print a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined ##报错:变量a未定义,上次交互界面定义的变量值是没有被保存的
>>> exit()
### 永久定义(存放在硬盘)###
脚本的方式执行动作
[root@desktop31 mnt]# vim hello.py
#!/usr/bin/python ##指定脚本解释器
a = 1
print a
[root@desktop31 mnt]# chmod +x hello.py
[root@desktop31 mnt]# ./hello.py
当我们在拥有不同版本python中执行python时下面这样定义解释器可以解决版本问题
[root@desktop31 mnt]# vim hello.py
#!/usr/bin/env python ##引用环境变量里面自定义的 Python 版本,具有较强的可移植性
a = 1
print a
[root@desktop31 mnt]# chmod +x hello.py
[root@desktop31 mnt]# ./hello.py
1
三、python编码
1.字符问题
[root@desktop31 mnt]# vim hello.py
#!/usr/bin/env python
a = "你好" ##当脚本中出现中文时是无法执行的
print a
[root@desktop31 mnt]# chmod +x hello.py
[root@desktop31 mnt]# ./hello.py ##脚本执行报错
File "./hello.py", line 2
SyntaxError: Non-ASCII character '\xe4' in file ./hello.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
vim hello.py
#!/usr/bin/env python
#coding:utf-8 ##指定编码,以下方式任选一
#coding=utf-8
#encoding:utf-8
#encoding=utf-8
a = "你好"
print a
[root@desktop31 mnt]# ./hello.py
你好
2)使用
[root@desktop31 ~]# ipython ##进入ipython
Python 2.7.5 (default, Feb 11 2014, 07:46:25)
Type "copyright", "credits" or "license" for more information.
IPython 3.2.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]: name_unicode = u'中国' ##输入命令
In [2]: type(name_unicode)
Out[2]: unicode
In [3]: print name_unicode
中国
In [4]: exit() ##退出
3.pycharm
1)安装
[root@desktop31 Desktop]# ls
pycharm-community-2017.1.4.tar.gz
[root@desktop31 Desktop]# tar zxf pycharm-community-2017.1.4.tar.gz
[root@desktop31 Desktop]# ls
pycharm-community-2017.1.4 pycharm-community-2017.1.4.tar.gz
[root@desktop31 Desktop]# cd pycharm-community-2017.1.4/
[root@desktop31 pycharm-community-2017.1.4]# ls
bin help Install-Linux-tar.txt lib plugins
build.txt helpers jre64 license
[root@desktop31 pycharm-community-2017.1.4]# cd bin/
[root@desktop31 bin]# ls
format.sh idea.properties pycharm64.vmoptions restart.py
fsnotifier inspect.sh pycharm.png
fsnotifier64 log.xml pycharm.sh
fsnotifier-arm printenv.py pycharm.vmoptions
#coding:utf-8
_author_ = "wangying"
'''
@author:wangying
@file:hello.py
@contact:1172420437@qq.com
@time:6/28/1710:15 AM
@desc
'''
hostname = raw_input('主机名: ') ##raw_input定义字符变量
IP = raw_input('IP: ')
used_year = input('used_year: ') ##input定义数字变量
CPU = raw_input('CPU: ')
Memory = raw_input('Memory: ')
manager_name = raw_input('manager_name: ')
if used_year < 10:
a = ''' ##'''指定格式输出
主机信息
主机名 :%s ##%s为字符占位符号
IP :%s
使用年限 :%d ##%d为数字占位符号
CPU :%s
Memory :%s
manager_name:%s
''' %(hostname, IP, used_year, CPU, Memory,manager_name) ##让占位符号取值
print a
else:
print '该服务器使用年限太久!'
六、python变量的定义
变量是内存中的一块区域。
变量的命名: 变量名由字母,数字,下划线组成。
[root@desktop31 mnt]# 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
>>> a_1 = 2
>>> print a_1
2
>>> _a = 3
>>> print _a
3
>>> 1a = 4
File "<stdin>", line 1
1a = 4
^
SyntaxError: invalid syntax
>>> a-1 = 5
File "<stdin>", line 1
SyntaxError: can't assign to operator
>>> exit()
python中地址变量与c语言刚好相反,一条数据包含包含多个标签;
[root@desktop31 mnt]# 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
>>> b = 1
>>> id(a)
34258024
>>> id(b)
34258024
>>> exit()
七、python运算符
1.赋值运算符:=、+=、-=、/=、%=、*=、**=、
[root@desktop31 mnt]# 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 = 10
>>> print a
10
>>> a += 1
>>> print a
11
>>> a -= 1
>>> print a
10
>>> a /= 2
>>> print a
5
>>> a *= 3
>>> print a
15
>>> a %= 2
>>> print a
1
>>> exit()
2.算术运算:+、-、*、/、//、%、**
[root@desktop31 mnt]# 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.
>>> 1 + 1
2
>>> 1 - 1
0
>>> 2 * 3
6
>>> 1 / 2
0
>>> 1.0 / 2
0.5
>>> 1.0 // 2
0.0
>>> 1 % 2
1
>>> 2 ** 3
8
>>> exit()
3.关系运算(返回一个布尔类型的结果):<、>、>=、<=、==、!=
[root@desktop31 mnt]# 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.
>>> 1 < 2
True
>>> 1 > 2
False
>>> 2 >= 2
True
>>> 2 <= 2
True
>>> 2 == 2
True
>>> 2 != 2
False
>>> 3 != 2
True
>>> exit()
4.逻辑运算:and #逻辑与
or #逻辑或
not #逻辑非
1 #本身代表true
0 #代表False
[root@desktop31 mnt]# 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.
>>> 2>1 and 3>2
True
>>> 2>1 and 1>2
False
>>> 2>1 or 1>2
True
>>> 0>1 or 1>2
False
>>> not 1>2
True
>>> not 1
False
>>> not 0
True
>>> exit()
1.数字
整形 int
长整形 long
强制定义为长整型 num2 = 1L
浮点型(小数) float
[root@desktop31 mnt]# 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.
>>> num1 = 1
>>> type(num1)
<type 'int'>
>>> num1 = 11111111111111111111111111111111111111
>>> type(num1)
<type 'long'>
>>> num2 = 1L
>>> type(num2)
<type 'long'>
>>> num3 = 1.2
>>> type(num3)
<type 'float'>
>>> exit()
2.字符串
1)字符串定义:' '、" "、""" """
[root@desktop31 mnt]# 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 = 'hello world'
>>> print a
hello world
>>> b = "hello world"
>>> print b
hello world
>>> c = """hello world"""
>>> print c
hello world
>>> type(a)
<type 'str'>
>>> type(b)
<type 'str'>
>>> type(c)
<type 'str'>
>>> exit()
### 注意:''不能识别' ###
[root@desktop31 mnt]# 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.
>>> say = 'let's go'
File "<stdin>", line 1
say = 'let's go'
^
SyntaxError: invalid syntax
>>> say = "let's go"
>>> print say
let's go
>>> exit()
2)转义符号
tab \t
换行 \n
[root@desktop31 mnt]# 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.
>>> mail = 'tom: hello i am happy'
>>> print mail
tom: hello i am happy
>>> mail = 'tom:\t hello\t i am happy'
>>> print mail
tom: hello i am happy
>>> mail = 'tom:\n hello\n i am happy'
>>> print mail
tom:
hello
i am happy
>>> exit()
3)三重引号
块注释
函数的doc文档
字符串格式化
[root@desktop31 mnt]# 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.
>>> mail = """tom:
... hello
... i am happy
... """
>>> print mail
tom:
hello
i am happy
>>> mail
'tom:\n\thello\n\ti am happy\n'
>>> exit()
4)字符串索引
[root@desktop31 mnt]# 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 = 'hello world'
>>> a[0]
'h'
>>> a[1]
'e'
>>> a[-1]
'd'
>>> a[1]+a[6]
'ew'
>>> exit()
5)字符串切片
a[从那里:到那里:步长]
[root@desktop31 mnt]# 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 = 'hello world'
>>> a[1:5] ##代表切片取出第2个到第4个
'ello'
>>> a[:5] ##代表切片取出第4个之前的
'hello'
>>> a[5:] ##代表切片取出第4个之后的
' world'
>>> a[5:1] ##python中默认是从左向右取值
''
>>> a[:] ##代表切片取出所有
'hello world'
>>> a[-4:-1] ##代表倒数第4个到倒数第2个切片
'orl'
>>> a[1:5:2] ##代表切片从第2个到第4个隔两个取一个
'el'
>>> a[-2:-4:-1] ##代表从倒数第4个到倒数第2个隔一个取一个
'lr'
>>> a[5:1:-2] ##当步长为-1时,从右向左取值
' l'