Python 漫游记

来源:互联网 发布:正在设置您的mac要多久 编辑:程序博客网 时间:2024/05/21 10:25

基础语法

  • 代码格式与c++/java不同,它一般没有“;”分隔
  • 在if else elif while for function()后用“:”//True False 大写
  • 没有声明
  • 缩进代码,用代码块的方式表达所属关系
  • pass代表没有内容的语句块

函数参数

  • 默认参数值:使用“=” times=1//默认参数放在最后,放在参数列表前面没有用
def say(message,times=1):    print(message*times)say('hello')say('world',5)输出:helloworldworldworldworldworld
  • 关键字参数
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 10a is 25 and b is 5 and c is 24a is 100 and b is 5 and c is 50
  • 可变参数//以后再说

文档字符串

def print_max(x, y):'''Prints the maximum of two numbers.打印两个数值中的最大数。The two values must be integers.这两个数都应该是整数'''#第一行以句号结束# 如果可能,将其转换至整数类型x = int(x)y = int(y)if x > y:print(x, 'is maximum')else:print(y, 'is maximum')print_max(3, 5)print(print_max.__doc__)

数据结构

  • 列表
shoplist = ['apple', 'mango', 'carrot', 'banana']print('I have', len(shoplist), 'items to purchase.')print('These items are:', end=' ')#end=''过一个空格来结束输出工作,而不是通常的换行for item in shoplist:    print(item,end='')
  • 元组Tuple
    元组(Tuple)用于将多个对象保存到一起。你可以将它们近似地看作列表,但是元组不能提
    供列表类能够提供给你的广泛的功能。元组的一大特征类似于字符串,它们是不可变的,也
    就是说,你不能编辑或更改元组。
    元组是通过特别指定项目来定义的,在指定项目时,你可以给它们加上括号,并在括号内部
    用逗号进行分隔。
    元组通常用于保证某一语句或某一用户定义的函数可以安全地采用一组数值,意即元组内的
    数值不会改变。
# 我会推荐你总是使用括号# 来指明元组的开始与结束# 尽管括号是一个可选选项。# 明了胜过晦涩,显式优于隐式。zoo = ('python', 'elephant', 'penguin')print('Number of animals in the zoo is', len(zoo))new_zoo = 'monkey', 'camel', zooprint('Number of cages 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])print('Number of animals in the new zoo is',len(new_zoo)-1+len(new_zoo[2]))输出:Number of cages in the new zoo is 3All animals in new zoo are ('monkey', 'camel', ('python', 'elephant', 'penguin'))Animals brought from old zoo are ('python', 'elephant', 'penguin')Last animal brought from old zoo is penguinNumber of animals in the new zoo is 5
  • 字典
    注意的是你只能使用不可变的对象(如字符串)作为字典的键值,但是你可以使用可
    变或不可变的对象作为字典中的值。
    形式: d = {key : value1 , key2 : value2}
# “ab”是地址(Address)簿(Book)的缩写ab = {'Swaroop': 'swaroop@swaroopch.com','Larry': 'larry@wall.org','Matsumoto': 'matz@ruby-lang.org','Spammer': 'spammer@hotmail.com'}print("Swaroop's address is", ab['Swaroop'])# 删除一对键值—值配对del ab['Spammer']print('\nThere are {} contacts in the address-book\n'.format(len(ab)))for name, address in ab.items():print('Contact {} at {}'.format(name, address))# 添加一对键值—值配对ab['Guido'] = 'guido@python.org'if 'Guido' in ab:print("\nGuido's address is", ab['Guido'])form输出:Swaroop's address is swaroop@swaroopch.comThere are 3 contacts in the address-bookContact Swaroop at swaroop@swaroopch.comContact Matsumoto at matz@ruby-lang.orgContact Larry at larry@wall.orgGuido's address is guido@python.org
  • 集合
    注意集合与列表上写法的不同
>>> bri = set(['brazil', 'russia', 'india'])>>> 'india' in briTrue>>> 'usa' in briFalse>>> bric = bri.copy()>>> bric.add('china')>>> bric.issuperset(bri)   #是否是扩展集合True>>> bri.remove('russia')>>> bri & bric # OR bri.intersection(bric){'brazil', 'india'}
原创粉丝点击