欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python学习笔记--2

程序员文章站 2023-12-23 23:12:39
(13)局部变量与全局变量(用global语句声明是全局的) def func(x): print 'x is', x x = 2 print 'chang...

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

上一篇:

下一篇: