Basic Python 3 grammar (some Python iterations)

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.

Iterative processing

Let's compare the code that retrieves the values in the list by iterating.

Iterative processing written normally

>>> target = ['AAA', 'BBB', 'CCC']
>>> current = 0
>>> while current < len(target):
...     print(target[current])
...     current += 1
...
AAA
BBB
CCC

Iterative processing written like Python

>>> target = ['AAA', 'BBB', 'CCC']
>>> for out in target:
...     print(out)
...
AAA
BBB
CCC

The second code is better Python-like code. Since a list is a Python iterator (corresponding to an iterator) object, processing it with a for statement retrieves the elements of the list one by one.

for statement

Besides lists, there are Python iterator objects like strings, tuples, dictionaries, sets, and more.

String

>>> target = 'python'
>>> for out in target:
...     print(out)
...
p
y
t
h
o
n

dictionary

Key removal (1)

>>> num = {0:'zero', 1:'one', 2:'two'}
>>> for out in num:
...     print(out)
...
0
1
2

Key removal (2)

>>> num = {0:'zero', 1:'one', 2:'two'}
>>> for out in num.keys():
...     print(out)
...
0
1
2

Extracting the value

>>> num = {0:'zero', 1:'one', 2:'two'}
>>> for out in num.values():
...     print(out)
...
zero
one
two

Extraction in tuple format

>>> num = {0:'zero', 1:'one', 2:'two'}
>>> for out in num.items():
...     print(out)
...
(0, 'zero')
(1, 'one')
(2, 'two')

Extraction in tuple format (application)

When processing retrieval and assignment in one step

>>> num = {0:'zero', 1:'one', 2:'two'}
>>> for key, value in num.items():
...     print('key:', key, 'value:', value)
...
key: 0 value: zero
key: 1 value: one
key: 2 value: two

Iterative processing using zip ()

You can use zip () to iterate over multiple sequences in parallel.

>>> list1 = ['1', '2', '3']
>>> list2 = ['A', 'B', 'C']
>>> list3 = ['one', 'two', 'three']
>>> for out1, out2, out3 in zip(list1, list2, list3):
...     print('list1:', out1, 'list2', out2, 'list3', out3)
...
list1: 1 list2 A list3 one
list1: 2 list2 B list3 two
list1: 3 list2 C list3 three

Generate a list with the return value of zip ()

>>> list1 = ['1', '2', '3']
>>> list2 = ['A', 'B', 'C']
>>> list( zip(list1, list2) )
[('1', 'A'), ('2', 'B'), ('3', 'C')]

Generate a dictionary with the return value of zip ()

>>> list1 = ['1', '2', '3']
>>> list2 = ['A', 'B', 'C']
>>> dict( zip(list1, list2) )
{'1': 'A', '2': 'B', '3': 'C'}

Creating a numeric sequence with range ()

Operation image

>>> for x in range(0, 3):
...     print(x)
...
0
1
2

Creating a list of numeric sequences

>>> list( range(0, 3) )
[0, 1, 2]

Recommended Posts

Basic Python 3 grammar (some Python iterations)
Python3 basic grammar
Python basic grammar / algorithm
Python basic course (9 iterations)
Python basic grammar (miscellaneous)
Python basic grammar note (4)
Python basic grammar note (3)
Python basic grammar memo
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
Python basic grammar (miscellaneous) Memo (3)
Python basic grammar (miscellaneous) Memo (2)
Basic Python grammar for beginners
I learned Python basic grammar
Python basic grammar (miscellaneous) Memo (4)
Python (Python 3.7.7) installation and basic grammar
Java and Python basic grammar comparison
Basic grammar of Python3 system (dictionary)
RF Python Basic_01
python grammar check
Basic Python writing
[Basic grammar] Differences between Ruby / Python / PHP
Python grammar notes
[Python] I personally summarized the basic grammar.
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
RF Python Basic_02
Basic grammar of Python3 system (included notation)
VBA user tried using Python / R: basic grammar
[Go] Basic grammar ① Definition
Python I'm also basic
Python Basic Course (7 Dictionary)
Python basic course (2 Python installation)
Basic sorting in Python
[Go] Basic grammar ② Statement
Python ~ Grammar speed learning ~
[python] class basic methods
Python Basic Course (11 exceptions)
Python basic course (6 sets)
Python3 cheat sheet (basic)
Python Basic Course (Introduction)
Python basic memorandum part 2
python basic on windows ②
[Go] Basic grammar ③ Pointer
Python basic memo --Part 2
Python basic course (13 classes)
Basic Python command memo
Basic knowledge of Python
OpenCV basic code (python)
Python basic memo --Part 1
python memorandum super basic
Python basic course (8 branches)
Python basic if statement
Python Basic Course (3 Python Execution)
Python Basic --Pandas, Numpy-
[For beginners] Learn basic Python grammar for free in 5 hours!
Python application: Pandas Part 1: Basic
Refactoring Learned in Python (Basic)
Five languages basic grammar comparison (C #, Java, Python, Ruby, Kotlin)
Grammar features added from Python3.6
BASIC authentication with Python bottle