[Python of Hikari-] Chapter 05-06 Control Syntax (Basics of Comprehension)

[Python] Chapter 05-06 Basics of comprehensions

First, take a look at the program below (no need to write a program)

L = []  #Create an empty list L
print(L)  #Output element of L

for i in range(10):
    L.append(i)  #Append element to list L with append method

print(L)


Execution result

[Execution result] </ font> [] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Briefly, an empty list ** L ** is created, and the elements of the empty list are output once with the ** print function **. (Of course it's empty) Then move into the for statement and use the ** append method ** to add ** L ** elements one by one. (The append method was explained earlier.)

Finally, the print function outputs the contents of the element.

However, you can also output this process in one line. There is a ** inclusion notation ** as a method, so I will explain it.

Creating a list by comprehension

There are various comprehension notations, but they are described using the following description method.

[Calculation result by variable for variable in for repeat target]

There is a reason why it is described as "calculation result by variable", but I will explain it later. First, let's actually describe the same processing content as the above program in the inclusion notation. This time, enter the following code from ** Python Console **.

>>> [i for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The list notation is enclosed in []. By writing like this, it is possible to create a list in one line without using the append method or variables.

I will explain the reason why it is called "calculation result by variable". For example, when printing values in 0.5 increments to the list, you cannot write ** range (0, 10, 0.5) **. (You cannot specify a decimal number for step)

Therefore, we use the comprehension notation. Actually, it is described as follows. Enter the following code from the ** Python Console **.

>>> [i * 0.5 for i in range(10)]
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]

The result of this comprehension can also be assigned to a variable.

>>> ls = [i * 0.5 for i in range(10)]
>>> ls
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]

Dictionary creation by inclusion notation

You can create dictionaries as well as lists. Enter the following code from the ** Python Console **.

>>> Countries = ['Japan', 'Canada', 'China', 'America']
>>> {n[0:2] : n for n in Countries}
{'Ja': 'Japan', 'Ca': 'Canada', 'Ch': 'China', 'Am': 'America'}

This time it's a dictionary, so use {} instead of []. The first two characters of each element are used as keys. It is output by combining it with the value.

Practice problem

We have prepared exercises. Please try to solve it. Please use ** Python Console **. [1] Create a list of the results of squares of integers from 1 to 9 using comprehension notation. The results are as follows. [1, 4, 9, 16, 25, 36, 49, 64, 81]

Finally

This time I touched on the inclusion notation, but have you noticed it? Actually, I'm only using ** Python Console ** this time. Not much in other languages yet. The comprehension can be entered as a command. You can also use simple notation, so please use it.

Return to [Table of Contents Link]

Recommended Posts

[Python of Hikari-] Chapter 05-06 Control Syntax (Basics of Comprehension)
[Python of Hikari-] Chapter 05-07 Control syntax (conditional branching of comprehension notation)
[Python of Hikari-] Chapter 05-08 Control syntax (while statement-another iterative syntax-)
[Python of Hikari-] Chapter 05-05 Control syntax (for statement-multiple loops-)
[Python of Hikari-] Chapter 05-10 Control syntax (interruption and continuation of iteration)
[Python of Hikari-] Chapter 05-04 Control syntax (for statement-use of range function-)
[Python] Chapter 05-02 Control Syntax (Combination of Conditions)
[Python of Hikari-] Chapter 05-03 Control syntax (for statement-extracting elements from list-)
[Python for Hikari] Chapter 09-01 Classes (Basics of Objects)
[Python of Hikari-] Chapter 05-09 Control syntax (use of for statement and while statement properly)
Basics of Python ①
Basics of python ①
[Python] Chapter 02-04 Basics of Python Program (About Comments)
[Python] Chapter 02-03 Basics of Python programs (input / output)
Basics of Python scraping basics
[Python of Hikari-] Chapter 08-04 Module (Installation of external library)
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
Python control syntax (memories)
# 4 [python] Basics of functions
[Python] Chapter 02-02 Basics of Python programs (Handling of character strings)
Basics of python: Output
[Python] Chapter 02-05 Basics of Python programs (string operations / methods)
[Python] Chapter 02-06 <Supplement> Basics of Python programs (handling of numerical values)
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
[Basics of Modern Mathematical Statistics with python] Chapter 1: Probability
[Python] Chapter 05-01 Control syntax (comparison operator and conditional branching)
python: Basics of using scikit-learn ①
Basics of Python × GIS (Part 1)
[Python of Hikari-] Chapter 08-03 Module (Import and use of standard library)
Python control syntax, functions (Python learning memo ②)
[ev3dev × Python] Control of multiple motors
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
Getting Started with Python Basics of Python
Review of the basics of Python (FizzBuzz)
Basics of Python x GIS (Part 2)
About the basics list of Python basics
Learn the basics of Python ① Beginners
[Python of Hikari-] Chapter 07-02 Exception handling (continuous execution of the program by exception handling)
[Basics of Modern Mathematical Statistics with python] Chapter 3: Typical Probability Distribution
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
Python basics ⑤
Python comprehension
Python basics
[Learning memo] Basics of class by python
[Python3] Understand the basics of Beautiful Soup
Python comprehension
Python basics ④
I didn't know the basics of Python
Python basics ③
Python basics
The basics of running NoxPlayer in Python
[Basics of python basics] Why do __name__ == "__main__"
Python learning memo for machine learning by Chainer Chapter 13 Basics of neural networks
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
[Introduction to Data Scientists] Basics of Python ♬