python 函数语法中的func(variable[,variable]) 怎么理解

表示其是可选参数。

一般是这么调用的:

sum([1,2,3,4])

返回 10

也可以填上那个可选参数,

  • start -- 指定相加的参数,如果没有设置这个值,默认为0。

那么sum([1,2,3,4],2)

则在计算完迭代对象后再额外加上2

返回12

② Python代码中func(*args, **kwargs)是什么意思

这是一种特殊的语法,在函数定义中使用*args和**kwargs传递可变长参数。*args用作传递非命名键值可变长参数列表(位置参数); **kwargs用作传递键值可变长参数列表。

  • 示例 *args

deftest_var_args(farg,*args):
print"formalarg:",farg
forarginargs:
print"anotherarg:",arg

test_var_args(1,"two",3)
#输出
formalarg:1
anotherarg:two
anotherarg:3
  • 示例 **kwargs

deftest_var_kwargs(farg,**kwargs):
print"formalarg:",farg
forkeyinkwargs:
print"anotherkeywordarg:%s:%s"%(key,kwargs[key])

test_var_kwargs(farg=1,myarg2="two",myarg3=3)
#输出
formalarg:1
anotherkeywordarg:myarg2:two
anotherkeywordarg:myarg3:3

③ 用 PYTHON写FUNCTION

>>> def s(M):
... if M==1:
... return 1
... else:
... return s(M-1)+1.0/M

>>> import math
>>> def Gauss(x,m=0,s=1):
... return (1/(math.sqrt(2*math.pi)*s))*math.exp(-0.5*((x-m)/s)**2)
...
>>> li = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
>>> print map(Gauss,li)
[1.4867195147342977e-006, 0.00013383022576488537, 0.0044318484119380075, 0.053990966513188063, 0.24197072451914337, 0.3989422804014327, 0.24197072451914337, 0.053990966513188063, 0.0044318484119380075, 0.00013383022576488537, 1.4867195147342977e-006]
>>>

④ python function

恰好做过...
您安好.
----------------------------
# File : warmup.py

def lines_starts_with(f, s):
____"""Return a list containing the lines of the given open file that start with
____the given str. Exclude leading and trailing whitespace.
____
____Arguments:
____- `f`: the file
____- `s`: str at the beginning of the line
____"""
____result = []
____for line in f:
________if line.startswith(s):
____________result.append(line.strip())
____return result

def file_to_dictionary(f, s):
____"""Given an open file that contains a unique string followed by a given
____string delimiter and more text on each line, return a dictionary that
____contains an entry for each line in the file: the unique string as the key
____and the rest of the text as the value (excluding the delimiter, and leading
____and trailing whitespace).
____
____Arguments:
____- `f`: the file
____- `s`: the delimiter str
____"""
____result = {}
____for line in f:
________k, _, v = line.partition(s)
________result[k.strip()] = v.strip()
____return result

def merge_dictionaries(d1, d2):
____"""Return a dictionary that is the result of merging the two given
____dictionaries. In the new dictionary, the values should be lists. If a key is
____in both of the given dictionaries, the value in the new dictionary should
____contain both of the values from the given dictionaries, even if they are the
____same.
____merge_dictionaries({ 1 : 'a', 2 : 9, -8 : 'w'}, {2 : 7, 'x' : 3, 1 : 'a'})
____should return {1 : ['a', 'a'], 2 : [9, 7], -8 : ['w'], 'x' : [3]}
____Arguments:
____- `d1`, 'd2': dicts to be merged
____"""
____result = {}
____for key in d1:
________if key in d2:
____________result[key] = [d1[key], d2[key]]
________else:
____________result[key] = [d1[key]]
____for key in d2:
________if not key in result:
____________result[key] = [d2[key]]
____return result

-----------------------------------------------------
# File fasta.py

from warmup import *

def count_sequences(f):
____"""Return the number of FASTA sequences in the given open file. Each
____sequence begins with a single line that starts with >.
____
____Arguments:
____- `f`: the file
____"""
____return len(lines_starts_with(f, '>'))

def get_sequence_list(f):
____"""Return a nested list where each element is a 2-element list containing a
____FASTA header and a FASTA sequence from the given open file (both strs).
____
____Arguments:
____- `f`: the file
____"""
____result = []
____current_key = ''
____current_data = ''
____for line in f:
________if line.startswith('>'):
____________if current_data:
________________result.append([current_key, current_data])
________________current_data = ''
____________current_key = line.strip()
________else:
____________current_data+= line.strip()
____result.append([current_key, current_data])
____return result

____
def merge_files(f1, f2):
____"""Return a nested list containing every unique FASTA header and sequence
____pair from the two open input files. Each element of the list to be returned
____is a 2-element list containing a FASTA header and a FASTA sequence.
____
____Arguments:
____- `f1`, 'f2': The File
____"""
____seq = get_sequence_list(f1) + get_sequence_list(f2)
____seq = list( set(seq) )
____return seq

def get_codons(s):
____"""Return a list of strings containing the codons in the given DNA sequence.
____
____Arguments:
____- `s`: DNA sequence, divied by 3
____"""
____return [s[3*i : 3*(i+1)] for i in xrange(len(s)/3)]

def get_amino_name_dict(f):
____"""Return a dictionary constructed from the given open file, where the keys
____are amino acid codes and the values are the amino acid names.
____
____Arguments:
____- `f`: the file, like amino_names.txt
____"""
____return file_to_dictionary(f, ' ')

def get_codon_amino_dict(f):
____"""Return a dictionary where the keys are codons and the values are amino
____acid codes from the given open file.
____
____Arguments:
____- `f`: the file, like codon_aminos.txt
____"""
____return file_to_dictionary(f, ':')

def translate(s, d):
____"""Return the given DNA sequence (the str parameter) translated into its
____amino acid codes, according to the given dictionary where the keys are
____codons and the values are amino acid codes. You may assume that the length
____of the string is divisible by 3.
____
____Arguments:
____- `s`: given DNA sequence
____- `d`: given dictionary
____"""
____codons = get_codons(s)
____result = []
____for i in codes:
________result.append(d[i])
____return result

def codon_to_name(s, d1, d2):
____"""Return the name of the amino acid for the given codon (the str
____parameter).
____
____Arguments:
____- `s`: given codon
____- `d1`: codons for keys and amino acid codes for values
____- `d2`: amino acid codes for keys and name for values
____"""
____return d2[d1[s]]

⑤ pythondef func(a,b): return a>>b 是什么意思

>>右移动运算符:把">>"左边的运算数的各二进位全部右移若干位,">>"右边的数指定移动的位数
5二级制 00000101,右移2位 就是 00000001

⑥ python函数中def func(*args)这里*的作用是什么

deffun_var_args(farg,*args):
print"arg:",farg
forvalueinargs:
print"anotherarg:",value

fun_var_args(1,"two",3)#*args可以抄当作可袭容纳多个变量组成的list

⑦ python内嵌函数与闭包里func是不是有位置参数return func(),没有位置参数return func

定义个函数,查看带括号和不带括号的type.
def func1():
pass
print(type(func1))
#执行结果:<class 'function'>
print(type(func1()))
#执行结果:<class 'NoneType'>
由此可见:
使用回return func 时返回的是func 这个函答数;
使用return func() 时返回的是func() 执行后的返回值,如果func()函数没有返回值则返回值为None,
func函数如果有其他打印语句也会一起执行。

⑧ python function 运用

应该是你定义了id()这个函数吧,id是python的内置函数,传入一个对象,返回对应的内存号。
不建议你定义同名函数,修改个名字吧。
不过即使同名也不会有问题,应该是你使用的时候没有导入你自己创建的那个函数,所以报错了。

如果解决了您的问题请点赞!
如果未解决请继续追问

⑨ Python里method和function的区别

def本身是一个函数对象。也可以叫它“方法”。属于对象的函数,就是对象的属性。def定义了一个模块的变量,或者说是类的变量。 python 的函数和其他语言的函数有很大区别。它是可以被其他变量覆盖的

⑩ python中TextorFunc具体应该怎么理解

这是Python函数可变参数 args及kwargs

*args表示任何多个无名参数,它是一个tuple
**kwargs表示关键字参数,它是一个dict