python基础学习
1、运算
[root@node01 bin]# python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 6543+7456 加
13999
>>> 10-2 减
8
>>> 10*2 乘
20
>>> 10/2 除
5
>>> 10%3 取模
1
>>> 10%2 取模
0
>>> 1.0/2.0
0.5
2、变量
>>> name = 'wolf'
>>> print name
wolf
>>> name = "wolf"
>>> print name
wolf
这里单引号,双引号都一样
3、常量
Python里没有真正意义上的常量,一般用大写规定标定表示是一个常量。
4、注释
#就是注释
5、数据类型
>>> age = 121 整型
>>> type(age)
<type 'int'>
>>> name = 'wolf' 字符串
>>> type(name)
<type 'str'>
>>> sec = True 布尔,注意大小写
>>> type(sec)
<type 'bool'>
>>> area = 1.22 浮点数
>>> type(area)
<type 'float'>
>>> area = -1.22 负数
>>> type(area)
<type 'float'>
>>> len(name) len得到长度
4
6、数据类型互相转换
>>> name = str(1212) 整数转字符串
>>> print name
1212
>>> type(name)
<type 'str'>
>>> age = int('111') 字符串转换为整数
>>> type(age)
<type 'int'>
>>> sex = bool('True')
>>> print sex
True
7、字符串拼接
>>> a1 = 'hello'
>>> a2 = 'world'
>>> print a1+a2
helloworld
>>> a1 = 'hello '
>>> print a1+a2
hello world
8、获取用户输入
>>> x = input("x:")
x:2
>>> y = input("y:")
y:3
>>> print x+y
5
>>> print x*y
6
9、函数
幂运算
>>> 2**3
8
>>> pow(2,3) 内建函数
8
>>> pow(2,4)
16
>>> 10+pow(2,4)/2
18
>>> abs(-10) 绝对值函数
10
>>> 1/2 整数除法会截除小数部分
0
>>> round(1.0/2.0) round函数会把符点数四舍五入为最近接的整数值
1.0
10、模块
improt
from。。。import。。。。
使用import导入到python以增强功能扩展
如年龄32.9岁
>>> import math
>>> math.floor(32.9)
32.0
上面学过数据类型转换,这里可以使用int函数进行转换
>>> int(math.floor(32.9))
32
导入单个函数
>>> from math import sqrt
>>> sqrt(9) 平方根
3.0
>>> foo=math.sqrt 也可以通过变量的方式把模块赋给foo
>>> foo(4)
2.0
>>> sqrt(-1) 负值
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> import cmath
>>> cmath.sqrt(-1)
1j
11、python文件保存和执行
.py结尾的文件
python *.py
[root@node01 python]# vi py01.py
print "Hello World!"
[root@node01 python]# python py01.py
Hello World!
在编写脚本时,像shell一样把#!/usr/bin/env python加到首行
#!/usr/bin/env python
print "Hello World!"
也可以执行
chmod a+x 脚本名称
./py01.py直接执行脚本
12、转义字符 \
>>> 'this is wolf's'
File "<stdin>", line 1
'this is wolf's'
^
SyntaxError: invalid syntax
>>> 'this is wolf\'s'
"this is wolf's"
不能把\放结尾。
13、字符串表示--str和repr
>>> "Hello,World"
'Hello,World'
>>> 10000L
10000L
>>> print "Hello,World"
Hello,World
>>> print 10000L
10000
>>> print repr("Hello,World")
'Hello,World'
>>> print repr(10000L)
10000L
>>> print str("Hello,World")
Hello,World
>>> print str(10000L)
10000
str、repr、反引号``是讲python值转换为字符串的3中方法。
14、长字符串、原始字符串、Unicode
很长一串字符串使用
''' dfdf
df
"dfd" '''
' dfdf \n df \n"dfd" '
>>> path = 'C:\nowhere'
>>> path
'C:\nowhere'
>>> print path
C:
owhere 这里明显不对了
原始字符串就派上用场了
>>> print r'C:\nowhere'
C:\nowhere
这个暂时没看懂
>>> u"Hello,wold!"
u'Hello,wold!'
1、运算
[root@node01 bin]# python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 6543+7456 加
13999
>>> 10-2 减
8
>>> 10*2 乘
20
>>> 10/2 除
5
>>> 10%3 取模
1
>>> 10%2 取模
0
>>> 1.0/2.0
0.5
2、变量
>>> name = 'wolf'
>>> print name
wolf
>>> name = "wolf"
>>> print name
wolf
这里单引号,双引号都一样
3、常量
Python里没有真正意义上的常量,一般用大写规定标定表示是一个常量。
4、注释
#就是注释
5、数据类型
>>> age = 121 整型
>>> type(age)
<type 'int'>
>>> name = 'wolf' 字符串
>>> type(name)
<type 'str'>
>>> sec = True 布尔,注意大小写
>>> type(sec)
<type 'bool'>
>>> area = 1.22 浮点数
>>> type(area)
<type 'float'>
>>> area = -1.22 负数
>>> type(area)
<type 'float'>
>>> len(name) len得到长度
4
6、数据类型互相转换
>>> name = str(1212) 整数转字符串
>>> print name
1212
>>> type(name)
<type 'str'>
>>> age = int('111') 字符串转换为整数
>>> type(age)
<type 'int'>
>>> sex = bool('True')
>>> print sex
True
7、字符串拼接
>>> a1 = 'hello'
>>> a2 = 'world'
>>> print a1+a2
helloworld
>>> a1 = 'hello '
>>> print a1+a2
hello world
8、获取用户输入
>>> x = input("x:")
x:2
>>> y = input("y:")
y:3
>>> print x+y
5
>>> print x*y
6
9、函数
幂运算
>>> 2**3
8
>>> pow(2,3) 内建函数
8
>>> pow(2,4)
16
>>> 10+pow(2,4)/2
18
>>> abs(-10) 绝对值函数
10
>>> 1/2 整数除法会截除小数部分
0
>>> round(1.0/2.0) round函数会把符点数四舍五入为最近接的整数值
1.0
10、模块
improt
from。。。import。。。。
使用import导入到python以增强功能扩展
如年龄32.9岁
>>> import math
>>> math.floor(32.9)
32.0
上面学过数据类型转换,这里可以使用int函数进行转换
>>> int(math.floor(32.9))
32
导入单个函数
>>> from math import sqrt
>>> sqrt(9) 平方根
3.0
>>> foo=math.sqrt 也可以通过变量的方式把模块赋给foo
>>> foo(4)
2.0
>>> sqrt(-1) 负值
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> import cmath
>>> cmath.sqrt(-1)
1j
11、python文件保存和执行
.py结尾的文件
python *.py
[root@node01 python]# vi py01.py
print "Hello World!"
[root@node01 python]# python py01.py
Hello World!
在编写脚本时,像shell一样把#!/usr/bin/env python加到首行
#!/usr/bin/env python
print "Hello World!"
也可以执行
chmod a+x 脚本名称
./py01.py直接执行脚本
12、转义字符 \
>>> 'this is wolf's'
File "<stdin>", line 1
'this is wolf's'
^
SyntaxError: invalid syntax
>>> 'this is wolf\'s'
"this is wolf's"
不能把\放结尾。
13、字符串表示--str和repr
>>> "Hello,World"
'Hello,World'
>>> 10000L
10000L
>>> print "Hello,World"
Hello,World
>>> print 10000L
10000
>>> print repr("Hello,World")
'Hello,World'
>>> print repr(10000L)
10000L
>>> print str("Hello,World")
Hello,World
>>> print str(10000L)
10000
str、repr、反引号``是讲python值转换为字符串的3中方法。
14、长字符串、原始字符串、Unicode
很长一串字符串使用
''' dfdf
df
"dfd" '''
' dfdf \n df \n"dfd" '
>>> path = 'C:\nowhere'
>>> path
'C:\nowhere'
>>> print path
C:
owhere 这里明显不对了
原始字符串就派上用场了
>>> print r'C:\nowhere'
C:\nowhere
这个暂时没看懂
>>> u"Hello,wold!"
u'Hello,wold!'