Control Statements in Python
One of the most fundamental constructs in computer programming is the control statement, which is often called an "if-else". These allow programmers to conditionally execute different blocks of code depending on some condition.
What is a control statement?
The term control statement can refer to any statement that has an if-else in it.
The "if" statement
The "if" statement in Python is used to check the value of a condition. If the condition is True, the code in the block of code to the left of the "if" statement will be executed. If the condition is False, the code in the block of code to the left of the "if" statement will not be executed.
The "if" statement is very easy to use, and is often the simplest way to check for a condition in Python.
Example:
if x < 5:
print("You are smaller than five.")
else:
print("You are not smaller than five.")
This example will print "You are smaller than five." if x is less than five, and "You are not smaller than five." if x is not less than five.
The "if" statement can be used in any context. This is the most common use of the "if" statement in Python.
The "else" statement
The "else" statement is used to check the value of a condition. If the condition is True, the code in the block of code to the right of the "else" statement will be executed. If the condition is False, the code in the block of code to the right of the "else" statement will not be executed.
Control Statements in Python |
Basic Control Statements
The control flow of a Python program is regulated by conditional statements, loops, and function calls.
Python has three types of control structures:
1.Sequential
Sequential statements are a set of statements that are executed in a sequence. The problem with sequential instructions is that if the logic in one of the lines is interrupted, the full execution of the source code is interrupted.
2.Selection/Decision control statements
In Python, select statements are also known as decision control statements or branch statements. The select statement enables a program to test multiple conditions and execute statements based on which condition is true. Some decision control statements are:
- Simple if
- if-else
- nested if
- if-elif-else
3.repetition statement
A repetition statement is used to repeat a group(block) of programming instructions.
In Python, we generally have two loops/repetitive statements:
- for loop
- while loop
Decision Control Statements :
Simple if :
# If the number is greater than zero, we print an message num = 6 if num > 0: print(num, "is a positive number.") num = -1 if num > 0: print(num, "is a positive number.") print("it is printed always.") output : 6 is a positive number it is printed always.
if-else :
# checks if the number is positive or negative num = 5 if num >= 0: print("Positive") else: print("Negative") output : Positive
if-elif-else :
If the condition for if is False, the condition of the following elif block is checked, and so on.
Example:
num=-2 if num > 0: print("Positive") elif num == 0: print("Zero") else: print("Negative") Output : Negative
Nested If else:
A if...elif...else statement can be included within another if...elif...else statement. In computer programming, this is known to as nesting.
num = float(input("Enter a number: ")) if num >= 0: if num == 0: print("Zero") else: print("Positive") else: print("Negative") output : Enter a number: 6 Positive
Repetition Statements:
for loop
In Python, the for loop iterates over a sequence (list, tuple, string) or other iterable objects. The process of iterating over a sequence is known as traversal.
Example:
# Program to print the 1 to 5 numbers using range for i in range(1,6): print(i) output : 1 2 3 4 5
while loop:
In Python, the while loop is used to iterate through a block of code as long as the test expression (condition) is true.
This loop is typically used when we don't know how many times to iterate ahead of time.
Example :
#print 1 to 5 using while loop i = 1 while(i<=5): print(i) i += 1 Output : 1 2 3 4 5
Conclusion:
Flow of control determines how a computer program will respond when given certain conditions and parameters. There are three basic types of control structures: Sequential, Selection and Repetition. To work with data, you need to understand variables and data types as well as instructions.
also read -Basic in python