Python provides different control flow statements to control the flow of execution in a program:
if statement – Conditional execution of a block of code based on a certain condition.
for loop – Iteration over a sequence of elements, such as a list, tuple or string.
while loop – Continuously executing a block of code until a certain condition is met.
break statement – Terminates the loop prematurely .
continue statement – Skips the current iteration of the loop and continues with the next iteration.
Indentation in Python Programming
Indentation is used to define blocks of code, such as loops and conditional statements. The standard indentation is 4 spaces, although you can use tabs or any number of spaces as long as it’s consistent throughout the code.
For example:
if True:
print(“Hello World”)
In this example, the code inside the if statement is indented by 4 spaces, indicating that it is part of the block of code that will be executed if the condition is True.
Using incorrect indentation will cause a IndentationError and the code won’t run. It’s important to be consistent with indentation in your code to ensure it runs as expected.
if statement
The if statement in Python allows you to control the flow of execution based on a certain condition. If the condition is True, a block of code is executed. If the condition is False, the block of code is skipped.
Example:
In this example, the value of x is 5, which is greater than 0. The condition x > 0 is True, so the code inside the if statement is executed and the message x is positive is printed.
if-else statement
The if-else statement in Python allows you to control the flow of execution based on a certain condition. If the condition is True, one block of code is executed. If the
condition is False, a different block of code is executed.
Example:
In this example, the value of x is 5, which is greater than In this example, the value of x is 5, which is greater than 0. The condition x > 0 is True, so the code inside the if statement is executed and the message x is positive is printed. If the value of x was less than or equal to 0, the code inside the else block would have been executed and the message x is not positive would have been printed.
if-elif statement
The if-elif statement in Python allows you to control the flow of execution based on multiple conditions. The conditions are evaluated in order until a True condition is found, at which point the corresponding block of code is executed. If no conditions are True, the optional else block is executed.
Example:
In this example, the value of x is 5, which is greater than 0. The condition x > 0 is True, so the code inside the first if statement is executed and the message x is positive is printed. If the value of x was equal to 0, the code inside the elif block would have been executed and the message x is zero would have been printed. If the value of x was less than 0, the code inside the else block would have been executed and the message x is negative would have been printed.
while loop
The while loop in Python allows you to execute a block of code repeatedly as long as a condition is True. The condition is evaluated before each iteration of the loop. If the condition is False, the loop terminates and execution continues with the next statement after the loop.
Example:
In this example, the variable count is initialized to 0. The loop continues as long as count is less than 5. On each iteration of the loop, the value of count is printed and then incremented by 1. After 5 iterations, the value of count becomes 5 and the condition count < 5 is False, so the loop terminates.
for loop
The for loop in Python is used to iterate over a sequence (such as a list, tuple, dictionary, or string) and execute a block of code for each item in the sequence.
Example:
In this example, the for loop iterates over 10 times and prints the value from 0 to 9
range function
The range function in Python is used to generate a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number. The range function can be used in for loops to repeat the loop for a
specific number of times.
break statement
The break statement in Python is used to exit a loop early, before the loop condition is met. It terminates the current loop and control is transferred to the statement immediately after the loop.
Example:
In this example, the for loop iterates over the values in the range (0, 10). The loop terminates early, when the value of i is 5, due to the break statement. So, only values from 0 to 4 are printed.
continue statement
The continue statement in Python is used to skip the current iteration of a loop and move on to the next iteration. It
returns control to the beginning of the loop, skipping any remaining statements in the current iteration.
Example:
In this example, the for loop iterates over the values in the range (0, 10). When the value of i is 5, the continue statement is executed, skipping the rest of the code in the current iteration, and moving on to the next iteration. So, the value 5 is not printed.
exit function
The exit function in Python is part of the sys module and is used to exit from the Python interpreter by raising the SystemExit exception.
Example:
import sys
# exits from the Python interpreter
sys.exit()
Note: When the exit function is called, the program terminates immediately and any remaining unhandled statements are not executed.
To buy Python books Click the below link
Python All-in-One for Dummies
Python Books
Handbook on Python Programming