検索

Pythonの関数の紹介

Aug 05, 2017 pm 04:08 PM
python導入

下面是我做的几个用列:


 1 #python中的函数定义,使用和传参 2 def_str = '''\ 3     python中的函数以如下形式声明: 4  5     def 函数名称([参数1,参数2,参数3......]): 6         执行语句 7  8     如: 9 10     def helloWorld():11         print('hello')12 13     if __name__ == '_main__':14         helloWorld()15 16     输出:hello17     '''18 print(def_str)19 20 #下面进行举例说明21 22 def helloWorld():23     print('输出:hello')24 25 if __name__ == '__main__':26     helloWorld()27     28 print('''\29     ################################################30     31     函数可以带参数和返回值,参数将按从左到右的匹配,32     参数可设置默认值,当使用函数时没给相应的参数时,33     会按照默认值进行赋值34 35     ################################################36     ''')37 38 #定义一个方法:x的y次方39 def myMethod(x,y):40     return x**y41 42 def fib(n):43     a , b = 0 , 144     while a  0:54         for i in range(0,length):55             oldList.append(i)56         return oldList57     else:58         return '你输入的长度小于0'59 60 def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):61     while True:62         ok = input(prompt)63         if ok in ('y', 'ye', 'yes'):64             return True65         if ok in ('n', 'no', 'nop', 'nope'):66             return False67         retries = retries - 168         if retries <p>运行效果:</p><p class="cnblogs_code"><br></p><pre class="brush:php;toolbar:false">Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================
>>> 
    python中的函数以如下形式声明:    def 函数名称([参数1,参数2,参数3......]):
        执行语句

    如:    def helloWorld():        print('hello')    if __name__ == '_main__':
        helloWorld()

    输出:hello
    
输出:hello    ################################################    
    函数可以带参数和返回值,参数将按从左到右的匹配,
    参数可设置默认值,当使用函数时没给相应的参数时,
    会按照默认值进行赋值    ################################################
    3 的 4 次方( 3 ** 4 ) =  81函数fib(n),当n = 20000 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 你输入的长度小于0
y输出:hello
Yes or no, please!
y
Yes or no, please!
y
Yes or no, please!
y
Yes or no, please!
y
Traceback (most recent call last):
  File "E:/Python33/python_workspace/test_function.py", line 80, in <module>
    ask_ok('y')
  File "E:/Python33/python_workspace/test_function.py", line 69, in ask_ok    raise IOError('refusenik user')
OSError: refusenik user>>></module>

 

以上がPythonの関数の紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Pythonアレイに要素をどのように追加しますか?Pythonアレイに要素をどのように追加しますか?Apr 30, 2025 am 12:19 AM

inpython、youappendelementStoalistusingtheappend()method.1)useappend()forsingleelements:my_list.append(4).2)useextend()or = formultipleElements:my_list.extend(another_list)ormy_list = [4,5,6] .3)forspecificpositions:my_list.insert(1,5).beaware

シバン関連の問題をどのようにデバッグしますか?シバン関連の問題をどのようにデバッグしますか?Apr 30, 2025 am 12:17 AM

シェバンの問題をデバッグする方法には次のものがあります。1。シバン行をチェックして、それがスクリプトの最初の行であり、接頭辞スペースがないことを確認します。 2.通訳パスが正しいかどうかを確認します。 3.通訳を直接呼び出してスクリプトを実行して、シェバンの問題を分離します。 4. StraceまたはTrustsを使用して、システムコールを追跡します。 5.シバンに対する環境変数の影響を確認してください。

Pythonアレイから要素をどのように削除しますか?Pythonアレイから要素をどのように削除しますか?Apr 30, 2025 am 12:16 AM

pythonlistscanbemanipulatedsingseveralmethodstoremoveElements:1)theremove()methodremovesthefirstoccurrenceofaspecifiedValue.2)thepop()methop()methodremovessanelementatagivenindex.3)thedelstatementementementementementementementementementemoritemoricedex.4)

Pythonリストに保存できるデータ型は何ですか?Pythonリストに保存できるデータ型は何ですか?Apr 30, 2025 am 12:07 AM

Integers、strings、floats、booleans、otherlists、anddictionaryを含むpythonlistscanstoreanydatype

Pythonリストで実行できる一般的な操作は何ですか?Pythonリストで実行できる一般的な操作は何ですか?Apr 30, 2025 am 12:01 AM

PythonListsSupportNumersoperations:1)AddingElementSwithAppend()、Extend()、Andinert()

numpyを使用してマルチディメンシャルアレイをどのように作成しますか?numpyを使用してマルチディメンシャルアレイをどのように作成しますか?Apr 29, 2025 am 12:27 AM

Numpyを使用して多次元配列を作成すると、次の手順を通じて実現できます。1)numpy.array()関数を使用して、np.array([[1,2,3]、[4,5,6]])などの配列を作成して2D配列を作成します。 2)np.zeros()、np.ones()、np.random.random()およびその他の関数を使用して、特定の値で満たされた配列を作成します。 3)アレイの形状とサイズの特性を理解して、サブアレイの長さが一貫していることを確認し、エラーを回避します。 4)np.reshape()関数を使用して、配列の形状を変更します。 5)コードが明確で効率的であることを確認するために、メモリの使用に注意してください。

Numpyアレイの「ブロードキャスト」の概念を説明します。Numpyアレイの「ブロードキャスト」の概念を説明します。Apr 29, 2025 am 12:23 AM

BroadcastinginNumPyisamethodtoperformoperationsonarraysofdifferentshapesbyautomaticallyaligningthem.Itsimplifiescode,enhancesreadability,andboostsperformance.Here'showitworks:1)Smallerarraysarepaddedwithonestomatchdimensions.2)Compatibledimensionsare

データストレージ用のリスト、array.array、およびnumpy配列を選択する方法を説明します。データストレージ用のリスト、array.array、およびnumpy配列を選択する方法を説明します。Apr 29, 2025 am 12:20 AM

Forpythondatastorage、chooseLists forfficability withmixeddatypes、array.arrayformemory-efficienthogeneousnumericaldata、およびnumpyArrays foradvancednumericalcomputing.listSareversatilebuteficient efficient forlargeNumericaldatates;

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。