Table of Contents
This guide covers the loop technique in python with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
Python programming language provides 2 types of loops, for loops and while loops. Use these loops together with loop control statements likebreakAndcontinueYou can create different types of loops. We'll learn about these Python loops below.
It is possible to create infinite loops in Python using while. When thewhileLoop condition alwaysTrue, you get an infinite loop.
To get rid of an infinite loop in Python, use Ctrl + C.
Example of while using infinite loop:
The Python program below requires the user to enter a number and print 3 times the number of that number. The loop is infinite because there are no stopping conditions.
while True:num = int(input("Nh?p m?t s?: "))print("G?p ba c?a",num,"là",3 * num)
The result when running the above code is:
Nh?p m?t s?: 3G?p ba c?a 3 là 9Nh?p m?t s?: 5G?p ba c?a 5 là 15Nh?p m?t s?: 7G?p ba c?a 7 là 21Nh?p m?t s?: 9G?p ba c?a 9 là 27Nh?p m?t s?: 10G?p ba c?a 10 là 30Nh?p m?t s?: Traceback (most recent call last):
You just enter Enter will continue to enter the new number and the program will run until you press Ctrl + C.
This is a regular repeat, without abreakStatement. ThewhileLoop condition appears at the beginning and end of the loop when this condition isFalse.
Python loop diagram provided at the beginning:
Example of a loop with the condition at the beginning:
# Try different numbers by assigning numbers to n
n = 15
# Use the following command if you want users to enter numbers
#n = int (input ("Enter number n:"))
# Initializing and counting variables i
tong = 0
i = 1
while i <= n:
tong = tong + i
i = i + 1 # update count
# Code by TipsMake.com
# in total
print ("Sum of numbers from 1 to", n, "is", tong)
The above program repeats the numbers until the given numbern, sums those numbers and prints them to the screen. After running the program, we get the result:
T?ng các s? t? 1 ??n 15 là 120
This type of loop can be done using an infinite loop associated with thebreakStatement in the loop block.
Python loop diagram with conditions in between:
Example of a loop with the middle condition:
# Nh?n ??u vào t? ng??i dùng cho ??n khi h? nh?p m?t nguyên âm nguyenAm = "aeiouAEIOU" # vòng l?p vô h?n while True : m = input ( "Nh?p m?t nguyên âm: " ) # ?i?u ki?n ? gi?a kh?i l?nh if m in nguyenAm : break print ( "?ây không ph?i là nguyên âm. Hãy th? l?i!" ) # Code by TipsMake.com print ( "Chu?n r?i, c?m ?n b?n!" )
The above program requires the user to enter a vowel, accompanied by a test command to force the user to enter until correct. As long as the user data entered is not vowel until then the loop continues.
After running the program, we get the following results:
This type of loop ensures that the block is executed at least once. It can be done using an infinite loop along with thebreakCommand at the end. This is quite similar to thedo.whileLoop in C.
Python loop diagram provided at the end:
Example of Python loop with the end condition:
Below we will create a dice program, giving random results to users using awhileLoop. The loop will continue until the user chooses to stop.
# Tung xúc x?c cho ??n khi ng??i dùng ch?n thoátimport random while True : input ( "Nh?n Enter ?? tung xúc x?c" ) # nh?n s? m?t xúc x?c b?t k? t? 1 ??n 6 num = random . randint ( 1 , 6 ) print ( "B?n tung ???c m?t" , num ) option = input ( "B?n có mu?n tung l?i không?(y/n) " ) # ?i?u ki?n if option == 'n' : break
When running the program, we get the following result:
Nh?n Enter ?? tung xúc x?cB?n tung ???c m?t 1B?n có mu?n tung l?i không?(y/n) yNh?n Enter ?? tung xúc x?cB?n tung ???c m?t 4B?n có mu?n tung l?i không?(y/n) n>>>
So you already know the basic iteration techniques in Python, don't forget to do Python exercises to practice your repetition techniques.
In the next lesson, you will learn about Python functions, syntax and how they work. Do not miss it!
Next lesson: Python functions
Previous post: Pass command in python
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.