The contents of the Python tutorial (Chapter 4) are itemized.

Previous article: The contents of the Python tutorial (Chapter 3) are itemized

Introduction

Python3 Engineer Certification Basic Exam As a countermeasure, this is a personal memo that summarizes the contents of the Python tutorial (book) in easy-to-memorize bullet points.

Reference material

Python Tutorial: https://docs.python.org/ja/3/tutorial/ Chapter 4: https://docs.python.org/ja/3/tutorial/controlflow.html Books: https://www.oreilly.co.jp/books/9784873117539/

"Chapter 4 Control Structure Tool"

--Python3 Engineer Certification Basic Exam Score ―― 9/40 questions (22.5%) ☆☆☆☆☆ (importance: large) --Theme --Conditional branch

4.1 if statement

-** if, elif, else ** can be used. --Indent the blocks in the condition. --if, elif is followed by a condition and a colon :. - if x < 0: - elif x == 2: --You can write multiple elifs. --The colon: follows the else. - else: --By the way, Python else is also used in loops such as for and while (described later). --There is no switch statement in Python.

(Reference) while statement

--In the tutorial, ["3.2 Programming, the first step"](https://qiita.com/Wakii/items/cc4158d602a7646758d9#32-%E3%83%97%E3%83%AD%E3%82%B0% E3% 83% A9% E3% 83% 9F% E3% 83% B3% E3% 82% B0% E3% 81% AF% E3% 81% 98% E3% 82% 81% E3% 81% AE% E4% The syntax described in B8% 80% E6% AD% A9). --Personally, it is easier to remember with for, so I will describe it here. -** while ** Conditional expression **: ** Can be written. --Repeat the execution as long as the conditional expression is true. --Indent the body of the loop. --It is also possible to nest while and put for inside while.

4.2 for statement

--Python for processes all items in ** sequences (lists and strings) ** in index order. -** for ** w ** in ** words **: ** can be written as. --Indent the body of the loop. --When modifying the item to be looped, it is recommended to loop the copy of the sequence. --Use ** slice notation (slicing) ** to copy the sequence. (-> Chapter 3) --It is also possible to nest for and put while inside the for.

4.3 range () function

-** range () ** function returns an arithmetic progression object. --Can be written as range (start, end, increment). - range(5) #[0, 1, 2, 3, 4] - range(1, 5) #[1, 2, 3, 4] - range(0, 10, 3) #[0, 3, 6, 9] --By combining for and range (), numerical iteration can be achieved. -* for idx in range (5): * Repeat the process while #idx is 0 to 4. --The object returned by range () is not a list, but ** iterable *. Structures and functions that target repeatable bodies, such as --for, are called ** iterators . - The list () function ** can be used to create a list from a repeatable body. - test = list (range (5)) * #test is a list of [0, 1, 2, 3, 4]. --list () is also an iterator.

4.4 break and continue statements, else clauses in loops

-** break ** breaks through the innermost loop (for or while) that surrounds it. -** continue ** skips the subsequent in-loop processing and starts the next iteration. --The loop's ** else: ** is executed if break is not called inside the loop. --For and while and else are described with the same indentation.

4.5 pass statement

-** pass statement ** does nothing. --Syntactically, a sentence is required, but if there is nothing to do (waiting for keyboard interrupt, etc.), write it.

4.6 Function definition

-** def ** Function name ** (** Formal argument list ): Can be written as . --Indent the body part of the function. --The first sentence of the function body is ** [docstring](https://qiita.com/Wakii/items/39fb6eee9250c4196a61#476-%E3%83%89%E3%82%AD%E3%83%A5 % E3% 83% A1% E3% 83% B3% E3% 83% 86% E3% 83% BC% E3% 82% B7% E3% 83% A7% E3% 83% B3% E6% 96% 87% E5 % AD% 97% E5% 88% 97-docstiring) ** can be written. --A reference to the object is passed as the argument of the function. --In other words, the changes made on the function side are reflected on the caller side. --A function call (execution) creates a new ** local symbol table ** for that function. - Symbol table ** is a directory in the namespace, and the symbol table for the function's local variables is the local symbol table. --When assigned in a function, the value is stored in the local symbol table. --The arguments (object references) passed to the function are also stored in the local symbol table. --When a function calls a function, another new local symbol table is created. --If you assign a function name to a variable, you can call the function with that variable name. --Functions, like variables, are added to the current symbol table when defined. --The interpreter recognizes it as a value of the type called a user-defined function. - return statement ** returns one value and returns from the function. --If no value is specified in return, ** None ** is returned. --Returns None even if the end of the function is reached without writing return. --None is usually ignored by the interpreter. --By the way, although it is not written in 4.6, the argument defined in the function declaration is called ** formal argument ** (parameter), and the argument passed in the function call is called ** actual argument ** (argument).

4.7 Further about functions

4.7.1 Default values for arguments

--You can set ** default value ** for function argument. --Arguments with default values can be omitted. --The default value is evaluated only once at the time and scope when the function is defined. --If you make variable objects (lists, dictionaries, class instances, etc.) default values, the default values may change each time the function is called. --As a countermeasure, there is a method of setting the default value of the argument to None and assigning the initialized object to the argument when None is passed in the function.

4.7.2 Keyword arguments

-** There are the following two types of keyword arguments **. --Arguments passed in the form of keyword = value at the time of call ... One keyword / value pair --Arguments received in dictionary format with ** to the left of the name ... Multiple keyword / value pairs --For keyword arguments, the following are called ** positional arguments **. --Arguments that pass only values ... one argument --Arguments received in repeatable form with * to the left of the name ... multiple arguments --If you pass an argument in the following way, an error will occur. --When you pass a keyword argument that is not written in the formal argument list --When passed as both a positional argument and a keyword argument for one argument --When the keyword argument is passed before the positional argument

4.7.3 List of optional arguments

--Tuple format arguments are a variable number of arguments. --Variadic arguments should be placed at the end of the formal argument list. --You can put a normal argument before it. --If you put a normal argument after it, it will be treated as a part of variadic argument. -In the case of * args format, all formal arguments after this are keyword arguments. -** Tuples ** are sequence data similar to lists, but they are immutable and often contain a mixture of different types of items. (-> [5.3 Tuples and Sequences](https://qiita.com/Wakii/items/34585aa75bf8c6a5b219#53-%E3%82%BF%E3%83%97%E3%83%AB%E3%81%A8 % E3% 82% B7% E3% 83% BC% E3% 82% B1% E3% 83% B3% E3% 82% B9))

4.7.4 Unpacking the argument list

--Retrieving list, tuple, and dictionary items and passing them to variables and arguments is called ** unpack **. --Lists and tuples are unpacked and become positional arguments by adding * to the left of the name. --Dictionaries are unpacked and become keyword arguments if you add ** to the left of the name. --For example, if you want to pass the list args = [0, 10, 3] to the range () function in the form * range (0, 10, 3) , you can write * range ( args) *.

4.7.5 lambda expression

-You can write a small anonymous function (lambda function) using the ** lambda keyword **. --Lambda functions can be used wherever a function object is needed. --As a syntactic limitation, you can only have a single expression. --Like a normal function definition, variables in the scope surrounding it can be used from the lambda function. --For example, if you define a lambda function in the function func (), you can refer to the local variable of func () in the lambda function.

4.7.6 Documentation string (docstiring)

--The first line should always be a short and concise summary of the object's purpose. --The first line begins with a capital letter and ends with a period. --When writing continuously, the second line should be blank and the summary and other descriptions should be separated. --The Python parser does not remove the indentation of multi-line string literals. --If you want to remove it, use the tool that processes the document.

4.7.7 Function annotation (function annotation)

-** Function annotation ** is the type metadata information. --Completely optional and used in user-defined functions. --Stored as a dictionary in the ** \ _ \ _ annotations__ attribute ** of the function. --The definition of the argument annotation is followed by a colon and an expression after the argument name. --The definition of the return value annotation is to put a literal "->" and an expression between the formal argument list of the def statement and the colon. --Example: * >>> def f (ham: str, eggs: str ='eggs')-> str: *

4.8 Intermission connection: Coding style

--There is ** PEP8 ** in the Python coding standard. Some excerpts below. ――Indentation should be 4 spaces. Don't use tabs as they can be confusing. --Wrap lines with 79 characters or less. --Use blank lines to separate functions, classes, and large blocks within functions. --If possible, make comment lines independent. -[docstring](https://qiita.com/Wakii/items/39fb6eee9250c4196a61#476-%E3%83%89%E3%82%AD%E3%83%A5%E3%83%A1%E3%83% B3% E3% 83% 86% E3% 83% BC% E3% 82% B7% E3% 83% A7% E3% 83% B3% E6% 96% 87% E5% AD% 97% E5% 88% 97- docstiring) is used. --Put a space around the operator or after the comma, but not just inside (). --Give consistent naming to classes and functions. --Class is CamelCase --Functions and methods are lower_case_with_underscores --Always use self as the first argument of the method. --The best encoding is UTF-8 (Python default) or ASCII. --Do not use non-ASCII characters for identifiers.

Next article: The contents of the Python tutorial (Chapter 5) are itemized

Recommended Posts

The contents of the Python tutorial (Chapter 4) are itemized.
The contents of the Python tutorial (Chapter 2) are itemized.
The contents of the Python tutorial (Chapter 8) are itemized.
The contents of the Python tutorial (Chapter 1) are itemized.
The contents of the Python tutorial (Chapter 10) are itemized.
The contents of the Python tutorial (Chapter 6) are itemized.
The contents of the Python tutorial (Chapter 3) are itemized.
Get the contents of git diff from python
the zen of Python
Not being aware of the contents of the data in python
Reproduce the execution example of Chapter 4 of Hajipata in Python
[Maya Python] Crush the contents of the script 2 ~ list Notes
Reproduce the execution example of Chapter 5 of Hajipata in Python
Towards the retirement of Python2
About the ease of Python
About the features of Python
Simulation of the contents of the wallet
The Power of Pandas: Python
About the matter that the contents of Python print are not visible in docker logs
[Maya Python] Crush the contents of the script 3 ~ List unknown Plugins
[Maya Python] Crush the contents of the script 1 ~ Camera Speed Editor
[Data science memorandum] Confirmation of the contents of DataFrame type [python]
A Python script that compares the contents of two directories
How to check if the contents of the dictionary are the same in Python by hash value
The story of Python and the story of NaN
Easy encryption of file contents (Python)
[Python] The stumbling block of import
[Python of Hikari-] Chapter 09-03 Class (inheritance)
First Python 3 ~ The beginning of repetition ~
[Python] [Table of Contents Links] Python Programming
Understand the contents of sklearn's pipeline
[Translation] scikit-learn 0.18 Tutorial Table of Contents
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
Change the Python version of Homebrew
See the contents of Kumantic Segumantion
Python Math Series ⓪ Table of Contents
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
Introductory table of contents for python3
About the basics list of Python basics
Learn the basics of Python ① Beginners
[python, ruby] fetch the contents of a web page with selenium-webdriver
Output the contents of ~ .xlsx in the folder to HTML with Python
[Python of Hikari-] Chapter 07-02 Exception handling (continuous execution of the program by exception handling)
Verification of the theory that "Python and Swift are quite similar"
Contents of reading VBA x Python fastest work technique Memo chapter1
[Python] A program that rotates the contents of the list to the left
I checked the contents of docker volume
Change the length of Python csv strings
Check the behavior of destructor in Python
[Python3] Understand the basics of Beautiful Soup
Pass the path of the imported python module
The story of making Python an exe
Learning notes from the beginning of Python 1
Check the existence of the file with python
About the virtual environment of python version 3.7
[Python] Understand the content of error messages
Python tutorial
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Python3] Rewrite the code object of the function