Python 漫游记

基础语法

  • 代码格式与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)

输出:
hello
worldworldworldworldworld
  • 关键字参数
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 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', zoo
print('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 3
All 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 penguin
Number 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.com

There are 3 contacts in the address-book

Contact Swaroop at swaroop@swaroopch.com
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org

Guido's address is guido@python.org
  • 集合
    注意集合与列表上写法的不同
>>> bri = set(['brazil', 'russia', 'india'])
>>> 'india' in bri
True
>>> 'usa' in bri
False
>>> bric = bri.copy()
>>> bric.add('china')
>>> bric.issuperset(bri)   #是否是扩展集合
True
>>> bri.remove('russia')
>>> bri & bric # OR bri.intersection(bric)
{'brazil', 'india'}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值