【Python入門 #7】関数 | 処理を使いまわそう

だれでもエンジニア / 山浦清透
4 Apr 202231:13

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

00:00

😀 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).

05:01

🏃‍♂️ 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.

10:02

📚 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.

15:04

🛒 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.

20:06

🔢 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.

25:07

📝 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.

30:09

🏡 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

A function in programming is a block of organized, reusable code that is designed to perform a specific task. It allows for the creation of reusable code which can be called whenever a task needs to be performed. In the video, the concept of functions is central to the discussion, with the host explaining how to define, use, and the benefits of using functions in Python.

💡Argument

An argument in the context of programming is a value that is passed into a function. It is used to customize the behavior of the function for each call. The video script mentions arguments when explaining how to define a function that calculates BMI, where weight and height are the arguments.

💡Return Value

A return value is the result that a function outputs after its execution. It is the outcome of the function's processing and can be used by other parts of the program. The video emphasizes the importance of return values in functions, as seen when the host discusses the BMI calculation function that returns the calculated BMI value.

💡Local Variable

A local variable is a variable that is declared inside a function and is only accessible within that function. It is used to store data that is only relevant to the function's operation. The script explains local variables in the context of a function that calculates the total price of groceries, where the count of items is a local variable.

💡Global Variable

A global variable is a variable that is declared outside of all functions and is accessible from any place in the program. Unlike local variables, global variables maintain their value throughout the program's execution. The video script cautions against excessive use of global variables due to potential issues with code maintainability and bugs.

💡BMI (Body Mass Index)

BMI is a person's weight in kilograms divided by the square of height in meters. It is often used to classify underweight, overweight, or healthy weight categories. In the video, the host uses the concept of BMI to demonstrate how to create and use a function to calculate it based on a person's weight and height.

💡Quiz

The video script includes a quiz element where the host poses questions related to programming concepts. The quiz is used as an interactive way to engage the audience and test their understanding of the material. For instance, a quiz question about calculating BMI for different individuals is used to illustrate the use of functions.

💡Indentation

Indentation in programming, particularly in Python, is used to define blocks of code. It is a way to indicate the beginning and end of sections such as functions or loops. The video script mentions indentation when explaining the syntax of defining a function in Python.

💡Default Function

Default functions are pre-defined functions that come with the programming language's standard library. They can be used without the need to define them manually. The video mentions default functions like 'print', which is used to output data to the console, as an example of a function that is already available in Python.

💡Dictionary

A dictionary in Python is a collection of key-value pairs, where each key is unique. It is used to store data in an organized manner. The script discusses defining a dictionary to hold a list of favorite celebrities with their names as keys and ages as values, demonstrating how dictionaries can be used to manage related data.

💡Loop

A loop is a programming construct that allows for the repetition of a block of code as long as a certain condition is met. In the video, the host uses a loop to iterate over the items in a dictionary, which is a common use case for loops in processing collections of data.

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

play00:00

同キャラです今日は python 入門

play00:01

の7回目ということで関数をテーマにやっ

play00:05

ていきますもう関数はプログラミングす上

play00:09

で絶対でできますねちょっとよね長い

play00:12

プログラムを書こうとしたら関数使うこと

play00:14

になりますしこの関数を使えないとまずね

play00:18

実践的な高とか書いたりとかできないです

play00:22

でそれくらい必須中の必須も関わらずなん

play00:25

かね最初この関数で難しく感じ勝ちなん

play00:29

ですよですが実際のところそんなに関数は

play00:32

難しいもうじゃありません構造は音

play00:34

シンプルなんです今日は関数は実は音

play00:38

シンプルなんだよというところでスッキリ

play00:40

とせ解説していきたいと思いますので是非

play00:43

最後までご覧ください

play00:44

で今回も早速クイズからみていこうと思い

play00:47

ますクイズを通じることで関数が何で必要

play00:51

なのかどんなときに使うんだというところ

play00:53

を一緒に根っこを体感しながら学んでいき

play00:55

ましょう

play00:56

今回 bmi というものを求めるクイズ

play00:59

になりますb mit のは身長と体重

play01:02

から始まんどどれくらい太っていうか痩せ

play01:06

ているかっていうところを見る者になり

play01:09

ますで bmi の四季としましては体重

play01:12

キログラム割る身長メートルの音に城信長

play01:16

かける新調したものが bmi になり

play01:19

ますこれが1810号から25っていう

play01:23

数字なあるとまぁ正常の範囲内で18.5

play01:28

を下回るとやせ形25

play01:31

1あると肥満しているという形ねスマそう

play01:34

いう指標になります

play01:36

で今回の問題は a さん b さん c

play01:38

さんの体重と身長から bmi を求めて

play01:42

ねっていう問題になりますa さんが体重

play01:44

70票の身長いって7m b さんが体重

play01:48

90キロの身長一定8メートルで c さん

play01:52

が体重50キロの身長1.55m とれ

play01:56

ちなみに僕は体重が今55キロの身長がね

