python学习笔记--2

13)局部变量与全局变量(用global语句声明是全局的)

def func(x):
    print ‘x is‘, x
    x = 2
    print ‘Changed local x to‘, x

x = 50
func(x)
print ‘x is still‘, x

 

打印结果为:x is 50

changed loacal x to 2

x is still 50

 

def func(x):

global x
    print ‘x is‘, x
    x = 2
    print ‘Changed local x to‘, x

x = 50
func(x)
print ‘x is still‘, x

打印结果为:x is 50

changed loacal x to 2

value of x is 2

14只有在形参表末尾的那些参数可以有默认参数值,即你不能在声明函数形参的时候,先声明有默认值的形参而后声明没有默认值的形参。这是因为赋给形参的值是根据位置而赋值的。例如,def func(a, b=5)是有效的,但是deffunc(a=5, b)是 无效 的。

(15)使用关键参数

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

(16)pass语句在python中表示一个空的语句块

(17)docstrings 是python的文档字符串,文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述。

def printMax(x, y):
    ‘‘‘Prints the maximum of two numbers.

    The two values must beintegers.‘‘‘

    x = int(x) # convert to integers, if possible
    y = int(y)

    if x > y:
        print x, ‘is maximum‘
    else:
        print y, ‘is maximum‘

printMax(3, 5)
print printMax.__doc__(双下划线)

打印结果:5 is maximum
Prints the maximum of two numbers.

        The two values must beintegers.

(18)使用模块可以在其他程序中重用很多函数,模块基本上就是一个包含了所有你定义的函数和变量的文件。为了在其他程序中重用模块,模块的文件名必须以.py为扩展名。可以创建自己的模块,如下:

# Filename: mymodule.py

def sayhi():
    print ‘Hi, this is mymodule speaking.‘

version = ‘0.1‘

# End of mymodule.py

 

# Filename: mymodule_demo.py

import mymodule

mymodule.sayhi()
print ‘Version‘, mymodule.version

打印结果:

Hi, thisis mymodule speaking.
Version 0.1

(19)在python中有三种内建的数据结构---列表、元组和字典。列表是可变的,元组是不可变的,字典是把键(名字)和值(详细情况)联系在一起,键是唯一的,键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2 }。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。使用列表的实例如下:

# Filename: using_list.py

# This is my shopping list

shoplist = [‘apple‘, ‘mango‘, ‘carrot‘, ‘banana‘]

print ‘I have‘, len(shoplist),‘items to purchase.‘

print ‘These items are:‘, # Notice the comma at end of theline
for item in shoplist:
    print item,

print ‘\nI also have to buy rice.‘
shoplist.append(‘rice‘)
print ‘My shopping list is now‘, shoplist

print ‘I will sort my list now‘
shoplist.sort()
print ‘Sorted shopping list is‘, shoplist

print ‘The first item I will buy is‘, shoplist[0]
olditem = shoplist[0]
del shoplist[0]
print ‘I bought the‘, olditem
print ‘My shopping list is now‘, shoplist

打印结果如下:

I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now [‘apple‘, ‘mango‘, ‘carrot‘, ‘banana‘, ‘rice‘]
I will sort my list now
Sorted shopping list is [‘apple‘, ‘banana‘, ‘carrot‘, ‘mango‘, ‘rice‘]
The first item I will buy is apple
I bought the apple
My shopping list is now [‘banana‘, ‘carrot‘, ‘mango‘, ‘rice‘]

使用元组的实例如下:

# 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]

 

打印结果为:

Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are (‘monkey‘, ‘dolphin‘, (‘wolf‘, ‘elephant‘,‘penguin‘))
Animals brought from old zoo are (‘wolf‘, ‘elephant‘, ‘penguin‘)
Last animal brought from old zoo is penguin

 

使用元组输出的实例:print语句可以使用跟着%符号的项目元组的字符串。这些字符串具备定制的功能。定制让输出满足某种特定的格式。定制可以是%s表示字符串或%d表示整数。元组必须按照相同的顺序来对应这些定制

# Filename:print_tuple.py

age = 22
name = ‘Swaroop‘

print ‘%s is %d years old‘ % (name, age)
print ‘Why is %s playing with that python?‘ % name

打印结果为:

Swaroop is 22 years old
Why is Swaroop playing with that 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‘]

打印结果如下:

Swaroop‘s address isswaroopch@byteofpython.info

There are 4 contacts in the address-book

Contact Swaroop at swaroopch@byteofpython.info
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org
Contact Guido at guido@python.org

Guido‘s address is guido@python.org

(20)序列的两个主要特点是索引操作符和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。

shoplist = [‘apple‘, ‘mango‘, ‘carrot‘, ‘banana‘]

# Indexing or ‘Subscription‘ operation
print ‘Item 0 is‘, shoplist[0]
print ‘Item 1 is‘, shoplist[1]
print ‘Item 2 is‘, shoplist[2]
print ‘Item 3 is‘, shoplist[3]
print ‘Item -1 is‘, shoplist[-1](shoplist[-1]表示序列的最后一个元素)
print ‘Item -2 is‘, shoplist[-2](shoplist[-2]抓取序列的倒数第二个项目。)

# Slicing on a list
print ‘Item 1 to 3 is‘, shoplist[1:3]

#(shoplist[1:3]返回从位置1开始,包括位置2,但是停止在位置3的一个序列切片,因此返回一个含有两个项目的切片。)
print ‘Item 2 to end is‘, shoplist[2:]
print ‘Item 1 to -1 is‘, shoplist[1:-1]
print ‘Item start to end is‘, shoplist[:]

#shoplist[:]返回整个序列的拷贝

# Slicing on a string
name = ‘swaroop‘
print ‘characters 1 to 3 is‘, name[1:3]
print ‘characters 2 to end is‘, name[2:]
print ‘characters 1 to -1 is‘, name[1:-1]
print ‘characters start to end is‘, name[:]

打印结果:

Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Item 1 to 3 is [‘mango‘, ‘carrot‘]
Item 2 to end is [‘carrot‘, ‘banana‘]
Item 1 to -1 is [‘mango‘, ‘carrot‘]
Item start to end is [‘apple‘, ‘mango‘, ‘carrot‘, ‘banana‘]
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop

python学习笔记--2

上一篇:C# 调用C++ 注意事项


下一篇:python - __str__ 和 __repr__