「学习笔记——Python」Python非正式导引
在本节的例子中,以提示符>>>, … ,开始的是输入,否则为输出, #后为python的注释
Table of Contents
1 把Python当作计算器
1.1 数字
1.2 字符串
1.3 Unicode 编码的字符串
1.4 List
2 初步程序设计
1 把Python当作计算器
1.1 数字
首先进入交互模式,将Python当作计算器
$ python
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 + 2
3
>>> 1 - 2
-1
>>> 2 * 3
6
>>> 2 / 4
0
>>> 2 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> (1+2)*3
9
还可以给变量赋值,然后参与运算
>>> x = 3
>>> y = 4
>>> (x + y) * 5
35
还可以连续赋值,但不能使用没有定义过的变量
>>> x = y = z = 3.4
>>> x
3.4
>>> y
3.4
>>> c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'c' is not defined
>>> z
3.4
同时,Python还支持复数,可以用 a + bj,或者complex(a,b)表示
>>> 1 + 2j
(1+2j)
>>> 2j * 3j
(-6+0j)
>>> complex(1+2j) * complex(1-2j)
(5+0j)
同时,还可以将复数赋给变量,然后提取实部,虚部
>>> x = (3+4j)
>>> x.real
3.0
>>> x.imag
4.0
float(),int(),可以将值分别转换为浮点型和整型,但对复数不适用,复数可以用abs()得出其绝对值,或称为模。
>>> int(3.4)
3
>>> int(3.5)
3
>>> float (3)
3.0
>>> a = 1 + 2j
>>> a
(1+2j)
>>> float(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float
>>> abs(a)
2.23606797749979
>>> abs(3+4j)
5.0
交互模式下可以用 _ 代表上次计算的结果
>>> 1 + 2
3
>>> 4 + _
7
>>> 5 * _
35
1.2 字符串
除了数学外,Python还可以操作字符串,注意转义字符,单引号,双引号,试几个例子,体会一下
>>> 'hello,python'
'hello,python'
>>> "hello,python"
'hello,python'
>>> 'Don't'
File "<stdin>", line 1
'Don't'
^
SyntaxError: invalid syntax
>>> 'Don\'t'
"Don't"
>>> "Dont't"
"Dont't"
>>> "Don\'t"
"Don't"
字符串可以分为多行,并用print函数打印
>>> hello = "This is a string which is \n\
... used to test multi\
... lines."
>>> print hello
This is a string which is
used to test multilines.
注意换行符\n,以及多行编辑\这两个符号的使用。
也可以用三引号,这样就不必写这两个转义字符。
>>> hello = """
... Usage: thingy [OPTIONS]
... -h Display this usage message
... -H hostname Display host name
... """
>>> print hello
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Display host name
如果不需要转义字符,即\n就表示\n,不表示转义,可以以r作用字符串起始
>>> hello = r"this is \n a test line"
>>> print hello
this is \n a test line
>>> hello = "this is \n a test line"
>>> print hello
this is
a test line
字符串可以被连接,甚至可以被“乘”(其实也是连接啦~)
>>> words = 'A' + ' hello ' + 'B'
>>> print words
A hello B
>>> '<' + words*3 + '>'
'<A hello BA hello BA hello B>'
字符串也可以被提取,下标从0开始,下标可以为负数,表示倒数 str[i:j]表示从str下标i到下标(j-1),共j-i个字符。 str[_:3]表示str[0:3] str[2:_ ]表示下标2开始到最后
>>> words = "hello"
>>> words[0]
'h'
>>> words[0:3]
'hel'
>>> words[1:2]
'e'
>>> words[:3]
'hel'
>>> words[2:]
'llo'
>>> words="hello"
>>> words[-1]
'o'
>>> words[-2]
'l'
字符串中字母不能被修改,只能再造字符串
>>> words
'hello'
>>> words[0] = 'a'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> words="hi"
>>> words
'hi'
字符串长度可用len函数得到
>>> words="hello"
>>> len(words)
5
1.3 Unicode 编码的字符串
使用u前缀来定义Unicode 编码的字符串 使用unicode函数将其它形式编码转化为unicode形式 使用encode可以将unicode形式编码转化为其它形式
>>> u'Hello python !'
u'Hello python !'
>>> unicode('\xc3\xa4\xc3\xb6\xc3\xbc', 'utf-8')
u'\xe4\xf6\xfc'
>>> u"äöü".encode('utf-8')
'\xc3\xa4\xc3\xb6\xc3\xbc'
1.4 List
Python有一系列数据结构,可以用于将多个值组合在一起,其中最多才多艺的要数List
>>> a = ['spam', 3, 4.5, "hi"]
>>> a
['spam', 3, 4.5, 'hi']
>>> a[0]
'spam'
>>> a[1]
3
>>> a[-1]
'hi'
>>> a[1:-1]
[3, 4.5]
>>> 2*a[:3] + ['xxx']
['spam', 3, 4.5, 'spam', 3, 4.5, 'xxx']
与字符串不同,List的各元素可以被改变
>>> a
['spam', 3, 4.5, 'hi']
>>> a[1] = 6
>>> a
['spam', 6, 4.5, 'hi']
同时List还可以方便地进行插入,替换等操作
>>> a
['spam', 6, 4.5, 'hi']
>>> a[
补充:Web开发 , Python ,