play02:01

1.66メイト166センチですで僕

play02:04

だいたい体重いつもは55キロ前後なん

play02:07

ですけど54から56なぐらいな角

play02:11

グネグネしてるんですけど一時期体重が

play02:14

65キロであった時期はっ大学時代に

play02:17

ボート部に所属していてぼーっとしてない

play02:20

でスナップを撮っていってこと工具やつね

play02:22

でそれめちゃくちゃ

play02:24

筋肉が必要なんですよ浴室全然こう筋肉

play02:27

つきにくい方であんま力強くないんです

play02:30

けど

play02:30

まあでもこう大学で部活やったら強く欄に

play02:33

ある筋肉をつける必要はそんな体重が必要

play02:36

だったことでめちゃくちゃ飯食ってあまり

play02:38

太りにくいんですけど一生懸命太らし

play02:40

ターズって戦車時代一番多かった時が65

play02:44

キロまでいってまぁそういう人生で史上

play02:47

一番多かったんですよです本当に本当にね

play02:49

3色だ最終増えなかったんで誤植た別なん

play02:52

ですよで5色のうちも例えばおやつの時間

play02:56

によしあの流動方多いを立てる見た感じで

play02:59

めっちゃ加工立てるの苦しいともながら

play03:02

すげー頑張ってくって人らして

play03:04

でそれで筋力をつけていったんですけど

play03:07

その後ねボートその選手引いた引退した後

play03:11

に引退した直後エコックすって言ってポー

play03:14

トってこうなく操縦する人がいるんですよ

play03:16

やっぱ操縦する人やってくれって言われて

play03:18

ちょっと今その人が足りなくてみたいなで

play03:21

この運転手のコックすって体重若い方が

play03:24

いいんですよこれたが運転者実際黒わけ

play03:27

じゃないんで今度はこっちらに体重カルプ

play03:29

公が良くて痩せてくれって言われてそれで

play03:32

僕減量して1ヶ月ちょいぐらいでため1回

play03:36

いつもて一ヶ月くらいで体重またこの55

play03:39

キロまで落としです10キロ痩せてソフト

play03:41

家何したかっていうと毎日10キロ近くで

play03:45

走ってで夜ご飯をちょっとしか食べないっ

play03:48

てしたんですね夜ご飯をほんのちょっとに

play03:50

しておいて毎日走るこれやるとね見る日で

play03:54

痩せましたで毎日で夜寝る時とかねお腹

play03:57

減ってんよごはんちょっとしか食べない

play03:59

グーッと中じゃないんだからうお腹減っ

play04:02

だーって思いながら寝ていくわけですねで

play04:04

本当に猫お腹減るもんだから結構ご飯

play04:07

早食いで噛むのは変えするちょっと少なめ

play04:10

だったりするんですけど

play04:11

それをねもうしっかりと年も夜ご飯と彼し

play04:15

たセラた海た一串50回ブレ館で感度ね

play04:18

買えば買うとその子がそのためのても

play04:21

ちょっと空腹が紛れるんですよそれで幸福

play04:24

二冠を紛らわして1か月で一見落としたん

play04:27

ですけど

play04:28

まあねその時ねすげー思ったのが

play04:30

さすがに一日中切らしてご飯の音夜ご飯

play04:33

カオスを食べる量を減らしたりしたら普通

play04:35

体重落ちるんだかっていうのとついでに

play04:38

一緒に筋肉を北上で全部落ちたんですよ

play04:42

およよよみたいな今まで一生懸命抗菌通し

play04:45

てきてからつけてきた筋肉はどこへ行った

play04:47

俺は筋肉みたいなその時に学校運動の音

play04:51

はかなさみたいな音すげー感じましたね

play04:54

ああ

play04:54

いままでやってきた筋トレは何だったん

play04:57

だろうなあみたいなはいそれ僕の身長と

play05:00

体重のお話でしたとりあえずもしこの動画

play05:03

見てる方で痩せたいって思っている方が

play05:04

いらっしゃったらきっと毎日10キロ走っ

play05:07

て夜ご飯を食べる量もちょこっとにしたら

play05:10

と痩せるはずだということをねお伝えして

play05:12

おきます

play05:13

まったくあのを進めない方ではないです

play05:16

はいではね web フォーム admin

play05:18

あのー

play05:19

をを止めてみましょうでこれどういう風に

play05:21

やるかというととりあえず何も考えずに

play05:23

やってみます何も考えずにやるとこんな

play05:27

感じになるんですよねこれ結局し体重はる

play05:30

新町9-2条をしてあげたらいいんで

play05:34

これねーさん b さん c さんそれだ

play05:36

よこれで体重はる慎重に状という形は

play05:40

プリッとしてあげたら全員ので bm 会

play05:42

を止めますまあ簡単ですよでもこのやり方

play05:46

には問題があるんですなったというとって

play05:50

ですねこれ実際自分で書いたわかんです

play05:52

