Summary of various for statements in Python

■ Basic form

i = 0 Start n times

input.py


n = 5
for i in range(n):
    print(i)

output.py


0
1
2
3
4

From i = x to (y-1)

input.py


x = 1
y = 4 
for i in range(x,y):
    print(i)

output.py


1
2
3

■ Character string

Extract one character at a time

input.py


for char in 'string':
    print(char)

output.py


s
t
r
i
n
g

■ List type

Extract one element at a time

input.py


list = ['a','b','c']
for a in list:
    print(a)

output.py


a
b
c

■ Dictionary type

Extract one element at a time

input.py


dict = {'key1':'val1', 'key2':'val2'}
for k,v in dict.items():
    print(k,v)

output.py


key1 val1
key2 val2

Take out only the key

input.py


for key in dict:
    print(key)

output.py


key1
key2

Recommended Posts

Summary of various for statements in Python
Summary of various operations in Tensorflow
[For beginners] Summary of standard input in Python (with explanation)
Summary of built-in methods in Python list
Summary of useful techniques for Python Scrapy
A brief summary of Graphviz in python (explained only for mac)
Summary of Python arguments
Various processing of Python
Summary of how to use MNIST in Python
Summary of frequently used Python arrays (for myself)
Basic story of inheritance in Python (for beginners)
Summary of Excel operations using OpenPyXL in Python
Data analysis in Python Summary of sources to look at first for beginners
Summary of tools needed to analyze data in Python
Summary of python file operations
Summary of Python3 list operations
What's new in Python 3.10 (Summary)
Python netCDF4 read speed and nesting of for statements
Summary of python environment settings for myself [mac] [ubuntu]
Summary of tools for operating Windows GUI with Python
Summary of pre-processing practices for Python beginners (Pandas dataframe)
About various encodings of Python 3
Equivalence of objects in Python
Techniques for sorting in Python
Face detection summary in Python
Implementation of quicksort in Python
What's new in Python 3.9 (Summary)
Python3> slice copy / slice notation> used in for statements, etc.
About "for _ in range ():" in python
Summary of date processing in Python (datetime and dateutil)
Basic summary of data manipulation in Python Pandas-Second half: Data aggregation
Check the operation of Python for .NET in each environment
[Memo] The mystery of cumulative assignment statements in Python functions
Summary of stumbling blocks in Django for the first time
I measured various methods of interprocess communication in multiprocessing of python3
Useful tricks related to list and for statements in Python
Summary of Hash (Dictionary) operation support for Ruby and Python
Google search for the last line of the file in Python
Check for memory leaks in Python
Pixel manipulation of images in Python
Check for external commands in python
Division of timedelta in Python 2.7 series
A brief summary of Python collections
Handling of JSON files in Python
Implementation of life game in Python
Waveform display of audio in Python
[TouchDesigner] Tips for for statements using python
Law of large numbers in python
Implementation of original sorting in Python
Run unittests in Python (for beginners)
Reversible scrambling of integers in Python
Introductory table of contents for python3
Record of Python introduction for newcomers
[For competition professionals] Summary of doubling
Summary of Python indexes and slices
[OpenCV; Python] Summary of findcontours function
Python Summary
Wrap (part of) the AtCoder Library in Cython for use in Python
Python summary
Various ways to create an array of numbers from 1 to 10 in Python.
Conversion of string <-> date (date, datetime) in Python