Control statements Loop statements in python are used to control the order of execution of the program based on the value and logic
Contents
Control Statements
Python provides three selection control statements that execute code based on the condition that evaluates to either true or false
The if statement performs an action if a condition is true.
The if-else statement performs an action if a condition is true or performs a different action if the condition is false.
if-elif-else statement perform one or many different actions depending on the truth or falsity of server condition
Please read the previous topic for more understanding about Control Statements
Python String
Python String Formatting
Boolean in Python
Python Operators
Please look into the below example for more understanding of the conditional statement
# if statement
x = 23
y = 17
if x > y:
print("x is greater than y")
# if-else statement
if y > x:
print("y is greater than x")
else:
print("x is greater than y")
# if-elif-else statement
if y > x:
print("x is less than y")
elif y > x:
print("y is equal to x")
else:
print("x is greater than y")
Output-
x is greater than y x is greater than y x is greater than y
Note:-
If the value is zero or none or false in value of if condition then it will consider as false.
Ternary Operators
If you have only one statement to execute, one for if, and one for else, you can put it all on the same line.
a = 10
b = 20
print(1) if a > b else print(-1) # 1 if a > b else -1
b = 8
print(1) if a > b else print(-1) if a < b else print(0)
Output-
-1 1
Pass Statement
if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.
x = 23
y = 17
if x > y:
pass
Loop Statements / Iteration Statement
Python provides two loop statements. Control Statements in Python
For Loop Statement
The for statement repeats an action or group of actions for every item in a sequence of items. A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the a keyword in other programming languages and works more like an iterator method as found in other object-orientated programming languages.
# Loop statement over a String
s = "Hello"
for i in s :
print(i)
# Loop statement over a List
list = ["Hello", "World"]
for i in list:
print(i)
Output-
H e l l o Hello World
While Loop Statement
The while statement repeats an action or group of actions as long as a condition remains true. The while loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when the condition becomes false, the line is executed immediately after the loop in the program. While loop comes under the category of indefinite iteration. Indefinite iteration means that the number of times the loop is to be executed is not explicitly specified in advance.
i = 1
while i <= 5:
print(i)
i += 1
Output –
1 2 3 4 5
While loop with else
As discussed above, the while loop executes the block until a condition is satisfied. When the condition becomes false, the statement immediately after the loop is executed. The else clause is executed only when your condition becomes false.
i = 1
while i <= 5:
print(i)
i += 1
else:
print("No Item available.")
Note- Control Statements
1 2 3 4 5 No Item available.
Break Satement in Python
The break statement in Python is used to terminate a loop. This means whenever the interpreter encounters the break keyword, it simply exits out of the loop. Once it breaks out of the loop, the control shifts to the immediate next statement. Also, if the break statement is used inside a nested loop, it terminates the innermost loop and the control shifts to the next statement in the outer loop.
i = 1
while i <= 5:
if i == 4:
break
print(i)
i += 1
Output –
1 2 3
Continue in Python
The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.
i = 1
while i <= 5:
i += 1
if i == 4:
continue
print(i)
Output-
2 3 5 6
Exercise:
Write a Python program to check the validity of a password (input from users).
Validation :
- At least 1 letter between [a-z] and 1 letter between [A-Z].
- At least 1 number between [0-9].
- At least 1 character from [$#@].
- Minimum length 6 characters.
- Maximum length 16 characters.
Please verify your answer in below link. Control Statements in Python