《简明 Python 教程》笔记-----基础知识
《简明 Python 教程》笔记-----面向对象及系统相关
number = 1
while number < 5:
print number
number += 1
print 'Done'
上面的代码会一直输出1,因为下一句number+=1不在while的范围内
if
guess == number:
print
'a.'
elif
guess < number:
print
'b'
else
:
print
'c'
4、while语句global x
print 'x is', x
x = 2
print 'Changed local x to', x
x = 50
func()
print 'Value of x is', x
def func(a, b=5)
有效,def func(a=5, b)无效
输入:
def func(a, b=5, c=10):
print 'a is', a, 'and b is', b, 'and c is', c
func(3, 7)
func(25, c=24)
func(c=50, a=100)
输出:
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
def printMax(x):
'''Prints the maximum of two numbers.
The two values must be integers.'''
x = int(x)
print y
printMax(3)
print printMax.__doc__ #调用函数文档
“文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述。 强烈建议 你在你的函数中使用文档字符串时遵循这个惯例。”
13、模块
每个.py文件都是一个模块
模块间可互相调用
# Filename: mymodule1.py
def sayhi():
print 'Hi, this is mymodule speaking.'
version = '0.1'
# Filename: mymodule_demo.py
import mymodule1
mymodule1.sayhi()
print 'Version', mymodule1.version
# Filename: mymodule_demo2.py
from mymodule import sayhi, version
sayhi()
print 'Version', version
demo和demo2都会输出:
Hi, this is mymodule speaking.
Version 0.1
14、可以用dir()函数来列出模块定义的标识符。标识符有函数、类和变量。
15、我们在print
语句的结尾使用了一个 逗号 来消除每个print
语句自动打印的换行符
16、数据结构:
列表:元素是可变的
shoplist = [
'apple'
,
'mango'
,
'carrot'
,
'banana'
]
print
len
(shoplist),
'
shoplist.append(
'rice'
)
shoplist.sort()
for
item
in
shoplist:
print
item,
元组:元素是不可变的
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
]
字典:删除,插入,查找,遍历
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']
ab['Guido'] = 'guido@python.org'
del ab['Spammer']
print '%d' % len(ab)
for name, address in ab.items():
print 'Contact %s at %s' % (name, address)
if 'Guido' in ab:
print "\nGuido's address is %s" % ab['Guido']
序列:切片操作符,左闭右开
shoplist = [
'apple'
,
'mango'
,
'carrot'
,
'banana'
]
print
'Item 1 to 3 is'
, shoplist[
1
:
3
]
print
'Item 2 to end is'
, shoplist[
2
:
]
print
'Item start to end is'
, shoplist[:]
name =
'swaroop'
print
'characters 1 to 3 is'
, name[
1
:
3
]
print
'characters 2 to end is'
, name[
2
:]
print
'characters start to end is'
, name[:]
参考:感觉就是指针
如果直接复制‘=’一个列表,那是原参数本身,同一块内存
如果是想重新申请一块相同内存的新的参数,那就得用切片操作符来拷贝
shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist
del shoplist[0]
输出是:
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist[:]
del shoplist[0]
输出是:
shoplist is ['mango', 'carrot', 'banana']
mylist is ['apple', 'mango', 'carrot', 'banana']
字符串的方法:一些字符串的处理方法,给的不全,有判断开头,查找某一部分,将列表转化成字符串(间隔差值)
name = 'Swaroop'
if name.startswith('Swa'):
print 1
if 'a' in name:
print 1
if name.find('war') != -1:
print 1
delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print delimiter.join(mylist)