RSS
热门关键字:  java  Ajax  JSP  JSF  Struts
当前位置 : 首页>Python>列表

Python精要

来源: 作者: 时间:2007-09-19 点击:

Pyhton基础(一)
 
1、运行 python 即可进入解释程序;
 
2、Ctrl+Z (Windows) Ctrl+D (Unix) 终结解释程序
 
3、import sys; sys.exit()  终结解释程序
 
4、注释
# this is the first comment
SPAM = 1                 # and this is the second comment
                         # ... and now a third!
STRING = "# This is not a comment."
 
5、数字计算,Python 就是计算器,cool!
>>> 2+2  # and a comment on the same line as code 注释可以写在同一行
4
>>> (50-5*6)/4   # 计算式子可以带括号
5.0
>>> 8/5 # Fractions aren't lost when dividing integers
1.6000000000000001
>>> print(8/5)  打印结果
1.6
>>> # Integer division returns the floor:  整除
... 7//3
2
>>> 7//-3  # 支持负数除法
-3
# 同时为多个变量赋值
>>> x = y = z = 0  # Zero x, y and z
>>> x
0
>>> y
0
>>> z
0
#支持复数运算
>>> 1j * 1J
(-1+0j)
>>> 1j * complex(0, 1)
(-1+0j)
>>> 3+1j*3
(3+3j)
>>> (3+1j)*3
(9+3j)
>>> (1+2j)/(1+1j)
(1.5+0.5j)
# 复数的两个部分
>>> a=1.5+0.5j
>>> a.real
1.5
>>> a.imag
0.5
# 象计算器那样,可以使用最后的值,使用 _ 代替
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
>>>

6、字符串操作
>>> 'spam eggs'
'spam eggs'
>>> 'doesn\'t'
"doesn't"
>>> "doesn't"
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'

# + 表示连接,* 表示重复
>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
# 象使用数组那样使用字符串当中的字符,与 VF、.NET 当中的 Substr、Substring 函数一样
>>> word[4]
'A'
>>> word[0:2]
'He'
>>> word[2:4]
'lp'
>>> word[:2]    # The first two characters
'He'
>>> word[2:]    # Everything except the first two characters
'lpA'
>>> word[:2] + word[2:]
'HelpA'
>>> word[:3] + word[3:]
'HelpA'
>>> word[1:100]
'elpA'
>>> word[10:]
''
>>> word[2:1]
''
>>> word[-1] # The last character
'A'
>>> word[-2]     # The last-but-one character
'p'
>>> word[-2:]    # The last two characters
'pA'
>>> word[:-2]    # Everything except the last two characters
'Hel'
总之,位置如图所示
 +---+---+-+---+---+
  | H | e | l | p | A |
 +---+---+-+---+---+
 0   1   2   3   4   5
-5  -4  -3  -2  -1
# 使用 len 函数
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34
 
7、使用 Unicode
>>> 'Hello\u0020World !'
'Hello World !'
>>> "Äpfel".encode('utf-8')
b'\xc3\x84pfel'


Python基础(二)
 
1、使用 Lists
>>> a = ['spam', 'eggs', 100, 1234]

共4页: 上一页 1 [2] [3] [4] 下一页
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
Google Adsense
相关文章