けどまいっかい婚式角度ねめんどくさいん

play05:54

ですよません同じことマイナーないって

play05:57

パクッていうのはめんどくさいんで

play05:59

しかもミスも起きうるわけなんですよね a

play06:03

例えばここでね括弧を2乗してるんです

play06:05

けど

play06:06

*2つ会に異常なんですけどこれは音*

play06:09

引っ込んしちゃったねかケニーとかでなっ

play06:12

ちゃう可能性とかもあるわけですね

play06:13

タイピングミスしちゃう可能性もあるわけ

play06:15

ですよこの愚直にねまいたいすどかくって

play06:19

いうないよねこういう問題を孕んでいるの

play06:21

でじゃあこれどうしたらねもっとスマート

play06:24

にかけるかっていうお話が今回ある関数な

play06:28

んですね関数というのはまず何かと申し

play06:31

ますと一連の処理に名前をつけてまとめた

play06:36

ものになります高度でいろいろと音一連の

play06:39

処理をやっていてその処理のねコードの

play06:42

複数行のねまぁ署員に対して名前を付ける

play06:45

んですねでそれパッケージング化したもの

play06:48

それが関数になりますイメージでいくと

play06:51

例えばあっドっていう感想を用意したとし

play06:54

ますね at っていうのは足し算する

play06:57

引数

play06:58

橋山するっていうね関するございますで

play07:01

関するイメージは引数を受け取ってて関数

play07:05

がブラックボックスの箱になっていてで

play07:07

このね箱の中に引数を入れると戻り値が出

play07:11

てくるって言うのが関数のイメージです

play07:13

引数っていうのは関数 new 私は+値

play07:17

ですね例えば今回1と2という値をを関数

play07:21

at に渡したとしましょうで関数 at

play07:23

は足し算してくれますと渡されたパイを

play07:27

足し算してくるんですね1と2を足し算し

play07:29

た結果戻り値その後もこのカラスを通して

play07:32

与えかまた帰ってくるんですねこの帰って

play07:35

くる与えること戻り値と抱えるチって言う

play07:37

んですけど

play07:38

そのとさんが帰ってくるという形のものが

play07:41

関数ですアッパでこの関数の中身は何やっ

play07:45

てるか外側かわかんないんですけど

play07:48

あっ対応渡したらそこへなんか処理小国

play07:50

ぶらっとしてくれてさは結果の値金返って

play07:53

くるっていうのが関数ですねこのブラック

play07:55

ボックス箱でこのプラボックスの箱の中今

play07:58

コードがね色々書いてあってそこに名前を

play08:01

付けたというのが関数です結構音シンプル

play08:04

ですこうやって話し関す引数を受け取って

play08:08

なんか処理しっ訂正で値を返すことでその

play08:11

箱が関数だよっていうことなんですね

play08:14

でこの関数で使うとねどういう不幸と関数

play08:18

ねじゃあ実際使ってみましょうとどういう

play08:20

風になるかというとまず関数の定義します

play08:24

関数を定義するときは手振っていうキー

play08:27

ワードを使いますで風スペースだとに関数

play08:30

につける名前をね

play08:32

つけてあげます今回 at っていうのを

play08:34

つけてみましょう足し算するとでその後

play08:37

at 格好だ後に引数を宣言するんですね

play08:41

引数っていうのは渡される私は関数に渡す

play08:44

あたりですって今回は a と b と

play08:47

いう2種類の大を受け取るということにし

play08:50

ましょうその後でコロンをつけて

play08:53

でこれでこの関数

play08:55

ます ke になります関数定義したら

play08:59

開業して in 弁当を下げて準備パイ側

play09:02

ようにインデントを4行を下げるのがね

play09:04

一般的でございます34秒でスペースねっ

play09:08

半角のスペースが4つら猫下げてあげ

play09:11

るって言うのあかーんタイソンの書き方に

play09:14

なります

play09:15

デビ猿と1個オール例えば a +ディ

play09:17

ギター

play09:18

ディザルトっていう形にしますこの中では

play09:21

ね結局その関数の中身の処理っていうのを

play09:23

変えていくんですねでえっと今回は

play09:26

ちょっとアートで引数を足したいので

play09:29

a + b をしておりますでその a +

play09:32

b の結果降りずあるとっていう変数に

play09:34

入れて

play09:35

でその後ディタージつあるとってこの

play09:37

リターンってするとこのリッターで指定し

play09:39

た値が戻り値として返してくれます今回し

play09:42

た a + dpu した結果をリターン

play09:45

しておりますなんで猫私合わされた値が

play09:48

支えのカバー戻り値として帰るというわけ

play09:51

でございます今チャミリターンをすると

play09:53

そこに関数の実行を終了になります例えば

play09:56

くなってなんかで処理買い手としても関数

play09:59

の処理としては終了という形になります

play10:02

ってこれから人と1個で関数を定義が出来

play10:05

ました引数を受け取って何か中で処理をし

play10:09

