Basic grammar of Python3 system (included notation)

Overview

You will study the basic grammar of Python 3 by referring to "Introduction to Python 3" by O'Reilly Japan. I hope it will be helpful for those who want to study Python in the same way.

Comprehension notation

Comprehension is a format that allows you to compactly create a Python data structure from one or more iterators.

List comprehension

The simplest form of list comprehension is as follows.

[ expression for item in iterable ]

Comparison with normal code

--Normal (not included) code

>>> str_list = []
>>> for i in range(1, 6):
...     str_list.append(str(i) * i)
...
>>> str_list
['1', '22', '333', '4444', '55555']

--Code in list comprehension

>>> [str(i) * i for i in range(1, 6)]
['1', '22', '333', '4444', '55555']

When adding a conditional expression

--Normal (not included) code

>>> num_list = []
>>> for i in range(1, 6):
...     if i % 2 == 1:
...         num_list.append(str(i) * i)
...
>>> num_list
['1', '333', '55555']

--Code in list comprehension

>>> [str(i) * i for i in range(1, 6) if i % 2 == 1]
['1', '333', '55555']

When nesting loops

--Normal (not included) code

>>> keys = range(0, 3)
>>> vals = range(0, 3)
>>> for key in keys:
...     for val in vals:
...         print(key, val)
...
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2

--Code in list comprehension

>>> keys = range(0, 3)
>>> vals = range(0, 3)
>>> tuple = [(key, val) for key in keys for val in vals]
>>> for key, val in tuple:
...     print(key, val)
...
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2

Dictionary comprehension

The simplest form of dictionary comprehension is as follows.

{ key_item : value_item for item in iterable }

Dictionary comprehension sample

>>> iter_obj = [(1, 'AA'), (2, 'BB'), (3, 'CC')]
>>> dict_obj = {key : val for key, val in iter_obj}
>>> dict_obj
{1: 'AA', 2: 'BB', 3: 'CC'}

Set comprehension

The simplest form of set comprehension is as follows.

{ item for item in iterable }

Set comprehension sample

>>> {10**i for i in range(1, 6)}
{100000, 100, 1000, 10, 10000}

Generator comprehension

The generator can only be run once.

Generator comprehension sample

>>> #What's between the parentheses is the generator comprehension, which returns the generator object.
>>> gene_obj = (num**2 for num in range(1, 6))
>>> num_list = list(gene_obj)
>>> num_list
[1, 4, 9, 16, 25]

>>> #When I use the same generator object again, nothing comes out
>>> num_list = list(gene_obj)
>>> num_list
[]

Recommended Posts

Basic grammar of Python3 system (included notation)
Basic grammar of Python3 system (dictionary)
Basic grammar of Python3 system (character string)
Python3 basic grammar
Basic grammar of Python3 system (how to use functions, closures, lambda functions)
Python basic grammar / algorithm
Python basic grammar (miscellaneous)
Python basic grammar note (4)
Basic knowledge of Python
Python basic grammar memo
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
Python basic grammar (miscellaneous) Memo (2)
Basic usage of Python f-string
I learned Python basic grammar
Python basic grammar (miscellaneous) Memo (4)
Python (Python 3.7.7) installation and basic grammar
I wrote the basic grammar of Python with Jupyter Lab
Java and Python basic grammar comparison
Included notation in Python function arguments
Basic study of OpenCV with Python
Python basic operation 1st: List comprehension notation
Memorandum of python beginners About inclusion notation
[Basic grammar] Differences between Ruby / Python / PHP
[Python] I personally summarized the basic grammar.
Python Basic Course (at the end of 15)
Memo of troubles about coexistence of Python 2/3 system
Status of each Python processing system in 2020
Comparing the basic grammar of Python and Go in an easy-to-understand manner
Introduction of Python
RF Python Basic_01
Basic operation list of Python3 list, tuple, dictionary, set
Python hand play (one line notation of if)
Basic Python writing
Basics of python ①
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
Copy of python
VBA user tried using Python / R: basic grammar
Python grammar notes
What you want to memorize with the basic "string manipulation" grammar of python
This is the only basic review of Python ~ 3 ~
RF Python Basic_02
Comparison of Python and Ruby (Environment / Grammar / Literal)
Basic story of inheritance in Python (for beginners)
Introduction of Python
[Introduction to Python] Basic usage of lambda expressions
Basic operation of Python Pandas Series and Dataframe (1)
Implementation example of simple LISP processing system (Python version)
Ruby expert learned the basic grammar of Go language
Summary of the basic flow of machine learning with Python
[For beginners] Learn basic Python grammar for free in 5 hours!
[Introduction to Python] Basic usage of the library matplotlib
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
[Go] Basic grammar ① Definition
[Python] Operation of enumerate
List of python modules
Python I'm also basic
Python Basic Course (7 Dictionary)
Basic sorting in Python
[Go] Basic grammar ② Statement