重学python基础

http://linux.chinaitlab.com/manual/python_chinese/
学python已经是第4遍了,这次让我学精吧
-------
help('print')
---------
单引和双引是相同的
u‘string’ U'string' unicode
r'\nstring' R'\nstring' 不转意
‘string \
string'
‘’‘string
string ’‘’
---------
运算符http://linux.chinaitlab.com/manual/python_chinese/ch05s02.html
/ 除
// 取整除
% 取余数
运算符优先级http://linux.chinaitlab.com/manual/python_chinese/ch05s03.html
-----------
在Python中没有switch语句
if guess == number:
elif guess < number:
else:
或字典
-----------
while running:
else:
---------------
for i in range(1, 5):
print i
else:
print 'The for loop is over'
---------------
靠,啥玩意都能用else
--------
#!/usr/bin/python
# Filename: break.py

while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
print 'Length of the string is', len(s)
print 'Done'

break也能在for中使用
#!/usr/bin/python
# Filename: continue.py

while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
continue
print 'Input is of sufficient length'
# Do other kinds of processing here...

---------------global-------
#!/usr/bin/python
# Filename: func_global.py

def func():
global x

print 'x is', x
x = 2
print 'Changed local x to', x

x = 50
func()
print 'Value of x is', x

----默认参数-----
#!/usr/bin/python
# Filename: func_default.py

def say(message, times = 1):
print message * times

say('Hello')
say('World', 5)

重要
[color=red]只有在形参表末尾的那些参数可以有默认参数值[/color],即你不能在声明函数形参的时候,先声明有默认值的形参而后声明没有默认值的形参。
--------------关键参数------
#!/usr/bin/python
# Filename: func_key.py

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)

[color=red]可以不按照顺序[/color],有默认值的可以不赋值
------pass---------
def someFunction():
pass
---------__doc__-----------
#!/usr/bin/python
# Filename: func_doc.py

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

The two values must be integers.'''
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__

[code="java"][/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值