简明pyton教程学习笔记 6-10 控制流+函数+模块+数据结构+解决问题

elif从句:它事实上把两个相关联的if else-if else语句合并为一个if-elif-else语句。

在while循环中使用一个else从句。

for..in是另外一个循环语句,它在一序列的对象上 递归,range(1,5,2)给出[1,3],,else部分是可选的。

for或while循环中 终止 ,任何对应的循环else块将不执行。

=======================================================

所有变量的作用域是它们被定义的块,从它们的名称被定义的那点开始。

如果你想要为一个定义在函数外的变量赋值,那么你就得告诉Python这个变量名不是局部的,而是 全局 的。我们使用global语句完成这一功能。

函数定义的形参名后加上赋值运算符(=)和默认值,从而给形参指定默认参数值。

也可以使用名字(关键字)而不是位置来给函数指定实参。

没有返回值的return语句等价于return None;pass语句在Python中表示一个空的语句块。

文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述:print printMax.__doc__

Python把 每一样东西 都作为对象,包括函数。

=======================================================

import sys
print 'The command line arguments are:'
for i in sys.argv:
    print i
print '\n\nThe PYTHONPATH is', sys.path, '\n'


The command line arguments are:
C:\Users\TaoHe\Desktop\temp.py

The PYTHONPATH is ['C:\\Users\\xx\\Desktop', 'C:\\Python26\\Lib\\idlelib', 'C:\\Windows\\system32\\python26.zip', 'C:\\Python26\\DLLs', 'C:\\Python26\\lib', 'C:\\Python26\\lib\\plat-win', 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26', 'C:\\Python26\\lib\\site-packages'] 

脚本的名称总是sys.argv列表的第一个参数。所以,在这里,'using_sys.py'是sys.argv,你可以直接输入位于当前目录的模块。否则,你得把你的模块放在sys.path所列的目录之一。

当你在下次从别的程序输入这个模块的时候,.pyc文件是十分有用的——它会快得多,因为一部分输入模块所需的处理已经完成了。

如果你想要直接输入argv变量到你的程序中(避免在每次使用它时打sys.),那么你可以使用from sys import argv语句。如果你想要输入所有sys模块使用的名字,那么你可以使用from sys import *语句,应该避免使用from..import而使用import语句。


使用模块的__name__
#!/usr/bin/python
# Filename: using_name.py
if __name__ == '__main__':
print 'This program is being run by itself'
else:
print 'I am being imported from another module'
(源文件:code/using_name.py)
输出
$ python using_name.py
This program is being run by itself
$ python
>>> import using_name
I am being imported from another module


你为dir()提供一个模块名的时候,它返回模块定义的名称列表。如果不提供参数,它返回当前模块中定义的名称列表。


=======================================================

Python中有三种内建的数据结构——列表、元组和字典。

列表中的项目应该包括在方括号中,列表是可变的 而字符串是不可变的 。

我们在print语句的结尾使用了一个 逗号 来消除每个print语句自动打印的换行符。


元组和列表十分类似,只不过元组和字符串一样是 不可变的,元组通过圆括号中用逗号分割的项目定义。

#!/usr/bin/python
# Filename: using_tuple.py
zoo = ('wolf', 'elephant', 'penguin')
print 'Number of animals in the zoo is', len(zoo)
new_zoo = ('monkey', 'dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]

即如果你想要的是一个包含项目2的元组的时候,你应该指明singleton = (2 , ),这样Python才能区分元组和表达式中一个带圆括号的对象。

元组最通常的用法是用在打印语句中

age = 22
name = 'Swaroop'
print '%s is %d years old' % (name, age)
print 'Why is %s playing with that python?' % name


你只能使用不可变的对象(比如字符串)来作为字典的键,

#!/usr/bin/python
# Filename: using_dict.py
# 'ab' is short for 'a'ddress'b'ook
ab = { 'Swaroop' : 'swaroopch@byteofpython.info',
'Larry' : 'larry@wall.org',
'Matsumoto' : 'matz@ruby-lang.org',
'Spammer' : 'spammer@hotmail.com'
}
print "Swaroop's address is %s" % ab['Swaroop']
# Adding a key/value pair
ab['Guido'] = 'guido@python.org'
# Deleting a key/value pair
del ab['Spammer']
print '\nThere are %d contacts in the address-book\n' % len(ab)
for name, address in ab.items():
print 'Contact %s at %s' % (name, address)
if 'Guido' in ab: # OR ab.has_key('Guido')
print "\nGuido's address is %s" % ab['Guido']


列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符和切片操作符。

shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist # mylist is just another name pointing to the same object!
del shoplist[0]
print 'shoplist is', shoplist
print 'mylist is', mylist
# notice that both shoplist and mylist both print the same list without
# the 'apple' confirming that they point to the same object
print 'Copy by making a full slice'
mylist = shoplist[:] # make a copy by doing a full slice
del mylist[0] # remove first item
print 'shoplist is', shoplist
print 'mylist is', mylist
# notice that now the two lists are different


delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print delimiter.join(mylist)

Brazil_*_Russia_*_India_*_China

=======================================================

实际问题这一章节就不记录了,一些基本的文件操作,字符串操作,系统命令

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值