print()格式输入
print(format(val,format_modifier))
val: 值
format_modifier 格式学
print(format(12.34567,'6.2f'))
6 输入占位
3 精度 小数点 后几位
百分号 格式化输出
print(format(0.346,'.2%'))
输入语句
re=raw_input(prompt)
prompt 提示字符
re 为返回值
1
2
3
4
5
6
7
8
9
10
11
|
>>> age
=
raw_input
(
'your age : '
)
your age :
23
>>>
print
(age)
23
>>> age
=
int
(age)
>>> age
=
int
(
raw_input
(
'plz input your age'
))
plz
input
your age
23
>>>
print
age
23
>>>
type
(age)
<
type
'int'
>
|
age =int(age) 字符串转换成整型
int 整型
float 浮点型数据
id 函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
>>>
print
x
12
>>>
id
(x)
38687280
>>>
print
x
12
>>> x
=
13
>>>
print
x
13
>>>
id
(x)
38687256
>>> x
=
12
>>>
print
x
12
>>>
id
(x)
38687280
>>> y
=
13
>>>
print
y
13
>>>
id
(y)
38687256
>>>指向 内存 一样
|
python 变量 不是内容变 指向内存单元的变
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
>>> x
=
12
>>> y
=
12.5
>>> z
=
'www.baidu.com'
>>>
print
x
12
>>>
print
y
12.5
>>>
print
z
www.baidu.com
>>>
type
(x)
<
type
'int'
>
>>>
type
(y)
<
type
'float'
>
>>>
type
(z)
<
type
'str'
>
|
python 函数
字符函数库
>>> s = 'baidu'
>>> s.islower()
True
小写函数
替换函数
>>> s4 ='abadsadsadasbab111111111'
>>> s4.replace('ab','AB')
'ABadsadsadasbAB111111111'
默认字符库
import math
引入数学函数库
>>> import math
>>> val=math.sin(3.14/6)
>>> print val
0.499770102643
>>> math.pi
3.1415926535897931
>>> val=math.sin(math.pi /6)
>>> print val
0.5
>>> 3*3*3*3
81
>>> math.pow(3,4)
81.0
pow函数 3的4次方
>>> 3*3*3*3*3*3*3
2187
>>> math.pow(3,7)
2187.0
>>> 3**7
2187
import os
加载操作系统模块
>>> import os
>>> os.getcwd()
'/root'
>>> import socket
>>> baiduip=socket.gethostbyname('www.baidu.com')
>>> print baiduip
115.239.210.26
获得某主机ip地址 网络库
输入quit()
第三方提供的函数库
httplib2
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
|
[root@ceshi ~]
# yum install python-setuptools
Loaded plugins: fastestmirror
Loading mirror speeds
from
cached hostfile
*
base: mirrors.
163.com
*
extras: mirrors.grandcloud.cn
*
updates: mirrors.grandcloud.cn
Setting up Install Process
Resolving Dependencies
-
-
> Running transaction check
-
-
-
> Package python
-
setuptools.noarch
0
:
0.6
.
10
-
3.el6
set
to be updated
-
-
> Finished Dependency Resolution
Dependencies Resolved
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
Package Arch Version Repository Size
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
Installing:
python
-
setuptools noarch
0.6
.
10
-
3.el6
base
336
k
Transaction Summary
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
Install
1
Package(s)
Upgrade
0
Package(s)
Total download size:
336
k
Installed size:
1.5
M
Is this ok [y
/
N]: y
Downloading Packages:
python
-
setuptools
-
0.6
.
10
-
3.el6
.noarch.rpm |
336
kB
00
:
00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : python
-
setuptools
-
0.6
.
10
-
3.el6
.noarch
1
/
1
Installed:
python
-
setuptools.noarch
0
:
0.6
.
10
-
3.el6
Complete!
|
1
2
3
4
5
6
|
import
urllib
import
webbrowser
url
=
'http://www.163.com'
content
=
urllib.urlopen(url).read()
open
(
'163.com.html'
,
'w'
).write(content)
webbrowser.open_new_tab(
'163.com.html'
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@ceshi ~]
# easy_install httplib2
Searching
for
httplib2
Reading http:
/
/
pypi.python.org
/
simple
/
httplib2
/
Reading http:
/
/
code.google.com
/
p
/
httplib2
/
Best match: httplib2
0.8
Downloading http:
/
/
httplib2.googlecode.com
/
files
/
httplib2
-
0.8
.
zip
Processing httplib2
-
0.8
.
zip
Running httplib2
-
0.8
/
setup.py
-
q bdist_egg
-
-
dist
-
dir
/
tmp
/
easy_install
-
jLdE50
/
httplib2
-
0.8
/
egg
-
dist
-
tmp
-
jb2a6G
zip_safe flag
not
set
; analyzing archive contents...
httplib2.__init__: module references __file__
Adding httplib2
0.8
to easy
-
install.pth
file
Installed
/
usr
/
lib
/
python2.
6
/
site
-
packages
/
httplib2
-
0.8
-
py2.
6.egg
Processing dependencies
for
httplib2
Finished processing dependencies
for
httplib2
|
安装第三方函数库
或者使用
先yum一下python-pip包
然后用pip install安装好了
进行测试
1
2
3
4
5
6
|
[root@ceshi ~]
# python
Python
2.6
.
6
(r266:
84292
, Nov
22
2013
,
12
:
16
:
22
)
[GCC
4.4
.
7
20120313
(Red Hat
4.4
.
7
-
4
)] on linux2
Type
"help"
,
"copyright"
,
"credits"
or
"license"
for
more information.
>>>
import
httplib2
>>>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@ceshi ~]
# python 1.py
Entry programme
hello the cruel world!
www.baidu.com
Leave programme
[root@ceshi ~]
# vi 1.py
#!/usr/bin/python
def
test_a():
print
'hello the cruel world!'
print
'www.baidu.com'
print
'Entry programme'
test_a()
print
'Leave programme'
|
自定义函数 注意要有冒号和缩进
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@ceshi ~]
# vi 1.py
#!/usr/bin/python
def
test_a():
print
'hello the cruel world!'
print
'www.baidu.com'
def
test_b(va11,va12):
print
va11,
print
va12
print
'Entry programme'
test_a()
test_b(
12
,
13
)
test_b(
'www.baidu.com'
,
'python'
)
test_a()
print
'Leave programme'
|
返回值 return
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#!/usr/bin/python
def
test_a():
print
'hello the cruel world!'
print
'www.baidu.com'
def
test_b(va11,va12):
print
va11,
print
va12
def
test_c(n1,n2):
print
n1,
print
n2
n
=
n1
+
n2
return
n
print
'Entry programme'
test_a()
test_b(
12
,
13
)
test_b(
'www.baidu.com'
,
'python'
)
sum
=
test_c(
100
,
102
)
print
'sum = '
,
sum
print
'Leave programme'
|
Entry programme
hello the cruel world!
www.baidu.com
12 13
www.baidu.com python
100 102
sum = 202
Leave programme
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
|
#!/usr/bin/python
def
test_a():
print
'hello the cruel world!'
print
'www.baidu.com'
def
test_b(va11,va12):
print
va11,
print
va12
def
test_c(n1,n2):
print
n1,
print
n2
n
=
n1
+
n2
return
n
def
test_d(n1,n2):
print
n1,
print
n2
n
=
n1
+
n2
m
=
n1
*
n2
p
=
n1
-
n2
e
=
n1
*
*
n2
return
n, m, p, e
print
'Entry programme'
test_a()
test_b(
12
,
13
)
test_b(
'www.baidu.com'
,
'python'
)
sum1
=
test_c(
100
,
102
)
print
'sum1 = '
, sum1
sum1, multi, plus, pow1
=
test_d(
2
,
10
)
print
sum1, multi,plus,pow1
print
'Leave programme'
|
[root@ceshi ~]# python 1.py
Entry programme
hello the cruel world!
www.baidu.com
12 13
www.baidu.com python
100 102
sum1 = 202
2 10
12 20 -8 1024
Leave programme