【Python入門 #7】関数 | 処理を使いまわそう
Summary
TLDRThe video script is a comprehensive tutorial on the concept and application of functions in Python programming. It begins with an introduction to functions as a fundamental and essential part of programming, emphasizing their necessity for writing efficient and practical code. The script uses a quiz format to engage viewers, asking them to calculate the BMI (Body Mass Index) of different individuals using their weight and height. The presenter shares personal anecdotes about his own weight fluctuations and the impact on his physical fitness. The core of the tutorial focuses on defining functions in Python, explaining how to name a function, pass arguments, and return a value. It demonstrates the use of functions with a step-by-step guide on calculating BMI, highlighting the benefits of using functions, such as code reusability and readability. The script also touches on the concept of local and global variables, advising the use of local variables for safety and global variables sparingly. The tutorial concludes with an exercise for viewers to define and use a function to calculate the area of rectangles with given dimensions, reinforcing the practical use of functions in Python.
Takeaways
- 📚 **Understanding Functions**: Functions in Python are fundamental for practical programming, allowing you to reuse code and avoid repetition.
- 🏋️ **Functions Simplified**: Despite initial perceptions, functions are not as difficult as they seem. They are essentially simple structures that perform a set of tasks.
- 🤔 **Quiz Interaction**: The video uses a quiz about calculating BMI to illustrate the necessity and practical use of functions, making the concept more relatable.
- 🍲 **Real-life Analogy**: The presenter uses the analogy of cooking to explain the concept of functions, likening them to a recipe that takes ingredients (arguments) and produces a dish (return value).
- 📊 **BMI Calculation Example**: The script provides a detailed example of calculating BMI using functions, demonstrating how to define, use, and call functions with arguments.
- 💡 **Function Definition**: Functions are defined using the `def` keyword, followed by the function name and arguments, with the body of the function indented afterward.
- 🔁 **Reusability**: One of the key advantages of functions is their reusability, allowing you to call the same function multiple times without rewriting the code.
- 🧩 **Local vs. Global Variables**: The script distinguishes between local variables, which are only accessible within the function, and global variables, which can be accessed from anywhere in the program.
- 🚧 **Best Practices**: It is recommended to use local variables as much as possible for safety and to minimize the risk of bugs, reserving global variables for when necessary.
- 📈 **Progression to Advanced Topics**: After covering the basics, the script moves on to more advanced topics, such as using functions to calculate the total price of groceries in a shopping cart scenario.
- 🔍 **Debugging and Maintenance**: Using local variables makes code easier to debug and maintain because their scope is limited, reducing the chances of unintended side effects.
Q & A
What is the main theme of the video script?
-The main theme of the video script is the introduction and explanation of functions in Python programming.
Why are functions considered essential in programming?
-Functions are essential in programming because they allow for the reuse of code, making it more efficient and easier to manage, especially in longer programs.
What is the BMI calculation based on?
-The BMI (Body Mass Index) calculation is based on a person's weight in kilograms divided by the square of their height in meters.
What is the normal range for BMI according to the script?
-According to the script, a BMI between 18.5 and 25 is considered to be within the normal range.
How does the speaker describe the structure of a function in Python?
-The speaker describes the structure of a function in Python as simple, where you define a function with a name, specify the parameters it accepts, write the processing code inside the function, and return a value using the 'return' statement.
What is the purpose of using a black box analogy for functions?
-The black box analogy is used to simplify the concept of functions, illustrating that you input values (arguments) into the function and it processes them to produce an output (return value), without needing to know the internal workings.
Why is it beneficial to use functions to perform repetitive tasks in code?
-Using functions to perform repetitive tasks is beneficial because it reduces the amount of code you need to write, minimizes the chance of errors, and makes the code more readable and maintainable.
What is the concept of local and global variables as mentioned in the script?
-Local variables are those that are defined within a function and can only be used inside that function. Global variables, on the other hand, can be accessed and modified from anywhere in the program.
Why should global variables be used with caution?
-Global variables should be used with caution because they can be accessed and modified from anywhere in the program, which can lead to unexpected behavior and make the code harder to debug.
How does defining functions help in making the code more readable?
-Defining functions with meaningful names helps in making the code more readable because it provides a clear indication of what the code block is intended to do, without having to read through the entire code.
What is the speaker's personal experience with weight and body mass index (BMI) mentioned in the script?
-The speaker shares their personal experience of having a weight that fluctuated between 54 to 56 kilograms, with a peak of 65 kilograms during their university days when they were part of the rowing club. They also discuss their strategy for losing weight, which involved running and reducing dinner portions.
How does the script demonstrate the practical use of functions with a quiz question?
-The script demonstrates the practical use of functions by creating a function to calculate the BMI for different individuals, thus avoiding the repetition of the same calculation code and making the script more efficient and easier to understand.
Outlines
😀 Introduction to Functions in Python
The first paragraph introduces the topic of functions in Python, emphasizing their importance in programming. It mentions that functions are essential for writing practical and efficient code. The speaker aims to clarify that functions are not as difficult as they might initially seem, and they are quite simple in structure. The paragraph ends with an invitation to the audience to learn about the necessity and application of functions through a quiz on calculating BMI (Body Mass Index).
🏃♂️ The Importance of Functions and BMI Quiz
The second paragraph discusses the importance of functions in reducing repetitive code and preventing errors like typing mistakes. It uses the context of a BMI quiz to illustrate how functions can streamline the process of calculating and understanding BMI for different individuals. The paragraph also touches upon the speaker's personal experience with body weight and muscle mass, highlighting the fluctuation in weight due to different life stages and activities.
📚 Defining and Using Functions in Python
The third paragraph delves into the process of defining and using functions in Python. It explains how functions are created using the 'def' keyword, followed by the function name and parameters. The paragraph outlines the structure of a function, including its body and the use of the 'return' statement to provide output. It also discusses local variables within functions and the concept of returning values from functions.
🛒 Understanding Local and Global Variables
The fourth paragraph explores the concept of local and global variables in the context of functions. It explains that local variables are only accessible within the function they are defined in, while global variables can be accessed from anywhere in the code. The paragraph highlights the potential pitfalls of using global variables, such as unintended side effects and increased complexity, and advises the use of local variables for safety and clarity.
🔢 Implementing a Function to Calculate BMI
The fifth paragraph provides a practical example of implementing a function to calculate BMI. It details the steps of defining the function, including naming it, declaring parameters (weight and height), and calculating the BMI using the formula weight(kg) / height(m)^2. The paragraph demonstrates how to use the function by passing arguments and receiving the BMI value as the return value.
📝 Defining a Dictionary and Outputting Information
The sixth paragraph shifts the focus to using dictionaries in Python. It instructs on defining a dictionary with favorite celebrities' names and ages, and then outputting this information. The paragraph explains the process of iterating over the dictionary items and accessing both the key (celebrity name) and value (age) to display them in a readable format.
🏡 Conclusion and Encouragement to Learn Functions
The seventh and final paragraph concludes the discussion by reiterating the importance of functions in Python. It encourages the audience to become familiar with functions as they are a fundamental aspect of programming. The speaker summarizes the key points discussed in the video and thanks the audience for watching.
Mindmap
Keywords
💡Function
💡Argument
💡Return Value
💡Local Variable
💡Global Variable
💡BMI (Body Mass Index)
💡Quiz
💡Indentation
💡Default Function
💡Dictionary
💡Loop
Highlights
The importance of functions in programming is emphasized as they are essential for writing efficient and practical code.
Functions are not as difficult as they may initially seem, and their structure is quite simple.
The concept of a function is introduced as a way to give a name to a set of operations, making the code more organized and reusable.
Parameters (arguments) are used in functions to perform operations on different inputs.
The process of defining a function in Python using the 'def' keyword and how to specify the function name and parameters.
Explanation of how to use indentation to define the scope of a function's code block.
The use of the 'return' statement to specify the output of a function.
An example of calculating BMI (Body Mass Index) using a function to demonstrate the practical application of functions.
The concept of local variables within a function and their scope, which is limited to the function in which they are declared.
Differentiation between local and global variables, with global variables being accessible from anywhere in the code.
The potential issues with using global variables, such as unintended side effects and difficulty in maintaining large codebases.
Advice on minimizing the use of global variables to improve code safety and reduce the likelihood of bugs.
Demonstration of how to define and use a function to calculate the total price of groceries based on quantity and price.
The use of a quiz format to engage the audience and reinforce the understanding of functions.
Explanation of how to use functions to avoid repetitive code and make the program more readable and maintainable.
The demonstration of defining a function to calculate the area of a rectangle given the length and width.
The transcript concludes with an encouragement to practice using functions to become more proficient in programming.
Transcripts
同キャラです今日は python 入門
の7回目ということで関数をテーマにやっ
ていきますもう関数はプログラミングす上
で絶対でできますねちょっとよね長い
プログラムを書こうとしたら関数使うこと
になりますしこの関数を使えないとまずね
実践的な高とか書いたりとかできないです
でそれくらい必須中の必須も関わらずなん
かね最初この関数で難しく感じ勝ちなん
ですよですが実際のところそんなに関数は
難しいもうじゃありません構造は音
シンプルなんです今日は関数は実は音
シンプルなんだよというところでスッキリ
とせ解説していきたいと思いますので是非
最後までご覧ください
で今回も早速クイズからみていこうと思い
ますクイズを通じることで関数が何で必要
なのかどんなときに使うんだというところ
を一緒に根っこを体感しながら学んでいき
ましょう
今回 bmi というものを求めるクイズ
になりますb mit のは身長と体重
から始まんどどれくらい太っていうか痩せ
ているかっていうところを見る者になり
ますで bmi の四季としましては体重
キログラム割る身長メートルの音に城信長
かける新調したものが bmi になり
ますこれが1810号から25っていう
数字なあるとまぁ正常の範囲内で18.5
を下回るとやせ形25
1あると肥満しているという形ねスマそう
いう指標になります
で今回の問題は a さん b さん c
さんの体重と身長から bmi を求めて
ねっていう問題になりますa さんが体重
70票の身長いって7m b さんが体重
90キロの身長一定8メートルで c さん
が体重50キロの身長1.55m とれ
ちなみに僕は体重が今55キロの身長がね
1.66メイト166センチですで僕
だいたい体重いつもは55キロ前後なん
ですけど54から56なぐらいな角
グネグネしてるんですけど一時期体重が
65キロであった時期はっ大学時代に
ボート部に所属していてぼーっとしてない
でスナップを撮っていってこと工具やつね
でそれめちゃくちゃ
筋肉が必要なんですよ浴室全然こう筋肉
つきにくい方であんま力強くないんです
けど
まあでもこう大学で部活やったら強く欄に
ある筋肉をつける必要はそんな体重が必要
だったことでめちゃくちゃ飯食ってあまり
太りにくいんですけど一生懸命太らし
ターズって戦車時代一番多かった時が65
キロまでいってまぁそういう人生で史上
一番多かったんですよです本当に本当にね
3色だ最終増えなかったんで誤植た別なん
ですよで5色のうちも例えばおやつの時間
によしあの流動方多いを立てる見た感じで
めっちゃ加工立てるの苦しいともながら
すげー頑張ってくって人らして
でそれで筋力をつけていったんですけど
その後ねボートその選手引いた引退した後
に引退した直後エコックすって言ってポー
トってこうなく操縦する人がいるんですよ
やっぱ操縦する人やってくれって言われて
ちょっと今その人が足りなくてみたいなで
この運転手のコックすって体重若い方が
いいんですよこれたが運転者実際黒わけ
じゃないんで今度はこっちらに体重カルプ
公が良くて痩せてくれって言われてそれで
僕減量して1ヶ月ちょいぐらいでため1回
いつもて一ヶ月くらいで体重またこの55
キロまで落としです10キロ痩せてソフト
家何したかっていうと毎日10キロ近くで
走ってで夜ご飯をちょっとしか食べないっ
てしたんですね夜ご飯をほんのちょっとに
しておいて毎日走るこれやるとね見る日で
痩せましたで毎日で夜寝る時とかねお腹
減ってんよごはんちょっとしか食べない
グーッと中じゃないんだからうお腹減っ
だーって思いながら寝ていくわけですねで
本当に猫お腹減るもんだから結構ご飯
早食いで噛むのは変えするちょっと少なめ
だったりするんですけど
それをねもうしっかりと年も夜ご飯と彼し
たセラた海た一串50回ブレ館で感度ね
買えば買うとその子がそのためのても
ちょっと空腹が紛れるんですよそれで幸福
二冠を紛らわして1か月で一見落としたん
ですけど
まあねその時ねすげー思ったのが
さすがに一日中切らしてご飯の音夜ご飯
カオスを食べる量を減らしたりしたら普通
体重落ちるんだかっていうのとついでに
一緒に筋肉を北上で全部落ちたんですよ
およよよみたいな今まで一生懸命抗菌通し
てきてからつけてきた筋肉はどこへ行った
俺は筋肉みたいなその時に学校運動の音
はかなさみたいな音すげー感じましたね
ああ
いままでやってきた筋トレは何だったん
だろうなあみたいなはいそれ僕の身長と
体重のお話でしたとりあえずもしこの動画
見てる方で痩せたいって思っている方が
いらっしゃったらきっと毎日10キロ走っ
て夜ご飯を食べる量もちょこっとにしたら
と痩せるはずだということをねお伝えして
おきます
まったくあのを進めない方ではないです
はいではね web フォーム admin
あのー
をを止めてみましょうでこれどういう風に
やるかというととりあえず何も考えずに
やってみます何も考えずにやるとこんな
感じになるんですよねこれ結局し体重はる
新町9-2条をしてあげたらいいんで
これねーさん b さん c さんそれだ
よこれで体重はる慎重に状という形は
プリッとしてあげたら全員ので bm 会
を止めますまあ簡単ですよでもこのやり方
には問題があるんですなったというとって
ですねこれ実際自分で書いたわかんです
けどまいっかい婚式角度ねめんどくさいん
ですよません同じことマイナーないって
パクッていうのはめんどくさいんで
しかもミスも起きうるわけなんですよね a
例えばここでね括弧を2乗してるんです
けど
*2つ会に異常なんですけどこれは音*
引っ込んしちゃったねかケニーとかでなっ
ちゃう可能性とかもあるわけですね
タイピングミスしちゃう可能性もあるわけ
ですよこの愚直にねまいたいすどかくって
いうないよねこういう問題を孕んでいるの
でじゃあこれどうしたらねもっとスマート
にかけるかっていうお話が今回ある関数な
んですね関数というのはまず何かと申し
ますと一連の処理に名前をつけてまとめた
ものになります高度でいろいろと音一連の
処理をやっていてその処理のねコードの
複数行のねまぁ署員に対して名前を付ける
んですねでそれパッケージング化したもの
それが関数になりますイメージでいくと
例えばあっドっていう感想を用意したとし
ますね at っていうのは足し算する
引数
橋山するっていうね関するございますで
関するイメージは引数を受け取ってて関数
がブラックボックスの箱になっていてで
このね箱の中に引数を入れると戻り値が出
てくるって言うのが関数のイメージです
引数っていうのは関数 new 私は+値
ですね例えば今回1と2という値をを関数
at に渡したとしましょうで関数 at
は足し算してくれますと渡されたパイを
足し算してくるんですね1と2を足し算し
た結果戻り値その後もこのカラスを通して
与えかまた帰ってくるんですねこの帰って
くる与えること戻り値と抱えるチって言う
んですけど
そのとさんが帰ってくるという形のものが
関数ですアッパでこの関数の中身は何やっ
てるか外側かわかんないんですけど
あっ対応渡したらそこへなんか処理小国
ぶらっとしてくれてさは結果の値金返って
くるっていうのが関数ですねこのブラック
ボックス箱でこのプラボックスの箱の中今
コードがね色々書いてあってそこに名前を
付けたというのが関数です結構音シンプル
ですこうやって話し関す引数を受け取って
なんか処理しっ訂正で値を返すことでその
箱が関数だよっていうことなんですね
でこの関数で使うとねどういう不幸と関数
ねじゃあ実際使ってみましょうとどういう
風になるかというとまず関数の定義します
関数を定義するときは手振っていうキー
ワードを使いますで風スペースだとに関数
につける名前をね
つけてあげます今回 at っていうのを
つけてみましょう足し算するとでその後
at 格好だ後に引数を宣言するんですね
引数っていうのは渡される私は関数に渡す
あたりですって今回は a と b と
いう2種類の大を受け取るということにし
ましょうその後でコロンをつけて
でこれでこの関数
ます ke になります関数定義したら
開業して in 弁当を下げて準備パイ側
ようにインデントを4行を下げるのがね
一般的でございます34秒でスペースねっ
半角のスペースが4つら猫下げてあげ
るって言うのあかーんタイソンの書き方に
なります
デビ猿と1個オール例えば a +ディ
ギター
ディザルトっていう形にしますこの中では
ね結局その関数の中身の処理っていうのを
変えていくんですねでえっと今回は
ちょっとアートで引数を足したいので
a + b をしておりますでその a +
b の結果降りずあるとっていう変数に
入れて
でその後ディタージつあるとってこの
リターンってするとこのリッターで指定し
た値が戻り値として返してくれます今回し
た a + dpu した結果をリターン
しておりますなんで猫私合わされた値が
支えのカバー戻り値として帰るというわけ
でございます今チャミリターンをすると
そこに関数の実行を終了になります例えば
くなってなんかで処理買い手としても関数
の処理としては終了という形になります
ってこれから人と1個で関数を定義が出来
ました引数を受け取って何か中で処理をし
てくださいボディターンで戻り値を返すと
いうものですね
でこれ定義できた次はそのカースを使って
いきましょう関数を呼び出すっていうふう
に置く言います関数の使い方呼び出し方は
アットっていう関数で帰って格好で来てっ
てあげます
と格好っていう風にするとね関数で格好
ってするとその関数を呼び出せます指野菜
に引数があり引数を渡してあげましょう
例えば1と2っていうふうに渡すと戻り
著者さんが出てきますし
パットで3と4というふうにしますと戻し
としては3+4なんで7が出てくるという
形ですこれが関数ですどうですやっぱ
そんなんで関数難しくないと思うんですよ
ね単純で付で定義してあげて引数をしてし
てあげます中でなんか処理を書いて最後
リターで戻り値を返すって呼び出す時は
歯数で格好っていう形で呼び出してあげ
ますで引数があるときはその引数を指定し
てあげれば ok ですというのが関数で
ございますはいでこの関数ってねなんか
嬉しいのかと申しますと
今回そのクイズで直面したもんだよね両方
とも解決してくれるわけなんですよまず
同じションっていうのを何回も彼から来て
するんですよ1回書けば良くなります
さっきはみるとたとえばこれ
あと1回やってますよ1+13+4という
2回やってるんですけどあくまでこの関数
の処理っていうのはここで一回かいたらお
しまいですそうニっでそれぞれ前に回
呼び出しているとつまり毎回は背負うよう
で書かなくていいんです1回かいたらそれ
を使いませんめちゃくちゃ便利でしょう
これが可能すごいいいところの一つ目で二
つ目は書類に名前が付くのでわかりやすく
なるんですよ何をしているかというのは
コレ at 1+2って書かれてたらあっ
たら1と2をね足し算してくれるんだなあ
って何となく想像つくじゃないですかそう
いうふうに名前の付いていることで処理が
何してるかがわかりやすくなるって言うの
が母数のいいところになります
で単数のにちょっと補足しておくんです
けれども
まず引数は省略可能です必要なかったら
書かなくても大丈夫ですもう格好っていう
ふうに帰って格好の中には何も書かなくて
引数は大丈夫です出戻りちまうなんか別値
を返す必要がない時とかは省略可能で
ございますって関西が見てきたように自分
でまずねテープでね定義できます
で多分それだけでなくて python 実
はデフォルトで関数を用意してくれてるん
ですねってたとえばさっき出てきたあの
プリントとかねプリントカットって言って
ですこれも実は関数なんでスプリントとか
これパイソンがデフォルトで用意してくれ
ている関数なんですよ別にパンプリントも
結局まぁ中身として python の中
で何か公園で婦みたいな感じ定義してあっ
てそれを僕達が使ってるって言うだけなん
ですねだから実は自分で定義したものも
帰っパイソンが用意し増えてこういった
プリプとかも結局は同じ関数同じものなん
ですよねって感想だか山自分で定義して
呼び出すこともできれば体操がねっ
レポートを用意しているものを使うでも
両方できますとラパンプリントと彼の正体
は実は単純こういうふうにテープでね定義
してくれてるそれを愛すを入ってくれて
いるとそれを単純に使ってるだけなんだよ
というお話です
はいん
ではねここでクイズの解答帯びていこうと
思いますって今回クイズ
今回それをね先ほど b mins bmm
求めるというところを
今回関数にしてみましたデベ f pmi
格好でウェイトとはいとと体重と身長家と
ありますよねビターンしますよ
リターンでウエイトあるはいとか駆駆**
のにいっぱいにという形やると bmi が
求められますこれで付でまず関数停止ます
bmi という関数の名前を付けます
引数に体重とチン超を受け取ります
で受け取ったら戻り値としてリターンで
戻り値を返してあげますウェイト体重はる
のハイトかける**法とまぁ身長かける
身長の2乗という形論で計算して八尾
リターンしますと戻り値として返します
これはには bmi の値なんでそれ値を
返すということをしているわけですね
ですると先ほどね a さん b さん c
さんの bmi というのペっぷり bmi
格好71点な bmi 九州1.8 bm
英語11.55みたいな形で書けばこれ
全部も止まります
でこうするとねどうですかねこの bmi
の計算の処理って
前回のね最初のこのクイズの
解答ですと全部ないかにこういう風に書い
ていたわけなんですけど計算式を
それねもうそんなことしなくてもこれ一回
にこの計算式書くだけで済んでいますよね
毎回かかなくてすむんようになったわけ
ですなのでそういったね毎回書くことに
よるタイピングミスとかもねまずこの時点
で防げていますと
で加えて意味もわかりやすいですよね
bmi カッコ70一定なっていうふうに
書いてあったら合っピットこれで bma
を出してくれるんだろうなぁなんか推測
つくじゃないですかさっきの
一番最初猫やとやっぱり bmi 求めて
るって言う問題でやってるねこれ何やって
もはカルチャーカルチャーけどぱっと見
どこで何やってるかよくわかるんですよね
計算してのわかるけどなんと計算してるん
だろうって感じですかコードメニューが
良くわかりにくいんですよでもこれ処理
いいね名前を付けることで関数としながら
つけて be me bmi
パティだなぁってわかるので行動にょ見
やすくなって分かりやすくなるというのが
で関数取ってもいいところですちなみに
関数は後年同じ処理をね何回も結構使い
ますと意欲回転もするんですけど別に処理
第1回だけの時でも高度に名前を付けてね
こう意味がわかりやすくなので別にそう
いう目的ね関数を使うこともよくあります
1回しかしない背負いの時はに関数使っ
ちゃダメとか思わずい
コードが読みやすくなるために関節感全然
ok だというのはねちょっと頭の片隅に
入れておいてください
ではペン続いてちょっと発展変動で見て
いこうと思います変数のでちょちコープっ
ていう話をしようと思いますこれらの関数
使う際にちょっと重要になってくる概念な
ので自的にこれを押さえておいてください
発展編です
で今回はスーパーので合計の金額をね
デイジーのでネジで合計金額を出すという
プログラムをね書いてみたいと思います
入ってもねスーパーと言ってもあの商品は
オニオンとキャロット玉ねぎと人参しじゃ
ないとしますねですねちょっとね次回を見
にくいかもしれないでちっちゃいかもしれ
ないんでちょっと読み上げちゃうんです
けども
あのまずオニオンプライスタマネギの値段
が30円30トゥでキャロットプライス=
50
ニンジンの値段は50円ベストいうふうに
宣言されています
その後で筆関数が定義されていますデフ
トータルプライスカッコオニオンカウント
キャロットカウントって関東閉じるん明日
でコロンとトータルプライスとことで合計
金額だしますよと
でを2匹数はオニオンカウントまあ
オニオンのタマネギのこすってキャロット
買うんぷニンジンの個数ですと様は
タマネギの個数と人参のコスを受け取って
それで合計金額をアスティプログラムです
ね
で
オニオントータルプライス=オニオン
カウントかけるオニオンプライスオニオン
トータルプライさんでオニオンの合計の
ネザーは鬼をカウントをにを残すかける
オニオンプライスポニー音のお値段ですか
って
キャロットトータル+=キャロット
カウントかけるキャロットプライスとまぁ
いいわニンジンの合計金額はニンジンの
個数をかける民事の1個あたりのお値段
ですとってディターンしますと戻り値を
返しますビターは鬼をトータルプライス+
キャロットトータルプライスです要は
玉ねぎの合計金額と認知の合計金額を足し
たものをトータルプライスとして戻り値と
して返してあげますよということをして
おります
拝殿その上で
こっからね今回ちょっとやりたかった本題
なんですけどもこれで
ちゃんいっぱい尾根呼び出したいときは
単純にプリッとトータルプライス格好で
例えば玉ねぎ2個で認知3個三鷹で兄さん
みたいな感じでね書いてあげたら普通に
この関数を呼び出すことができますっここ
でちょっと喰種が問題なんですけど
プリントを人トータルクライスでしたら
その結果はどうなのでしょうか
5人をトータルプライスっていうのはこの
関数の中で宣言されている変数ですこれを
これに関する外側ね関数のストカールこの
鬼をトータルプライスという読み出したら
どのようになるでしょうか
でこれねあーもう声変えちゃってるんです
けどこれ実際呼び出すとねーなーっていう
形で名前のエラーになります name
エラーネームオニオントータル price
is not ティファインドっていく
ところでオニオントータルプライスは定義
されてませんよーって言うエラーになり
ます
これはですねこの関数の中で宣言された
変数
マートの鬼をトータルパイスとかって鑑賞
の中で宣言されてますよこういう編集の
ことをローカル変数と言いますローカル
変数っていうのはこの関数の中でだけ
使える変数意味ですねでこの関数中江宣言
された変数はローカル変数でこれ関数の中
でしか使えないのでこの関数の外側で
呼び出すとエラーになっちゃうんですね
それは定義されてないよこれから外側から
そんなセンスは存在しないよと言って
エラーになっちゃうんですよ
通ってこのオニオンプライスみたいなて
やっぱり半数の外側でてしてるじゃない
ですかこれはグローバル変数と言います
このグローバル変数っていうのはどこから
でも呼び出して使えるよっていうのが
グローバル変数ですねこのグローバル変数
はどこかでも使えるんでこの関数の外で
例えばプリントオニオンプライスとしても
呼び出すことができますし
またこの関数の中でもいる出せますこの
観衆の中でもね実はオニオントータル+=
をにをカウント*オーディオプライスと
いう形でここで5人プライスを呼び出し
使っているんですねこのようにグローバル
変数というのは監修のストアへ定義して
そうするとどこからでも使うことができ
ますでここで注意なんですけども
高話は聞くとねあじゃあ変数は全部
グローバル変数として関数の外側で定義し
た方がいいのかなちょっと感じるかもしれ
ないですけど実はそれはねまったくそんな
ことないですむしろこのグローバルフィス
はなるべく使わないようにして本当に
スタート聞いての質感艶して極力ローカル
変数を使うみましょうというのが
プログラミングをプロが格上るコツになり
ますローカル変数は結局この関数の中でた
使えないので
そうすると何が良いかっていうと安全なん
ですよグローバル変数でどこからでも使え
てサイドカー度か修正とかできるんですよ
デマーコプレイな凶数だったらですね
グローバーであるドローカーだろうそんな
気にする必要はないんですけど
例えば入っファイアー通じですか6で
ブローバル変数だと他のファイルカーでも
この変数を呼び出せるんですよすると思わ
なところでこの変数使われているからこの
辺その中身を修正できなかったりとか思わ
れるファイルして思ってもいないような
ファイルでこのグローバル変数の値が勝手
に書き換えられているとでそれが故にと
中華グローバル変数値が強く変わっていて
それでおぼ暴くが生まれるということが
よく起こるんですよファイル数が多くなれ
ばなるほど作っているプログラムの規模が
大きくなるほどこのグローバル変数という
のはバクのもとになりますようなとこから
でも使えてどこかありも修正ができるが
ゆえに畑m 復帰の子ができるがゆえに
それがいいに終わるところで支えられた青
マートコーデ値が修正されてそれが爆の
温床になっちゃうというわけなんですよね
一方でローカル変数はこの関数の中でしか
使えないことがわかっているのでこの使わ
れている範囲が修正されてが入りこの関数
の中だけ見ればいいね oi のところで
そういう風に使いたいと修正された奴が
ないんですなので非常に安全ですとバグを
起こしにくいですっていうのがローカル
変数になりますなので基本的には変数使う
時はこのローカル変数というのをぜひ意識
してみてくださいエアー宇部店ローカル
変数を使うようにします関数ないとかで
変数宣言する用意しますでただ複数の関数
をまたいつか痛いとかそういう変数がある
ときだけそういう時だけしまーやむを得ず
ねグローバル変数は使う
いう事例していくとバッグの出にくい硬度
になるのでぜひここねっ押さえておいて
くださいはいりょう実際に今回関数で定義
して使ってみようと思いますでまぁ今回
クイズやったりとemi を実際実装して
みましょう
でまず関数を使うときはデフでね帝位を
宣言をしていきます関数使うよっていう
ことですねデフと書いてその後関数目書い
ていきます今解説 emi という感じで
はか何をしているかをはわかりやすい名前
をつけておきましょうで彼を知ってこの
とってこれは関数の定義になりますで引数
を受け取るときは引数の値を書いていき
ます
例えば体重を受け取るならが weight
とダブルいや1 eht かなティ f 8
とってついて慎重なんではいというか等で
受け取っておきましょうで開業してって
これで2000件はできたらで引数のとか
も設定
宣言できたで続いて処理を書いていきます
処理としては
bmi の計算をしていきますので計算し
た値をねディザールトっていう中に出て
でこれはヘッド bmi なんで bmi
は体重はる
ヘッド信長の8人以上ですという形でね
ってこういう風に処理を書きますで最後
それを戻り値としてリターンに値を返して
あげると ok ですよというのがね関数
でございましたでこれにこういうふうにね
1回これを挟んじゃってるんですけどこれ
ぐらいの処理だったらもうこうやってね
特に編アイデアさはず一発にリザルトって
いう風にしてもありターンしてねしまって
も ok ですってこれ面会たらこれでね
関数を定義できましたと関数で触れして
名前つけて引数を宣言して何か処理してで
最後で行ったんだ代を返してあげますと
いうものですねあとこれ使ってあげるだけ
す pmi とって例えば僕の場合ですと
a 8今は今は55キロとかで身長が
166cm で1.66という感じでね
呼び出してあげますあとこれプリントでね
こうやって出力してあげましょう
はい家ではやってみます
するとね
19.9っていうのがねあ僕のねあの
bmi ですとということがね分かります
ってこういうふうにねそして挙げると
例えばねこれこれもあとはもう呼び出す
だけでね書類に書かなくてもね毎回に
できるわけです例えば身長が同じ身長だと
して体重が80キロの人の bmi 求め
たかったらこの辺ふき直したけどねここは
4 dmi が出てきますし
関数でこういうふうに一度定義した後は
自由に使え回せるんでねとても便利です
しかも名前もねこれで名前もつくんでね何
やってるかもわかりやすかって非常に読み
やすくもなって一石二鳥というの関数で
ございますはいとではこれでねっパイアの
関数一通り見てきたので前回のクイズの
回答を見ていきます
前回のクイズこちらでした好きな芸能人の
一覧を辞書で定義し
で税の名前と年齢を出力するプログラムを
作成せよっていうところでしたねでは早速
これをやっていきましょうはいえーよね
やっていこうと思いますまずは好きな
芸能人
の一覧を寝込んで名前と年齢をセットでも
出してそれを辞書に定義してくださいって
いうことなのでそこをやっていきますね
ええ
芸能人は芸能人ってこえー声なんて言うん
でしょう
芸能人英語で何て言うか考えてちょっと
イケてなさすぎるんですけど1回芸能ジっ
ていうねこの辺でねあの変数面付けちゃい
ますね芸能人
えっと=で辞書はねこういうふうに波括弧
へ定義するんでしたと出えーっとそうだな
ま好きな芸能人なんでん例えばボケ西島
さん好きなんで西島秀俊さんと a 三島
市へと資産がご年齢は何歳かと申しますと
何歳なんだろうぞ
早いと西村さんは51歳ならねほんと
かっこいいね50一打のカッコよさ
51歳と
であとは例えばガッキーとかすごい好きな
んで
新垣結衣と
ペックかっきーの年齢は何歳かと申します
と
立っ
33歳7かはい楽器33みたいな感じで
書いてあげますとってこういうでねまず
辞書エセ定義が出来ました続いて来い定義
できたのでこれのねねん芸能人の名前と
年齢をね出力するところを書いていき
ましょう茶目にティッシュはこういう形で
a
key と value っていう形でね
書いていきましてそこの間ねこういうふう
に転んで繋げていくとそれを列挙していく
形ですねでそういうところへ名前かお名前
の人が年齢何歳みたいな感じよねペアを
こうやってモテるというわけですね便利
ですよねこういうふうに何かセットに保ち
たいときとかは
でこれをね一覧べ出力を出力していきたい
ので for ループにこいつ気を回して
いきます当院で今回は芸能人の.アイテム
ずっと今回この key と value
両方を出力したいのでドットアイテムずで
出力していこうと思います
で a 時計と vkey と value
とことで兄と v としますキーが系が
こっちですねでバリーがこっちですねに
アクセスしていきますでそれをね4ルート
ばした1行ずつアクセスしていきますっ
ええっとまぁあとは出力単純してあげれば
いいんでプリントしてあげましょう
でえっ今回 f まず二文字列使おうかな
ああああああえーっと名前が兄で
まぁちょっとスペース空けて次年齢を出し
てあげましょう v でまぁ男性みたいな
感じでやってみましょうでコアねこれやっ
ていますね吹いて来い実現やると眠いに
しまさんが51歳でガッキーが33歳
ギター形で強くされましたはいこれがね
できれば今回のクイズオッケーですはい
りょう続いて今回のクイズです今回こちら
のクイズを通じて関数の練習をしていき
ましょう
で今回は辺の長さが8 b センチメートル
a センチメートルと b センチ
メートルの長方形の面積を関数を定義して
出力してくださいというものですで2つ8
その長方形があります一つ目の情報系が
まあ3cm と4センチメートルって二つ
目の情報系が10cm とセンチメートル
フォア2つの長方形の面積というのを
関数を定義してそれを呼び出す形で求めて
ください出力してくださいというものです
ね実際に関数をね自分でね使って定義して
使ってみることで関数の使い方っていうの
がねよりわかってくると思うので是非
こちらやってみましょう
関数ねほんといれば重要年必須中の必須な
のでぜひここへ慣れてみてください最後
までご覧いただきどうもありがとうござい
ました
半数は実際で勉強していくと昨日も多くて
ややこしく感じがちなんですけど本当に
アジアのとこうっていうので今回行った
ところなんですよ関数っていうのはなんか
new力の値を受け取ってそれを日本
ヨット処理してその後踊りつした値を返し
てあげますこれ入力があってそれなんか
処理してブラックボックス箱があって処理
してそれを出力するとペソのトラップ
ボックスの箱に行こう名前を付けたいっ
添えたね関数だっていうそのイメージさえ
おさえていただければ
半数随分とすっきり理解して使いやすく
なると思うので是非このイメージを追って
みてください
Voir Plus de Vidéos Connexes
Functions in Python | Introduction | Python for beginners #lec56
Coding Exercise for Beginners in Python with solution | Exercise 18| Python for Beginners #lec57
Optoelectronic devices: Introduction
Functions in Karel - Python
Go (Golang) Tutorial #9 - Using Functions
Re 2. Problems on Recursion | Strivers A2Z DSA Course
5.0 / 5 (0 votes)