Control Statements in Python
Control Statements in Python
Indentation in Python
Indentation in Python
• In Python, indentation is used to declare a block. If two
statements are at the same indentation level, then they are
the part of the same block.
Remember:
input () function is used to get input from user.
Example:
a=input (“Enter a value”)
Conditional Statements in Python Cont..
If-else statement:
• The if-else statement provides an else block combined with the if
statement which is executed in the false case of the condition.
Syntax:
Output:
if condition:
python ifelsedemo.py
#block of statements
Enter your age: 19
else:
You are eligible to vote!!
#another block of statements (else-block)
Example: ifelsedemo.py
age = int(input("Enter your age : "))
if age>=18:
print("You are eligible to vote !!")
else:
print("Sorry! you have to wait !!"))
Conditional Statements in Python Cont..
If-elif-else statement:
• The elif statement enables us to check multiple conditions and
execute the specific block of statements depending upon the true
condition among them.
Syntax:
if condition1:
# block of statements
elif condition2:
# block of statements
elif condition3:
# block of statements
else:
# block of statements
Conditional Statements in Python Cont..
Example: maxnum.py
a=int(input("Enter a value : "))
b=int(input("Enter b value : "))
c=int(input("Enter c value : "))
if (a>b) and (a>c):
print("Maximum value is :",a)
elif (b>a) and (b>c):
print("Maximum value is :",b)
else:
print("Maximum value is :",c)
Output:
python maxnum.py
Enter a value: 10
Enter b value: 14
Enter c value: 9
Maximum value is: 14
Loop Statements in Python
Loop Statements in Python
• Sometimes we may need to alter the flow of the program. If the
execution of a specific code may need to be repeated several
numbers of times then we can go for loop statements.
• In python, the following are loop statements
o while loop
o for loop
while loop:
• With the while loop we can execute a set of statements as long
as a condition is true. The while loop is mostly used in the case
where the number of iterations is not known in advance.
Syntax:
while expression:
Statement(s)
Loop Statements in Python Cont..
for loop:
• The for loop in Python is used to iterate the statements or a
part of the program several times. It is frequently used to
traverse the data structures like list, tuple, or dictionary.
Syntax:
for iterating_var in sequence:
statement(s)
Output:
python fedemo.py
1234
for loop completely exhausted
Jump Statements in Python
Jump Statements in Python
• Jump statements in python are used to alter the flow of a loop
like you want to skip a part of a loop or terminate a loop.
• In python, the following are jump statements
o break
o continue
break:
• The break is a keyword in python which is used to bring the
program control out of the loop.
• The break statement breaks the loops one by one, i.e., in the
case of nested loops, it breaks the inner loop first and then
proceeds to outer loops.
• The break is commonly used in the cases where we need to
break the loop for a given condition.
Syntax: break
Jump Statements in Python Cont..
Example: breakdemo.py
i = 1 Output:
while i < 6: python breakdemo.py
print(i)
1
if i == 3:
break 2
i += 1 3
continue:
• The continue statement in python is used to bring the program
control to the beginning of the loop.
• The continue statement skips the remaining lines of code inside
the loop and start with the next iteration.
• It is mainly used for a particular condition inside the loop so
that we can skip some specific code for a particular condition.
Syntax: continue
Jump Statements in Python Cont..
Example: continuedemo.py
str =input("Enter any String : ")
for i in str:
if i == 'h':
continue;
print(i,end=" ");
Output:
python continuedemo.py
Enter any String : python
pyton