てくださいボディターンで戻り値を返すと

play10:11

いうものですね

play10:13

でこれ定義できた次はそのカースを使って

play10:16

いきましょう関数を呼び出すっていうふう

play10:18

に置く言います関数の使い方呼び出し方は

play10:20

アットっていう関数で帰って格好で来てっ

play10:24

てあげます

play10:25

と格好っていう風にするとね関数で格好

play10:28

ってするとその関数を呼び出せます指野菜

play10:31

に引数があり引数を渡してあげましょう

play10:33

例えば1と2っていうふうに渡すと戻り

play10:37

著者さんが出てきますし

play10:39

パットで3と4というふうにしますと戻し

play10:42

としては3+4なんで7が出てくるという

play10:45

形ですこれが関数ですどうですやっぱ

play10:48

そんなんで関数難しくないと思うんですよ

play10:50

ね単純で付で定義してあげて引数をしてし

play10:54

てあげます中でなんか処理を書いて最後

play10:56

リターで戻り値を返すって呼び出す時は

play10:59

歯数で格好っていう形で呼び出してあげ

play11:02

ますで引数があるときはその引数を指定し

play11:05

てあげれば ok ですというのが関数で

play11:08

ございますはいでこの関数ってねなんか

play11:11

嬉しいのかと申しますと

play11:13

今回そのクイズで直面したもんだよね両方

play11:16

とも解決してくれるわけなんですよまず

play11:19

同じションっていうのを何回も彼から来て

play11:22

するんですよ1回書けば良くなります

play11:25

さっきはみるとたとえばこれ

play11:27

あと1回やってますよ1+13+4という

play11:30

2回やってるんですけどあくまでこの関数

play11:33

の処理っていうのはここで一回かいたらお

play11:35

しまいですそうニっでそれぞれ前に回

play11:39

呼び出しているとつまり毎回は背負うよう

play11:41

で書かなくていいんです1回かいたらそれ

play11:43

を使いませんめちゃくちゃ便利でしょう

play11:46

これが可能すごいいいところの一つ目で二

play11:49

つ目は書類に名前が付くのでわかりやすく

play11:53

なるんですよ何をしているかというのは

play11:55

コレ at 1+2って書かれてたらあっ

play11:58

たら1と2をね足し算してくれるんだなあ

play12:01

って何となく想像つくじゃないですかそう

play12:03

いうふうに名前の付いていることで処理が

play12:05

何してるかがわかりやすくなるって言うの

play12:07

が母数のいいところになります

play12:12

で単数のにちょっと補足しておくんです

play12:14

けれども

play12:15

まず引数は省略可能です必要なかったら

play12:18

書かなくても大丈夫ですもう格好っていう

play12:21

ふうに帰って格好の中には何も書かなくて

play12:23

引数は大丈夫です出戻りちまうなんか別値

play12:26

を返す必要がない時とかは省略可能で

play12:29

ございますって関西が見てきたように自分

play12:32

でまずねテープでね定義できます

play12:34

で多分それだけでなくて python 実

play12:37

はデフォルトで関数を用意してくれてるん

play12:39

ですねってたとえばさっき出てきたあの

play12:43

プリントとかねプリントカットって言って

play12:46

ですこれも実は関数なんでスプリントとか

play12:49

これパイソンがデフォルトで用意してくれ

play12:52

ている関数なんですよ別にパンプリントも

play12:55

結局まぁ中身として python の中

play12:58

で何か公園で婦みたいな感じ定義してあっ

play13:01

てそれを僕達が使ってるって言うだけなん

play13:04

ですねだから実は自分で定義したものも

play13:06

帰っパイソンが用意し増えてこういった

play13:08

プリプとかも結局は同じ関数同じものなん

play13:11

ですよねって感想だか山自分で定義して

play13:14

呼び出すこともできれば体操がねっ

play13:16

レポートを用意しているものを使うでも

play13:18

両方できますとラパンプリントと彼の正体

play13:20

は実は単純こういうふうにテープでね定義

play13:23

してくれてるそれを愛すを入ってくれて

play13:24

いるとそれを単純に使ってるだけなんだよ

play13:26

というお話です

play13:28

はいん

play13:29

ではねここでクイズの解答帯びていこうと

play13:33

思いますって今回クイズ

play13:36

今回それをね先ほど b mins bmm

play13:39

求めるというところを

play13:42

今回関数にしてみましたデベ f pmi

play13:45

格好でウェイトとはいとと体重と身長家と

play13:49

ありますよねビターンしますよ

play13:52

リターンでウエイトあるはいとか駆駆**

play13:57

のにいっぱいにという形やると bmi が

play14:00

求められますこれで付でまず関数停止ます

play14:04

bmi という関数の名前を付けます

play14:07

引数に体重とチン超を受け取ります

play14:11

で受け取ったら戻り値としてリターンで

play14:14

戻り値を返してあげますウェイト体重はる

play14:17

のハイトかける**法とまぁ身長かける

play14:23

