Meritshot Tutorials
- Home
- »
- Break Statement in Python
Python Tutorial
-
Understanding Variables in PythonUnderstanding Variables in Python
-
Types Of Operators in PythonTypes Of Operators in Python
-
How to create string ?How to create string ?
-
Data Structure in PythonData Structure in Python
-
What is a Function in PythonWhat is a Function in Python
-
Parameters and Arguments in PythonParameters and Arguments in Python
-
What is a Lambda FunctionWhat is a Lambda Function
-
What is a Regular Expression?What is a Regular Expression?
-
Introduction to Loops in PythonIntroduction to Loops in Python
-
If-else Statements in PythonIf-else Statements in Python
-
Break Statement in PythonBreak Statement in Python
-
OOPS in PythonOOPS in Python
-
Space and Time in PythonSpace and Time in Python
-
Data Type in PythonData Type in Python
-
Understanding Variables in PythonUnderstanding Variables in Python
-
Types Of Operators in PythonTypes Of Operators in Python
-
How to create string ?How to create string ?
-
Data Structure in PythonData Structure in Python
-
What is a Function in PythonWhat is a Function in Python
-
Parameters and Arguments in PythonParameters and Arguments in Python
-
What is a Lambda FunctionWhat is a Lambda Function
-
What is a Regular Expression?What is a Regular Expression?
-
Introduction to Loops in PythonIntroduction to Loops in Python
-
If-else Statements in PythonIf-else Statements in Python
-
Break Statement in PythonBreak Statement in Python
-
OOPS in PythonOOPS in Python
-
Space and Time in PythonSpace and Time in Python
-
Data Type in PythonData Type in Python
Break Statement in Python
The break statement enables a program to skip over a part of the code. A break statement terminates the very loop it lies within.
for i in range(1,101,1):
print(i ,end=” “) if(i==50):
break else:
print(“Mississippi”)
print(“Thank you”)
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Mississippi
- Thank you
Continue Statement
The continue statement skips the rest of the loop statements and causes the next iteration to occur.
for i in [2,3,4,6,8,0]: if (i%2!=0):
continue print(i)
2
4
6
8
0
See 3 is skipped because 3 when divided by 2 is not equal to 0, condition is satisfied and the iteration is skipped
Map, Filter and Reduce
In Python, the map, filter, and reduce functions are built-in functions that allow you to apply a function to a sequence of elements and return a new sequence. These functions are known as higher-order functions, as they take other functions as arguments.
Map
The map function applies a function to each element in a sequence and returns a new sequence containing the transformed elements. The map function has the following syntax:
map(function, iterable)
The function argument is a function that is applied to each element in the iterable argument. The iterable argument can be a list, tuple, or any other iterable object.
Here is an example of how to use the map function:
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Double each number using the map function
doubled = map(lambda x: x * 2, numbers)
# Print the doubled numbers
print(list(doubled))
[2, 4, 6, 8, 10]
In the above example, the lambda function lambda x: x * 2 is used to double each element in the numbers list. The map function applies the lambda function to each element in the list and returns a new list containing the doubled numbers.
filter
The filter function filters a sequence of elements based on a given predicate (a function that returns a boolean value) and returns a new sequence containing only the elements that meet the predicate. The filter function has the following syntax:
filter(predicate, iterable)
The predicate argument is a function that returns a boolean value and is applied to each element in the iterable argument. The iterable argument can be a list, tuple, or any other iterable object.
Here is an example of how to use the filter function:
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Get only the even numbers using the filter function
evens = filter(lambda x: x % 2 == 0, numbers)
# Print the even numbers
print(list(evens))
In the above example, the lambda function lambda x: x % 2 == 0 is used to filter the numbers list and return only the even numbers. The filter function applies the lambda function to each element in the list and returns a new list containing only the even numbers.
Reduce
The reduce function is a higher-order function that applies a function to a sequence and returns a single value. It is a part of the functools module in Python and has the following syntax:
reduce(function, iterable)
The function argument is a function that takes in two arguments and returns a single value. The iterable argument is a sequence of elements, such as a list or tuple.
The reduce function applies the function to the first two elements in the iterable and then applies the function to the result and the next element, and so on. The reduce function returns the final result.
Here is an example of how to use the reduce function:
from functools import reduce
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Calculate the sum of the numbers using the reduce function
sum = reduce(lambda x, y: x + y, numbers)
# Print the sum
print(sum)
In the above example, the reduce function applies the lambda function lambda x, y: x + y to the elements in the numbers list. The lambda function adds the two arguments x and y and returns the result. The reduce function applies the lambda function to the first two elements in the list (1 and 2), then applies the function to the result
(3) and the next element (3), and so on. The final result is the sum of all the elements in the list, which is 15.
It is important to note that the reduce function requires the functools module to be imported in order to use it.