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