身長の2乗という形論で計算して八尾

play14:27

リターンしますと戻り値として返します

play14:28

これはには bmi の値なんでそれ値を

play14:32

返すということをしているわけですね

play14:34

ですると先ほどね a さん b さん c

play14:37

さんの bmi というのペっぷり bmi

play14:40

格好71点な bmi 九州1.8 bm

play14:44

英語11.55みたいな形で書けばこれ

play14:48

全部も止まります

play14:50

でこうするとねどうですかねこの bmi

play14:52

の計算の処理って

play14:54

前回のね最初のこのクイズの

play14:58

解答ですと全部ないかにこういう風に書い

play15:01

ていたわけなんですけど計算式を

play15:03

それねもうそんなことしなくてもこれ一回

play15:07

にこの計算式書くだけで済んでいますよね

play15:10

毎回かかなくてすむんようになったわけ

play15:12

ですなのでそういったね毎回書くことに

play15:15

よるタイピングミスとかもねまずこの時点

play15:17

で防げていますと

play15:19

で加えて意味もわかりやすいですよね

play15:21

bmi カッコ70一定なっていうふうに

play15:24

書いてあったら合っピットこれで bma

play15:27

を出してくれるんだろうなぁなんか推測

play15:29

つくじゃないですかさっきの

play15:31

一番最初猫やとやっぱり bmi 求めて

play15:35

るって言う問題でやってるねこれ何やって

play15:37

もはカルチャーカルチャーけどぱっと見

play15:40

どこで何やってるかよくわかるんですよね

play15:42

計算してのわかるけどなんと計算してるん

play15:44

だろうって感じですかコードメニューが

play15:46

良くわかりにくいんですよでもこれ処理

play15:49

いいね名前を付けることで関数としながら

play15:52

つけて be me bmi

play15:55

パティだなぁってわかるので行動にょ見

play15:59

やすくなって分かりやすくなるというのが

play16:00

で関数取ってもいいところですちなみに

play16:03

関数は後年同じ処理をね何回も結構使い

play16:08

ますと意欲回転もするんですけど別に処理

play16:11

第1回だけの時でも高度に名前を付けてね

play16:14

こう意味がわかりやすくなので別にそう

play16:15

いう目的ね関数を使うこともよくあります

play16:19

1回しかしない背負いの時はに関数使っ

play16:21

ちゃダメとか思わずい

play16:22

コードが読みやすくなるために関節感全然

play16:25

ok だというのはねちょっと頭の片隅に

play16:27

入れておいてください

play16:29

ではペン続いてちょっと発展変動で見て

play16:32

いこうと思います変数のでちょちコープっ

play16:35

ていう話をしようと思いますこれらの関数

play16:38

使う際にちょっと重要になってくる概念な

play16:42

ので自的にこれを押さえておいてください

play16:44

発展編です

play16:45

で今回はスーパーので合計の金額をね

play16:51

デイジーのでネジで合計金額を出すという

play16:53

プログラムをね書いてみたいと思います

play16:56

入ってもねスーパーと言ってもあの商品は

play16:59

オニオンとキャロット玉ねぎと人参しじゃ

play17:02

ないとしますねですねちょっとね次回を見

play17:05

にくいかもしれないでちっちゃいかもしれ

play17:06

ないんでちょっと読み上げちゃうんです

play17:08

けども

play17:09

あのまずオニオンプライスタマネギの値段

play17:12

が30円30トゥでキャロットプライス=

play17:15

50

play17:16

ニンジンの値段は50円ベストいうふうに

play17:18

宣言されています

play17:20

その後で筆関数が定義されていますデフ

play17:23

トータルプライスカッコオニオンカウント

play17:26

キャロットカウントって関東閉じるん明日

play17:29

でコロンとトータルプライスとことで合計

play17:33

金額だしますよと

play17:34

でを2匹数はオニオンカウントまあ

play17:37

オニオンのタマネギのこすってキャロット

play17:40

買うんぷニンジンの個数ですと様は

play17:43

タマネギの個数と人参のコスを受け取って

play17:46

それで合計金額をアスティプログラムです

play17:49

play17:50

play17:51

オニオントータルプライス=オニオン

play17:53

カウントかけるオニオンプライスオニオン

play17:56

トータルプライさんでオニオンの合計の

play17:59

ネザーは鬼をカウントをにを残すかける

play18:03

オニオンプライスポニー音のお値段ですか

play18:05

って

play18:06

キャロットトータル+=キャロット

play18:08

カウントかけるキャロットプライスとまぁ

play18:10

いいわニンジンの合計金額はニンジンの

play18:13

個数をかける民事の1個あたりのお値段

play18:16

ですとってディターンしますと戻り値を

play18:19

返しますビターは鬼をトータルプライス+

play18:23

キャロットトータルプライスです要は

play18:25

玉ねぎの合計金額と認知の合計金額を足し

play18:29

たものをトータルプライスとして戻り値と

play18:33

