import math
成都網(wǎng)站建設(shè)、成都做網(wǎng)站的關(guān)注點(diǎn)不是能為您做些什么網(wǎng)站,而是怎么做網(wǎng)站,有沒有做好網(wǎng)站,給創(chuàng)新互聯(lián)公司一個展示的機(jī)會來證明自己,這并不會花費(fèi)您太多時間,或許會給您帶來新的靈感和驚喜。面向用戶友好,注重用戶體驗,一切以用戶為中心。
a = abs
print(a(-1))
n1 = 255
print(str(hex(n1)))
def my_abs(x):
# 增加了參數(shù)的檢查
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x = 0:
return x
else:
return -x
print(my_abs(-3))
def nop():
pass
if n1 = 255:
pass
def move(x, y, step, angle=0):
nx = x + step * math.cos(angle)
ny = y - step * math.sin(angle)
return nx, ny
x, y = move(100, 100, 60, math.pi / 6)
print(x, y)
tup = move(100, 100, 60, math.pi / 6)
print(tup)
print(isinstance(tup, tuple))
def quadratic(a, b, c):
k = b * b - 4 * a * c
# print(k)
# print(math.sqrt(k))
if k 0:
print('This is no result!')
return None
elif k == 0:
x1 = -(b / 2 * a)
x2 = x1
return x1, x2
else:
x1 = (-b + math.sqrt(k)) / (2 * a)
x2 = (-b - math.sqrt(k)) / (2 * a)
return x1, x2
print(quadratic(2, 3, 1))
def power(x, n=2):
s = 1
while n 0:
n = n - 1
s = s * x
return s
print(power(2))
print(power(2, 3))
def enroll(name, gender, age=8, city='BeiJing'):
print('name:', name)
print('gender:', gender)
print('age:', age)
print('city:', city)
enroll('elder', 'F')
enroll('android', 'B', 9)
enroll('pythone', '6', city='AnShan')
def add_end(L=[]):
L.append('end')
return L
print(add_end())
print(add_end())
print(add_end())
def add_end_none(L=None):
if L is None:
L = []
L.append('END')
return L
print(add_end_none())
print(add_end_none())
print(add_end_none())
def calc(*nums):
sum = 0
for n in nums:
sum = sum + n * n
return sum
print(calc(1, 2, 3))
print(calc())
l = [1, 2, 3, 4]
print(calc(*l))
def foo(x, y):
print('x is %s' % x)
print('y is %s' % y)
foo(1, 2)
foo(y=1, x=2)
def person(name, age, **kv):
print('name:', name, 'age:', age, 'other:', kv)
person('Elder', '8')
person('Android', '9', city='BeiJing', Edu='人民大學(xué)')
extra = {'city': 'Beijing', 'job': 'Engineer'}
person('Jack', 24, **extra)
def person2(name, age, *, city, job):
print(name, age, city, job)
person2('Pthon', 8, city='BeiJing', job='Android Engineer')
def person3(name, age, *other, city='BeiJing', job='Android Engineer'):
print(name, age, other, city, job)
person3('Php', 18, 'test', 1, 2, 3)
person3('Php2', 28, 'test', 1, 2, 3, city='ShangHai', job='Pyhton Engineer')
def test2(a, b, c=0, *args, key=None, **kw):
print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'key=', key, 'kw =', kw)
test2(1, 2, 3, 'a', 'b', 'c', key='key', other='extra')
args = (1, 2, 3, 4)
kw = {'d': 99, 'x': '#'}
test2(*args, **kw)
在開發(fā)中我們可以借助于相關(guān)插件或使用Python內(nèi)置函數(shù)"help()”來查看某個函數(shù)的參數(shù)說明,以查看內(nèi)置函數(shù)sorted()為例:
函數(shù)參數(shù)包括:必選參數(shù)、默認(rèn)參數(shù)、可選參數(shù)、關(guān)鍵字參數(shù)。
1、默認(rèn)參數(shù):放在必選參數(shù)之后,計算x平方的函數(shù):
這樣的話每次計算不同冪函數(shù)都要重寫函數(shù),非常麻煩,可使用以下代碼計算:
默認(rèn)參數(shù)最大好處就是降低調(diào)用函數(shù)的難度。
2、可變參數(shù):就是傳入的參數(shù)個數(shù)是可變的,可以是1個、2個到任意個,還可以是0個,在參數(shù)前面加上*就是可變參數(shù)。在函數(shù)內(nèi)部,參數(shù)numbers接收得到的是一個tuple,調(diào)用該函數(shù)時,可以傳入任意個參數(shù),包括0個參數(shù):
也可以類似可變參數(shù),先組裝一個dict,然后,把該dict轉(zhuǎn)換為關(guān)鍵字參數(shù)傳進(jìn)去:
按住 control 鍵,點(diǎn)擊 linspace ,走你,就可以看到函數(shù)具體都需要哪些參數(shù)了
由于Python語言的動態(tài)類型特性,在集成開發(fā)環(huán)境或編輯工具編碼時,給予的代碼提示及自動完成功能不象靜態(tài)語言工具(比如使用VisualStudio開發(fā)C#)那樣充分。
實現(xiàn)開發(fā)過程中,我們借助于相關(guān)插件或使用Python內(nèi)置函數(shù)"help()”來查看某個函數(shù)的參數(shù)說明,以查看內(nèi)置函數(shù)sorted()為例:
help(sorted)Help on built-in function sorted in module builtins: sorted(iterable, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customise the sort order, and the reverse flag can be set to request the result in descending order.