重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import math
成都网站建设、成都做网站的关注点不是能为您做些什么网站,而是怎么做网站,有没有做好网站,给创新互联公司一个展示的机会来证明自己,这并不会花费您太多时间,或许会给您带来新的灵感和惊喜。面向用户友好,注重用户体验,一切以用户为中心。
a = abs
print(a(-1))
n1 = 255
print(str(hex(n1)))
def my_abs(x):
# 增加了参数的检查
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='人民大学')
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)
在开发中我们可以借助于相关插件或使用Python内置函数"help()”来查看某个函数的参数说明,以查看内置函数sorted()为例:
函数参数包括:必选参数、默认参数、可选参数、关键字参数。
1、默认参数:放在必选参数之后,计算x平方的函数:
这样的话每次计算不同幂函数都要重写函数,非常麻烦,可使用以下代码计算:
默认参数最大好处就是降低调用函数的难度。
2、可变参数:就是传入的参数个数是可变的,可以是1个、2个到任意个,还可以是0个,在参数前面加上*就是可变参数。在函数内部,参数numbers接收得到的是一个tuple,调用该函数时,可以传入任意个参数,包括0个参数:
也可以类似可变参数,先组装一个dict,然后,把该dict转换为关键字参数传进去:
按住 control 键,点击 linspace ,走你,就可以看到函数具体都需要哪些参数了
由于Python语言的动态类型特性,在集成开发环境或编辑工具编码时,给予的代码提示及自动完成功能不象静态语言工具(比如使用VisualStudio开发C#)那样充分。
实现开发过程中,我们借助于相关插件或使用Python内置函数"help()”来查看某个函数的参数说明,以查看内置函数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.