跳至主要内容

Python学习笔记2010026

开始学习python.
RH5已经开始内置python,而solaris还是没有嵌入但是机器上有,所以开始使用。
学习的教程是dive into python.网上有CHM和PDF, 个人现在看,由于篇幅大,但是相对来说基础知识涵盖的比较丰富。
安装就不提了,主要是在unix/linux上,所以直接跑。
xtuulei@cnshx021:%:python
Python 2.4.4 (#3, Dec  3 2007, 12:14:12)
[GCC 3.3.2] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
 
根据教程,
def 定义函数,和抒写规则一样,建议按照标准的书写规则,空行已经。 """ """" 类似于dic string表示好注释一样。
if else一样都需要规范书写。
模块中定义import sys的时候, 键入sys.__name__可以表现出当前模块。
 
----------------dictionary定义
dictionary: 定义参数一一对应
d = {"test":"test1",42:"test2","test3":33,44:56} 无先后顺序。
定义修覆盖,可添加
d["test4"] = 22
d["Test"]= 22
 
del ["test4"] 可删除。
d.clear() 清空d
 
键入d显示结果。
键入d["test"]显示test值,但是键入d["test1"]不能显示test这个key
 
 
----------------- list定义
类似于数组
li = ["a","b","mpilgrim","z","example"]
键入li查看结果
从0开始计数
li[0] 结果等于a
但是同时可以反计数,从-1开始
li[-5] 结果等于a
 
>>> li[1:3] 
['b', 'mpilgrim']
您可以通过指定 2 个索引得到 list 的子集, 叫做一个 “slice” 。返回值是一个新的 list, 它包含了 list 中按顺序从第一个 slice 索引 (这里为 li[1]) 开始, 直到但是不包括第二个 slice 索引 (这里为 li[3]) 的所有元素。
>>> li[1:-1]
['b', 'mpilgrim', 'z']
如果一个或两个 slice 索引是负数, slice 也可以工作。如果对您有帮助, 您可以这样理解:从左向右阅读 list, 第一个 slice 索引指定了您想要的第一个元素, 第二个 slice 索引指定了第一个您不想要的元素。返回的值为在其间的每个元素
>>> li[0:3] 
['a', 'b', 'mpilgrim'
 List 从 0 开始, 所以 li[0:3] 返回 list 的前 3 个元素, 从 li[0] 开始, 直到但不包括 li[3]。
 
li可以简写,可以将0去除,默认为0
li同样和dictionary一样可以增加参数
li.append("test") 在最后插入, 先前有个练习,在import sys后,键入sys.path可以看到sys module的具体路径,当打sys.append("path")就是意味在最后插入一段新的库包含的位置。
li.insert(2, "new") 如此可以指定li[2]是new,而如果用append加入的那个test换成new,两个不冲突。
li.extend(["test1","test2"])可以增加多个参数
 
extend和append的参数,extend将多个插入对象当作各个分布体,append讲多个插入对象当作一个整体.
PS. len(li)可读li的个数
 
在li[]中查找数目
li.index("test") index用于查询第一个出现的,而且也只显示第一个出现的index
要测试一个值是否在 list 内, 使用 in, 如果值存在, 它返回 True, 否则返为 False
"test" in li
True
 
删除li中的数值
li.remove 表示删除首次并只删除一个
li.pop() 表示删除最后一个,并返回该值
list做运算。
>>> li = ['a', 'b', 'mpilgrim']
>>> li = li + ['example', 'new']
>>> li
['a', 'b', 'mpilgrim', 'example', 'new']
Lists 也可以用 + 运算符连接起来。 list = list + otherlist 相当于 list.extend(otherlist)。 但 + 运算符把一个新 (连接后) 的 list 作为值返回, 而 extend 只修改存在的 list。 也就是说, 对于大型 list 来说, extend 的执行速度要快一些。
>>> li += ['two']         
>>> li
['a', 'b', 'mpilgrim', 'example', 'new', 'two']
Python 支持 += 运算符。 li += ['two'] 等同于 li.extend(['two'])。
>>> li = [1, 2] * 3  
>>> li
[1, 2, 1, 2, 1, 2]
* 运算符可以作为一个重复器作用于 list。 li = [1, 2] * 3 等同于 li = [1, 2] + [1, 2] + [1, 2], 即将三个 list 连接成一个
 
------tuple
tuple创建和list一样,但是不能修改。 类似于list已经被写保护了。
 
-------变量
 
如上,变量可以被定义。
 
>> x =1
>>x
1
 
一个变量赋多值
>>> v = ('a', 'b', 'e')
>>>('x','y','z') = v
>>>a
      x

 

-------格式化字符串
>>> k = "uid"
>>> v = "sa"
>>> "%s=%s" % (k, v)
'uid=sa'
 
>>> uid = "sa"
>>> pwd = "secret"
>>> print pwd + " is not a good password for " + uid 
secret is not a good password for sa
+ 是字符串连接操作符。
>>> print "%s is not a good password for %s" % (pwd, uid)
secret is not a good password for sa
+和%s是一样的
>>> userCount = 6
>>> print "Users connected: %d" % (userCount, )  
Users connected: 6
字符串格式化通过将 %s 替换成 %d 即可处理整数 后面之所以写成这样就让他定义成一个tuple,让这个东西不可修改。
但其实如果
>>> userCount = 6
>>> print "Users connected: %d" % userCount
Users connected: 6

也是可以实现的


>>> print "Users connected: " + userCount
错误
 
>>> print "aha %f" % 13
>>> print "aha %2f" % 13
>>> print "aha %+.2f" % 1.5
>>> pring "aha %+2f" % 1.5
%f 格式符选项被认为是一个十进制浮点数, 不指定精度时打印 6 位小数。使用包含 ".2" 精度修正符的 %f 格式符选项将只打印 2 位小数。您甚至可以混合使用各种修正符。 添加 + 修正符用于在数值之前显示一个正号或负号。 注意 ".2" 精度修正符仍旧在他原来的位置, 用于只打印 2 位小数。


--
Alex Tu
-----------------------------------------------------------
ShangHai,China

评论

此博客中的热门博文

Python学习笔记20100117

映射list   Dictionary 是用{}. list 是一那个[]. turple是用() 当你定义过dictionary后,你可以使用d.keys(), d.values(),d.items()将定义后的时候分别显示出来 当然可以将list里的值加减乘除,也可以如一般的定义直接重新复制这个list. 链接与分割字符串的 li=";", join(li) 则显示的就是 分隔符为;的数 如果使用li=li.split(";")则将刚刚;的分隔符删除,而split里也可定义域,如li.split(";",1)   自省 <---这是啥 之前在使用的时候发觉有些书本上的模块不能调用,很多是py脚本定义过的函数。 下载该脚本s,然后上传至指定位置。 >>>import sys >>>sys.path >>>sys.path.append("绝对位置")然后就能调用这些脚本和参数了。 删除 >>>sys.path.pop() 定义一个参数是 def info(test, test1=10.test2=12): info是函数名,test是必备参数,因为没有定义值,test1和test2是可选参数,定义了初始值   以上是外部函数的调用,下面转到内部函数 内部函数有type,str,dir及其他   ---type 返回任意字符的类型,模块也可以。types模块 >>> type(1) <type 'int'> >>> li=[] >>> type(li) <type 'list'> >>> import odbchelper >>> type(odbchelper) <type 'module'> >>> type(sys) <type 'module'> >>> import types ...

Setagaya park

  We visited Setagaya Park (世田谷区) yesterday. This park reminds us when we in Europe, which has a fancy fountain in the middle, and free entrance. People just laid down on the grass, enjoy the day time they can waste. people who have kids together,  play all different kinds of sports. you really can't image whether you are in Europe or  Japan. I would prefer to use a Bus instead of the metro, the metro is fast, but you only can see the dark, light, people sit or stand as Sardine, without any emotion. but on the bus, you can see the traffic, the shop, everything on the road. When we on our way to the park, we stop by a good chocolate shop named " craft chocolate works ",  this is a very good place to have ice cream, you probably can choose half & half, you can enjoy the feeling when you smell the chocolate smell and eat with it.  or you can enjoy beer or cake in "globe antiques (ザ グローブアン ティ ークス) .   also, we checked the Blue Bottle Coffee Sangenjaya ...