实验一下Python中初级的方法
2008-08-27 20:12
411 查看
再次好好看看Python,看看里边初级的方法如print等,以及三个内建类型list,tuple,dictionary,以及内建方法slice等的操作,虽然都是很基础的东西,但是记录下来,以后可以复习巩固.
_str = "Hello, Python!"
print "_str's length = ", len(_str)
_list = [1, 2, 3]
print "_list's length = ", len(_list)
_tuple = tuple(_str)
print "_tuple's length = ", len(_tuple)
#max() & min()
print "Max(_list) = ", max(_list) #result:3
print "Min(_list) = ", min(_list) #result:1
#slice a list
_list_slice = _list[1:3]
print "Members in _list_slice:";
for _ls in _list_slice: #result:2 3.Begin form index 1, end before index 3
print _ls
#modify a list using 'slice'
_list[1:4] = [6, 4, 10]
print "Members in new list:";
for _ls in _list: #result:1 6 4 10.Length changed to 4
print _ls
#get tuple elements
print "Members in _tuple:";
for _t in _tuple: #result:Hello, Python!(over).Each char a line.
print _t
#slice a tuple
_tuple_slice = _tuple[:-1]
print "Members in _tuple_slice:";
for _ts in _tuple_slice: #result: H e l l o , [space] P y t h o n(over).End before the last one.All char in a line as a result of '//'.(maybe ",")
print _ts, /
#must go on in the next line, otherwise a "invalid syntax" error. comment or null line is help.
#however, if no "/" last line, just end with ",", we can use "print" just after last line, with content in the same line if no "/n"
print "/nhello"
#dictionary
_dic = { 'i' : 'u',
'he' : 'she'}
_dic['him'] = 'her' #add an element him->her
_dic[9] = 'I am' #add an element 9->I am
print "_dic's length = " , len(_dic)
for _key in _dic: #result: as in _dic, not as the order
print _key, "/t:/t", _dic[_key]
#usage of "print"
print "It's %d bug, maybe!" % 1 #result: it's 1 bug, maybe!
print ("not like %s and %s" % ('C', 'C++')) #result: not like c and c++
#modify tuple
try:
_tuple[1] = 'r' #raise an error
except TypeError:
print "Tuple object can't be modified"
_str = "Hello, Python!"
print "_str's length = ", len(_str)
_list = [1, 2, 3]
print "_list's length = ", len(_list)
_tuple = tuple(_str)
print "_tuple's length = ", len(_tuple)
#max() & min()
print "Max(_list) = ", max(_list) #result:3
print "Min(_list) = ", min(_list) #result:1
#slice a list
_list_slice = _list[1:3]
print "Members in _list_slice:";
for _ls in _list_slice: #result:2 3.Begin form index 1, end before index 3
print _ls
#modify a list using 'slice'
_list[1:4] = [6, 4, 10]
print "Members in new list:";
for _ls in _list: #result:1 6 4 10.Length changed to 4
print _ls
#get tuple elements
print "Members in _tuple:";
for _t in _tuple: #result:Hello, Python!(over).Each char a line.
print _t
#slice a tuple
_tuple_slice = _tuple[:-1]
print "Members in _tuple_slice:";
for _ts in _tuple_slice: #result: H e l l o , [space] P y t h o n(over).End before the last one.All char in a line as a result of '//'.(maybe ",")
print _ts, /
#must go on in the next line, otherwise a "invalid syntax" error. comment or null line is help.
#however, if no "/" last line, just end with ",", we can use "print" just after last line, with content in the same line if no "/n"
print "/nhello"
#dictionary
_dic = { 'i' : 'u',
'he' : 'she'}
_dic['him'] = 'her' #add an element him->her
_dic[9] = 'I am' #add an element 9->I am
print "_dic's length = " , len(_dic)
for _key in _dic: #result: as in _dic, not as the order
print _key, "/t:/t", _dic[_key]
#usage of "print"
print "It's %d bug, maybe!" % 1 #result: it's 1 bug, maybe!
print ("not like %s and %s" % ('C', 'C++')) #result: not like c and c++
#modify tuple
try:
_tuple[1] = 'r' #raise an error
except TypeError:
print "Tuple object can't be modified"
相关文章推荐
- python+selenium初级(10)—八大定位方法
- 记录一下python easy_install和pip安装地址和方法
- python实现的工厂方法--设计模式初级
- python 之简单畅聊一下静态方法和动态方法
- python+selenium初级(3)—WebElement接口常用方法
- 【python初级】010-构造方法,属性和迭代器
- 初级Python中map函数的运用以及列表转字符串的方法
- 分享一下Python 开发者节省时间的10个方法
- 分享一下Python 开发者节省时间的10个方法
- cjson libcurl 发送请求给python 接受数据和处理数据 参考了一下别人的方法
- 学习了一下python中使用adb命令的方法
- 记录一下python easy_install和pip安装地址和方法
- python中翻转字符串的方法,标记一下
- 新闻上的文本分类:机器学习大乱斗 王岳王院长 王岳王院长 5 个月前 目标 从头开始实践中文短文本分类,记录一下实验流程与遇到的坑 运用多种机器学习(深度学习 + 传统机器学习)方法比较短文本分类处
- python31初级[list+tuple+set+dict+str+file的成员方法]
- python 中 五种字典(dict)的遍历方法,实验法比较性能。
- python 类方法(classmethod)小实验代码
- python-numpy一些方法总结
- python中字符串常用方法
- Python实现文件内容批量追加的方法示例