して返してあげますよということをして

play18:34

おります

play18:36

拝殿その上で

play18:38

こっからね今回ちょっとやりたかった本題

play18:40

なんですけどもこれで

play18:43

ちゃんいっぱい尾根呼び出したいときは

play18:45

単純にプリッとトータルプライス格好で

play18:48

例えば玉ねぎ2個で認知3個三鷹で兄さん

play18:53

みたいな感じでね書いてあげたら普通に

play18:55

この関数を呼び出すことができますっここ

play18:58

でちょっと喰種が問題なんですけど

play19:01

プリントを人トータルクライスでしたら

play19:05

その結果はどうなのでしょうか

play19:07

5人をトータルプライスっていうのはこの

play19:09

関数の中で宣言されている変数ですこれを

play19:14

これに関する外側ね関数のストカールこの

play19:17

鬼をトータルプライスという読み出したら

play19:19

どのようになるでしょうか

play19:21

でこれねあーもう声変えちゃってるんです

play19:23

けどこれ実際呼び出すとねーなーっていう

play19:26

形で名前のエラーになります name

play19:29

エラーネームオニオントータル price

play19:32

is not ティファインドっていく

play19:33

ところでオニオントータルプライスは定義

play19:37

されてませんよーって言うエラーになり

play19:38

ます

play19:39

これはですねこの関数の中で宣言された

play19:44

変数

play19:45

マートの鬼をトータルパイスとかって鑑賞

play19:47

の中で宣言されてますよこういう編集の

play19:50

ことをローカル変数と言いますローカル

play19:53

変数っていうのはこの関数の中でだけ

play19:56

使える変数意味ですねでこの関数中江宣言

play20:00

された変数はローカル変数でこれ関数の中

play20:03

でしか使えないのでこの関数の外側で

play20:06

呼び出すとエラーになっちゃうんですね

play20:08

それは定義されてないよこれから外側から

play20:11

そんなセンスは存在しないよと言って

play20:13

エラーになっちゃうんですよ

play20:16

通ってこのオニオンプライスみたいなて

play20:19

やっぱり半数の外側でてしてるじゃない

play20:21

ですかこれはグローバル変数と言います

play20:24

このグローバル変数っていうのはどこから

play20:27

でも呼び出して使えるよっていうのが

play20:29

グローバル変数ですねこのグローバル変数

play20:32

はどこかでも使えるんでこの関数の外で

play20:35

例えばプリントオニオンプライスとしても

play20:38

呼び出すことができますし

play20:40

またこの関数の中でもいる出せますこの

play20:43

観衆の中でもね実はオニオントータル+=

play20:46

をにをカウント*オーディオプライスと

play20:48

いう形でここで5人プライスを呼び出し

play20:50

使っているんですねこのようにグローバル

play20:53

変数というのは監修のストアへ定義して

play20:55

そうするとどこからでも使うことができ

play20:58

ますでここで注意なんですけども

play21:02

高話は聞くとねあじゃあ変数は全部

play21:05

グローバル変数として関数の外側で定義し

play21:08

た方がいいのかなちょっと感じるかもしれ

play21:11

ないですけど実はそれはねまったくそんな

play21:13

ことないですむしろこのグローバルフィス

play21:15

はなるべく使わないようにして本当に

play21:18

スタート聞いての質感艶して極力ローカル

play21:21

変数を使うみましょうというのが

play21:23

プログラミングをプロが格上るコツになり

play21:26

ますローカル変数は結局この関数の中でた

play21:30

使えないので

play21:31

そうすると何が良いかっていうと安全なん

play21:34

ですよグローバル変数でどこからでも使え

play21:37

てサイドカー度か修正とかできるんですよ

play21:40

デマーコプレイな凶数だったらですね

play21:42

グローバーであるドローカーだろうそんな

play21:44

気にする必要はないんですけど

play21:45

例えば入っファイアー通じですか6で

play21:49

ブローバル変数だと他のファイルカーでも

play21:51

この変数を呼び出せるんですよすると思わ

play21:54

なところでこの変数使われているからこの

play21:57

辺その中身を修正できなかったりとか思わ

play22:00

れるファイルして思ってもいないような

play22:02

ファイルでこのグローバル変数の値が勝手

play22:07

に書き換えられているとでそれが故にと

play22:10

中華グローバル変数値が強く変わっていて

play22:13

それでおぼ暴くが生まれるということが

play22:16

よく起こるんですよファイル数が多くなれ

play22:19

ばなるほど作っているプログラムの規模が

play22:21

大きくなるほどこのグローバル変数という

play22:24

のはバクのもとになりますようなとこから

play22:27

でも使えてどこかありも修正ができるが

play22:29

ゆえに畑m 復帰の子ができるがゆえに

play22:32

それがいいに終わるところで支えられた青

play22:34

マートコーデ値が修正されてそれが爆の

play22:37

温床になっちゃうというわけなんですよね

play22:40

一方でローカル変数はこの関数の中でしか

