Home  >  Article  >  Backend Development  >  How to use while statement in Python [For Beginners]

How to use while statement in Python [For Beginners]

云罗郡主
云罗郡主Original
2019-01-24 16:59:424396browse

The while statement is a repetitive loop statement, so how to write it in Python, the following php Chinese website will lead you to learn how to use the while statement in Python. [Recommended reading: Python Video Tutorial]

How to use while statement in Python [For Beginners]

1: What is a while statement? How to use the while statement in Python

The while statement is used repeatedly for the same process for repeated processing. The advantage of using the while statement is that we do not have to write the same process over and over again, and it also reduces the number of lines of code. .

For example:

val = 1
while  val < 10:
  print(val)
  val = val + 1

In the first line, we assign the number 1 to the variable val, in the second line, we keep the variable VAL less than 10, and in the third line, val is equal to val 1. In Python, if it is a word, it uses a working variable to name a certain number of group names.

We can also indent this tab character in Python, which means it is a block of processing performed by a while statement. The tab character is usually set to two or four letters and can be accessed by pressing TAB Key entry, using indentation to indicate processing blocks is a Python-specific syntax.

2: Usage examples of if statements and break statements

cars=[&#39;audi&#39;,&#39;bmw&#39;,&#39;subaru&#39;,toyota]
for car in cars:
 if car == &#39;audi&#39;:
 print(car.upper)
 else:
 print(car.title)
#-->AUDI Bmw Subaru Toyota

The above is a complete introduction to how to use while statements in Python, if you want to know more about Python, please pay attention to the PHP Chinese website.


The above is the detailed content of How to use while statement in Python [For Beginners]. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more