Introduction to Control Statements in Python
Control statements are responsible for managing the flow in which loops get executed. The order in which the specified set of statements need to be executed can be effectively governed using these control statements. the python programming language offers three major sets of statements to control the flow of the program; they are listed below
How do Control Statements work in Python?
Below are the Control Statements in Python:
1. For Loops
For passing through an ordered set of programming statements, the for loops are useful in python. The loop’s execution happens until the last necessary element is executed in the specified range or the mentioned condition is satisfied.
Code:
fruits = ["jackfruit", "mango", "banana"]
for x in "banana":
print(x)
Output:
2. While Loops
When the well-organized set of programming statements need to be executed until the condition is satisfied, then the while loops must be used. In this instance, when the condition is satisfied, the next statement call happens. as like for loop here to the body of the code is determined using the code segment’s indentation.
Code:
value = 1
while value < 6:
print(value)
value += 1
Output:
3. Break
Terminates the loop and passes the control to the statement after the loop. If a break statement is mentioned within a nested looping, then the control will be pulled out of the nesting’s most inner loop.
Syntax:
break
Code:
# Note : BeautifulSoup library needs to be installed for this webpage parsing
from bs4 import BeautifulSoup
import requests
total_links = 0
count_check = 10
Input_url = input("Enter the website from which urls need to be extracted: ")
response = requests.get("http://" + Input_url)
html_bytes = response.text
soup = BeautifulSoup(html_bytes)
for every_link in soup.find_all('a'):
if total_links < count_check:
total_links = total_links + 1
print(every_link.get('href'))
else:
total_links = 1
break
print("Condition check is broken ")
BeautifulSoup Library Installation: libraries can be installed in multiple techniques in python; we have used pip installer for getting them installed; the command used for installing beautiful soup using pip installer is as below,
pip install BeautifulSoup
Output:
Explanation:
This program is a web scraping program; it is used for scrapping the first 10 links from any given webpage. here the beautifulsoup library is used for attaining web scraping. The web scraping process involves scrapping each and every html tag’s from a web page and. In this program, once every tag is extracted, the tag is referenced to verify whether it is a reference link using the ‘a’ tag check. Once the first 10 tags are extracted, the looping process is skipped out using the break tag. The break happens after a counter reset, and yet the program break statement kicks the program flow out of the loop instance. Because of this, even though the loop reset happens, the program prints only the first ten url’s.
4. Continue
Skips the remaining sentences in the loop and checks the condition posted in the loop
Syntax:
Continue
Code:
# Note : BeautifulSoup library needs to be installed for this webpage parsing
from bs4 import BeautifulSoup
import requests
total_links = 0
count_check = 10
Input_url = input("Enter the website from which urls need to be extracted: ")
response = requests.get("http://" + Input_url)
html_bytes = response.text
soup = BeautifulSoup(html_bytes)
for every_link in soup.find_all('a'):
if total_links < count_check:
total_links = total_links + 1
print(every_link.get('href'))
else:
total_links = 1
continue
print("Condition check is continued")
BeautifulSoup Library Installation: libraries can be installed in multiple techniques in python; we have used pip installer for getting them installed; the command used for installing beautiful soup using pip installer is as below,
pip install BeautifulSoup
Output:
Explanation:
This is another web scraping program; it is used for scrapping all the links from any given webpage. Here the beautifulsoup library is used for attaining web scraping. The web scraping process involves scrapping each and every html tag’s from a web page and. In this program, once every tag is extracted, the tag is referenced to verify whether it is a reference link using the ‘a’ tag check. In comparison to the above program here, the loop break statement is replaced with a continue statement. So this makes the control to be switched to the loop check again and again. Since the loop reset followed with a loop check happens iteratively, all the urls on the page gets printed.
5. Pass
It just passes the execution when reaching a specific statement. The pass will be occurring a specific micro second and then starts executing the statement after the pass statement.
Syntax:
Pass
Code:
# Note : BeautifulSoup library needs to be installed for this webpage parsing
from bs4 import BeautifulSoup
import requests
total_links = 0
count_check = 10
Input_url = input("Enter the website from which urls need to be extracted: ")
response = requests.get("http://" + Input_url)
html_bytes = response.text
soup = BeautifulSoup(html_bytes)
for every_link in soup.find_all('a'):
if total_links < count_check:
total_links = total_links + 1
print(every_link.get('href'))
else:
total_links = 1
pass
print("Condition check is passed")
BeautifulSoup Library Installation: libraries can be installed in multiple techniques in python; we have used pip installer for getting them installed; the command used for installing beautiful soup using pip installer is as below,
pip install BeautifulSoup
Output:
Explanation:
This is another web scraping program; it is used for scrapping all the links from any given webpage. As like above programs here too beautifulsoup library is used for attaining web scraping. The web scraping process involves scrapping each and every html tag’s from a web page and. In this program, once every tag is extracted, the tag is referenced to verify whether it is a reference link using the ‘a’ tag check. In comparison to the above program here, the pass statement is placed. So it allows the control to be stopped for quite a few micro seconds, and then, unlike either breaking the loop or switching control to the condition check, it flows to the next statement in the program.
Conclusion
Every high-level programming language depends on the looping and condition statements for its optimised execution. The loop control statements are used for attaining better control over the loops. The python loop control statements offer a powerful capability for looped execution.
Recommended Articles
We hope that this EDUCBA information on “Control Statements in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.