play22:43

使えないことがわかっているのでこの使わ

play22:46

れている範囲が修正されてが入りこの関数

play22:48

の中だけ見ればいいね oi のところで

play22:50

そういう風に使いたいと修正された奴が

play22:53

ないんですなので非常に安全ですとバグを

play22:56

起こしにくいですっていうのがローカル

play22:58

変数になりますなので基本的には変数使う

play23:01

時はこのローカル変数というのをぜひ意識

play23:04

してみてくださいエアー宇部店ローカル

play23:06

変数を使うようにします関数ないとかで

play23:09

変数宣言する用意しますでただ複数の関数

play23:14

をまたいつか痛いとかそういう変数がある

play23:17

ときだけそういう時だけしまーやむを得ず

play23:19

ねグローバル変数は使う

play23:22

いう事例していくとバッグの出にくい硬度

play23:25

になるのでぜひここねっ押さえておいて

play23:27

くださいはいりょう実際に今回関数で定義

play23:30

して使ってみようと思いますでまぁ今回

play23:33

クイズやったりとemi を実際実装して

play23:36

みましょう

play23:37

でまず関数を使うときはデフでね帝位を

play23:40

宣言をしていきます関数使うよっていう

play23:42

ことですねデフと書いてその後関数目書い

play23:46

ていきます今解説 emi という感じで

play23:49

はか何をしているかをはわかりやすい名前

play23:52

をつけておきましょうで彼を知ってこの

play23:54

とってこれは関数の定義になりますで引数

play23:57

を受け取るときは引数の値を書いていき

play24:00

ます

play24:01

例えば体重を受け取るならが weight

play24:03

とダブルいや1 eht かなティ f 8

play24:07

とってついて慎重なんではいというか等で

play24:11

受け取っておきましょうで開業してって

play24:14

これで2000件はできたらで引数のとか

play24:16

も設定

play24:17

宣言できたで続いて処理を書いていきます

play24:21

処理としては

play24:23

bmi の計算をしていきますので計算し

play24:26

た値をねディザールトっていう中に出て

play24:29

でこれはヘッド bmi なんで bmi

play24:32

は体重はる

play24:34

ヘッド信長の8人以上ですという形でね

play24:39

ってこういう風に処理を書きますで最後

play24:42

それを戻り値としてリターンに値を返して

play24:45

あげると ok ですよというのがね関数

play24:48

でございましたでこれにこういうふうにね

play24:50

1回これを挟んじゃってるんですけどこれ

play24:52

ぐらいの処理だったらもうこうやってね

play24:55

特に編アイデアさはず一発にリザルトって

play24:59

いう風にしてもありターンしてねしまって

play25:01

も ok ですってこれ面会たらこれでね

play25:04

関数を定義できましたと関数で触れして

play25:07

名前つけて引数を宣言して何か処理してで

play25:10

最後で行ったんだ代を返してあげますと

play25:12

いうものですねあとこれ使ってあげるだけ

play25:15

す pmi とって例えば僕の場合ですと

play25:19

a 8今は今は55キロとかで身長が

play25:24

166cm で1.66という感じでね

play25:29

呼び出してあげますあとこれプリントでね

play25:31

こうやって出力してあげましょう

play25:34

はい家ではやってみます

play25:36

するとね

play25:37

19.9っていうのがねあ僕のねあの

play25:40

bmi ですとということがね分かります

play25:43

ってこういうふうにねそして挙げると

play25:45

例えばねこれこれもあとはもう呼び出す

play25:47

だけでね書類に書かなくてもね毎回に

play25:50

できるわけです例えば身長が同じ身長だと

play25:53

して体重が80キロの人の bmi 求め

play25:56

たかったらこの辺ふき直したけどねここは

play25:58

4 dmi が出てきますし

play26:00

関数でこういうふうに一度定義した後は

play26:02

自由に使え回せるんでねとても便利です

play26:05

しかも名前もねこれで名前もつくんでね何

play26:08

やってるかもわかりやすかって非常に読み

play26:11

やすくもなって一石二鳥というの関数で

play26:14

ございますはいとではこれでねっパイアの

play26:17

関数一通り見てきたので前回のクイズの

play26:20

回答を見ていきます

play26:22

前回のクイズこちらでした好きな芸能人の

play26:25

一覧を辞書で定義し

play26:28

で税の名前と年齢を出力するプログラムを

play26:32

作成せよっていうところでしたねでは早速

play26:36

これをやっていきましょうはいえーよね

play26:38

やっていこうと思いますまずは好きな

play26:41

芸能人

play26:43

の一覧を寝込んで名前と年齢をセットでも

play26:46

出してそれを辞書に定義してくださいって

play26:48

いうことなのでそこをやっていきますね

play26:50

ええ

play26:51

芸能人は芸能人ってこえー声なんて言うん

play26:54

でしょう

play26:55

芸能人英語で何て言うか考えてちょっと

play26:58

イケてなさすぎるんですけど1回芸能ジっ

