Previous article: The contents of the Python tutorial (Chapter 3) are itemized
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.
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/
--Python3 Engineer Certification Basic Exam Score ―― 9/40 questions (22.5%) ☆☆☆☆☆ (importance: large) --Theme --Conditional branch
-** 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.
--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.
--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.
-** 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.
-** 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.
-** pass statement ** does nothing. --Syntactically, a sentence is required, but if there is nothing to do (waiting for keyboard interrupt, etc.), write it.
-** 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).
--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.
-** 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
--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))
--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) *.
-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.
--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.
-** 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: *
--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