Python キーワード - 導入
Python のキーワード |セット1
その他のキーワード:
16.試してみる : このキーワードは例外処理に使用されます。 キーワード「例外」を使用してコード内のエラーを捕捉するために使用されます。 「try」ブロック内のコードは、ブロックの実行以外のタイプのエラーがあるかどうかチェックされます。
17.除く : 上で説明したように、これは例外をキャッチするために「try」と連携して機能します。
18.上げる : 明示的に例外を発生させる例外処理にも使用されます。
19. ついに : 「try」ブロックの結果が何であっても、「finally」と呼ばれるブロックは常に実行されます。詳細記事 - Python での例外処理
intを文字列Javaに変換する方法
20. のために : このキーワードはフローの制御とループに使用されます。
21. その間 : フローと for ループの制御に使用される「for」と同様の働きがあります。
22.パス : Python の null ステートメントです。これに遭遇しても何も起こりません。これはインデントエラーを防ぐために使用され、プレースホルダーとして使用されます。
詳細記事 - しばらくの間
23.インポート : このステートメントは、現在のプログラムに特定のモジュールを組み込むために使用されます。
24.から : 一般的に import from とともに使用され、インポートされたモジュールから特定の機能をインポートするために使用されます。
25. として : このキーワードは、インポートされたモジュールのエイリアスを作成するために使用されます。つまり、インポートされたモジュールに新しい名前を付けます。たとえば、数学を mymath としてインポートします。
詳細記事 - からおよびとしてインポート
26.ラムダ : このキーワードは、内部的にステートメントを許可しないインライン関数を返すために使用されます。詳細記事 - マップフィルターラムダ
27. 戻る : このキーワードは関数から戻るために使用されます。詳細記事 - Python での戻り値 。
リストのインデックス
28. 収量 : このキーワードは return ステートメントと同様に使用されますが、ジェネレーターを返すために使用されます。詳細記事 - 利回りキーワード
29. と : このキーワードは、コンテキスト マネージャーによって定義されたメソッド内でコード ブロックの実行をラップするために使用されます。このキーワードは、日常のプログラミングではあまり使用されません。
30.インチ : このキーワードは、コンテナに値が含まれているかどうかを確認するために使用されます。このキーワードは、コンテナーをループするためにも使用されます。
31. : このキーワードは、オブジェクトの同一性をテストするために使用されます。つまり、両方のオブジェクトが同じメモリ位置を使用しているかどうかを確認します。
Python# Python code to demonstrate working of # in and is # using 'in' to check if 's' in 'geeksforgeeks': print ('s is part of geeksforgeeks') else : print ('s is not part of geeksforgeeks') # using 'in' to loop through for i in 'geeksforgeeks': print (iend=' ') print ('r') # using is to check object identity # string is immutable( cannot be changed once allocated) # hence occupy same memory location print (' ' is ' ') # using is to check object identity # dictionary is mutable( can be changed once allocated) # hence occupy different memory location print ({} is {})
出力:
s is part of geeksforgeeks g e e k s f o r g e e k s True False
32. グローバル : このキーワードは、関数内の変数をグローバル スコープであるように定義するために使用されます。
33. 非ローカル : このキーワードはグローバルと同様に機能しますが、グローバルではなく、入れ子関数の場合に外側の囲み関数の変数を指す変数を宣言します。
e-rモデル図Python
# Python code to demonstrate working of # global and non local #initializing variable globally a = 10 # used to read the variable def read(): print (a) # changing the value of globally defined variable def mod1(): global a a = 5 # changing value of only local variable def mod2(): a = 15 # reading initial value of a # prints 10 read() # calling mod 1 function to modify value # modifies value of global a to 5 mod1() # reading modified value # prints 5 read() # calling mod 2 function to modify value # modifies value of local a to 15 doesn't effect global value mod2() # reading modified value # again prints 5 read() # demonstrating non local # inner loop changing the value of outer a # prints 10 print ('Value of a using nonlocal is : 'end='') def outer(): a = 5 def inner(): nonlocal a a = 10 inner() print (a) outer() # demonstrating without non local # inner loop not changing the value of outer a # prints 5 print ('Value of a without using nonlocal is : 'end='') def outer(): a = 5 def inner(): a = 10 inner() print (a) outer()
出力:
10 5 5 Value of a using nonlocal is : 10 Value of a without using nonlocal is : 5