play27:00

ていうねこの辺でねあの変数面付けちゃい

play27:03

ますね芸能人

play27:06

えっと=で辞書はねこういうふうに波括弧

play27:09

へ定義するんでしたと出えーっとそうだな

play27:13

ま好きな芸能人なんでん例えばボケ西島

play27:17

さん好きなんで西島秀俊さんと a 三島

play27:21

市へと資産がご年齢は何歳かと申しますと

play27:27

何歳なんだろうぞ

play27:29

早いと西村さんは51歳ならねほんと

play27:32

かっこいいね50一打のカッコよさ

play27:34

51歳と

play27:36

であとは例えばガッキーとかすごい好きな

play27:39

んで

play27:41

新垣結衣と

play27:43

ペックかっきーの年齢は何歳かと申します

play27:46

play27:48

立っ

play27:49

33歳7かはい楽器33みたいな感じで

play27:53

書いてあげますとってこういうでねまず

play27:56

辞書エセ定義が出来ました続いて来い定義

play27:59

できたのでこれのねねん芸能人の名前と

play28:03

年齢をね出力するところを書いていき

play28:05

ましょう茶目にティッシュはこういう形で

play28:07

a

play28:08

key と value っていう形でね

play28:11

書いていきましてそこの間ねこういうふう

play28:13

に転んで繋げていくとそれを列挙していく

play28:16

形ですねでそういうところへ名前かお名前

play28:19

の人が年齢何歳みたいな感じよねペアを

play28:22

こうやってモテるというわけですね便利

play28:25

ですよねこういうふうに何かセットに保ち

play28:27

たいときとかは

play28:28

でこれをね一覧べ出力を出力していきたい

play28:32

ので for ループにこいつ気を回して

play28:34

いきます当院で今回は芸能人の.アイテム

play28:39

ずっと今回この key と value

play28:41

両方を出力したいのでドットアイテムずで

play28:44

出力していこうと思います

play28:47

で a 時計と vkey と value

play28:49

とことで兄と v としますキーが系が

play28:52

こっちですねでバリーがこっちですねに

play28:55

アクセスしていきますでそれをね4ルート

play28:57

ばした1行ずつアクセスしていきますっ

play29:00

ええっとまぁあとは出力単純してあげれば

play29:03

いいんでプリントしてあげましょう

play29:05

でえっ今回 f まず二文字列使おうかな

play29:08

ああああああえーっと名前が兄で

play29:12

まぁちょっとスペース空けて次年齢を出し

play29:15

てあげましょう v でまぁ男性みたいな

play29:18

感じでやってみましょうでコアねこれやっ

play29:20

ていますね吹いて来い実現やると眠いに

play29:24

しまさんが51歳でガッキーが33歳

play29:29

ギター形で強くされましたはいこれがね

play29:31

できれば今回のクイズオッケーですはい

play29:34

りょう続いて今回のクイズです今回こちら

play29:37

のクイズを通じて関数の練習をしていき

play29:41

ましょう

play29:42

で今回は辺の長さが8 b センチメートル

play29:46

a センチメートルと b センチ

play29:47

メートルの長方形の面積を関数を定義して

play29:51

出力してくださいというものですで2つ8

play29:55

その長方形があります一つ目の情報系が

play29:59

まあ3cm と4センチメートルって二つ

play30:02

目の情報系が10cm とセンチメートル

play30:06

フォア2つの長方形の面積というのを

play30:09

関数を定義してそれを呼び出す形で求めて

play30:12

ください出力してくださいというものです

play30:14

ね実際に関数をね自分でね使って定義して

play30:18

使ってみることで関数の使い方っていうの

play30:21

がねよりわかってくると思うので是非

play30:24

こちらやってみましょう

play30:25

関数ねほんといれば重要年必須中の必須な

play30:28

のでぜひここへ慣れてみてください最後

play30:31

までご覧いただきどうもありがとうござい

play30:33

ました

play30:34

半数は実際で勉強していくと昨日も多くて

play30:38

ややこしく感じがちなんですけど本当に

play30:41

アジアのとこうっていうので今回行った

play30:43

ところなんですよ関数っていうのはなんか

play30:45

new力の値を受け取ってそれを日本

play30:48

ヨット処理してその後踊りつした値を返し

play30:51

てあげますこれ入力があってそれなんか

play30:54

処理してブラックボックス箱があって処理

play30:56

してそれを出力するとペソのトラップ

play30:58

ボックスの箱に行こう名前を付けたいっ

play31:01

添えたね関数だっていうそのイメージさえ

play31:03

おさえていただければ

play31:05

半数随分とすっきり理解して使いやすく

play31:09

なると思うので是非このイメージを追って

play31:11

みてください

Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
Python ProgrammingFunctionsVariablesQuizzesBmi CalculationLocal VariablesGlobal VariablesCode EfficiencyData StructuresReal-world CodingTechnical Tutorial
Besoin d'un résumé en anglais ?