Home >Backend Development >Python Tutorial >How to use while statement in Python [For Beginners]
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]
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=['audi','bmw','subaru',toyota] for car in cars: if car == 'audi': 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!