[Python] Notes on while statements (writing style and infinite loop)

[Python] Notes on while statements (writing style and infinite loop)

A memo on how to write a while statement in python.

Basic syntax

python


initial value
while condition:
processing
Step

--Unlike the for statement, the initial value is defined outside the while statement. --Add ":" after the while statement (not ";") --Indent (TAB key) in the while statement --No need for ";" after processing or step

▼ Repeat processing 5 times from initial value 5 to 1

python


i=5;
while i>0:
  print(i)
 i -= 1

//output
5
4
3
2
1

infinite loop

Forgot to define steps (numerical increase / decrease)

The conditional expression becomes true and the process is repeated.

python


i=5;
while i>0:
  print(i)

Define a step outside a while statement

Since the value does not change in while, it repeats forever.

python


i=5;
while i>0:
  print(i)
i-=1

Recommended Posts

[Python] Notes on while statements (writing style and infinite loop)
Notes on Python and dictionary types
Notes on reading and writing float32 TIFF images in python
[Python] Sum Σ (sigma) and infinite product Π (pie) were loop statements!
Notes on installing Python3 and using pip on Windows7
Notes on HDR and RAW image processing with Python
Notes on writing config files for Python Note: configparser
Python on Ruby and angry Ruby on Python
Notes on pyenv and Atom
[Python] Notes on data analysis
Notes on deploying pyenv with Homebrew and managing Python versions
Notes on installing Python on CentOS
Python 3.6 on Windows ... and to Xamarin.
Minimum grammar notes for writing Python
Notes on using MeCab from Python
Notes on using post-receive and post-merge
Integrate Modelica and Python on Windows
Notes on installing Python using PyEnv
Python CSV file reading and writing
Reading and writing NetCDF with Python
Notes on using rstrip with python.
Notes on accessing dashDB from python
Reading and writing CSV with Python
(Personal notes) Python metaclasses and metaprogramming
Easy Python to learn while writing
Reading and writing text in Python
Notes on setting pyenv and python environment using Homebrew on Mac OS Marvericks
Notes for using OpenCV on Windows10 Python 3.8.3.
Notes using cChardet and python3-chardet in Python 3.3.1.
Python and Bash on Cisco Catalyst IOS-XE
Notes on PyQ machine learning python grammar
difference between statements (statements) and expressions (expressions) in Python
Notes on nfc.ContactlessFrontend () for nfcpy in python
Python virtual environment and packages on Ubuntu
A memo with Python2.7 and Python3 on CentOS
Notes on doing Japanese OCR with Python
Reading and writing JSON files with Python
See file and folder information on python
Understand python lists, dictionaries, and so on.
Notes on using code formatter in Python
Until you publish a web service on GCP while studying JQuery and Python