Loop control statements

Loop control statements change execution from its normal sequence. Following are the loop control statements provided by Python:

  • Break: Break statement in Python is used to bring the control out of the loop when some external condition is triggered.
  • Continue: Continue statement is opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.
  • Pass: Pass statement is used to write empty loops. Pass is also used for empty control statement, function and classes.
Python
# Python program to demonstrate
# break, continue and pass 
   
s = 'w3wiki'
 
for letter in s: 
    if letter == 'e' or letter == 's': 
        break
    print(letter, end = " ")
print()
 
for letter in s: 
    if letter == 'e' or letter == 's': 
        continue
    print(letter, end = " ")
print()    
 
for letter in s: 
    if letter == 'e' or letter == 's': 
        pass
    print(letter, end = " ")

Output:

g 
g k f o r g k 
g e e k s f o r g e e k s 

Note: For more information, refer  break, continue and pass in Python.

How to Learn Python from Scratch in 2024

Python is a general-purpose high-level programming language and is widely used among the developers’ community. Python was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code.

If you are new to programming and want to learn Python Programming from Scratch, then this complete 2024 guide is here to help you. Whether you are an experienced programmer or new to the programming world, this guide will help you with the knowledge and resources needed to get started with Python Language.

Similar Reads

Key features of Python

Python has many reasons for being popular and in demand. A few of the reasons are mentioned below....

Getting started with Python Programming –

Python is a lot easier to code and learn. Python programs can be written on any plain text editor like notepad, notepad++, or anything of that sort. One can also use an online IDE for writing Python codes or can even install one on their system to make it more feasible to write these codes because IDEs provide a lot of features like intuitive code editor, debugger, compiler, etc.To begin with, writing Python Codes and performing various intriguing and useful operations, one must have Python installed on their System. This can be done by following the step by step instructions provided below:...

Fundamentals of Python

Python Indentation...

Basics of Input/Output

Taking input from user –...

Data Types

Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes....

Decision Making

Decision Making in programming is similar to decision making in real life. A programming language uses control statements to control the flow of execution of the program based on certain conditions. These are used to cause the flow of execution to advance and branch based on changes to the state of a program....

Control flow (Loops)

Loops in programming come into use when we need to repeatedly execute a block of statements. For example: Suppose we want to print “Hello World” 10 times. This can be done with the help of loops. The loops in Python are:...

Loop control statements

Loop control statements change execution from its normal sequence. Following are the loop control statements provided by Python:...

Functions

Functions are generally the block of codes or statements in a program that gives the user the ability to reuse the same code which ultimately saves the excessive use of memory, acts as a time saver and more importantly, provides better readability of the code. So basically, a function is a collection of statements that perform some specific task and return the result to the caller. A function can also perform some specific task without returning anything. In Python, def keyword is used to create functions....

Object Oriented Programming

Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function....

Inheritance

Inheritance is the ability of any class to extract and use features of other classes. It is the process by which new classes called the derived classes are created from existing classes called Base classes....

File Handling

File handling is the ability of Python to handle files i.e. to read and write files along with many other file handling options. Python treats files differently as text or binary and this is important. Each line of code includes a sequence of characters and they form a text file. Each line of a file is terminated with a special character, called the EOL or End of Line characters like comma {, } or newline character....

Modules and Packages

Modules...

Modules

A module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, which can be considered as a module named GFG which can be imported with the help of import statement....

Packages

Packages are a way of structuring many packages and modules which helps in a well-organized hierarchy of data set, making the directories and modules easy to access....

Regular expressions(RegEx)

Python RegEx is a powerful text matching tool that uses a pre-defined pattern to match the text. It can identify the presence or absence of text by comparing it to a specific pattern, and it can also divide a pattern into one or more sub-patterns. Below is the list of metacharacters:...

Exception handling

Like other languages, Python also provides the runtime errors via exception handling method with the help of try-except....