Beyond the EU: DORA and NIS 2 Directive's Global Impact
controlstatementspy.docx
1. The if statement executes only when specified condition is true. We can pass any valid
expression into the if parentheses.
There are various types of if statements in Python.
o if statement
o if-else statement
o nested if statement
Syntax:
if(condition):
statements
Ex:
a=10
if a==10:
print "Welcome to Durgasoft"
If Else:
The If statement is used to test specified condition and if the condition is true, if block
executes, otherwise else block executes.
The else statement executes when the if statement is false.
if(condition): False
statements
else: True
statements
Ex:
year=2000
if year%4==0:
print "Year is Leap"
else:
print "Year is not Leap"
Program To Find Even or Odd:
x=int(input(print("enter any number:")))
r=x%2
if r==0:
2. print("Even Number")
else:
print("Odd Number")
Nested If Else:
In python, we can use nested If Else to check multiple conditions. Python
provides elif keyword to make nested If statement.
This statement is like executing a if statement inside a else statement.
If statement:
Body
elif statement:
Body
else:
Body
EX:
a=10
if a>=20:
print "Condition is True"
else:
if a>=15:
print "Checking second value"
else:
print "All Conditions are false"
x=int(input(print("Enter Any Num:")))
if x==1:
print("One")
elif x == 2:
print("Two")
elif x == 3:
print("Three")
elif x == 4:
print("Four")
3. elif x == 5:
print("Five")
else:
print("Invalid Number")
For Loop:
Python for loop is used to iterate the elements of a collection in the order that they
appear. This collection can be a sequence(list or string).
for <variable> in <sequence>:
x=["mohan",20,34.5]
for i in x:
print(i)
To Find Sum of 10 Numbers:
sum=0
for n in range(1,11):
sum+=n
print (sum)
num=2
for a in range (1,6):
print num * a
Python Nested For Loops
Loops defined within another Loop are called Nested Loops. Nested loops are used to
iterate matrix elements or to perform complex computation.
When an outer loop contains an inner loop in its body it is called Nested Looping.
4. num_list = [1, 2, 3]
char_list = ['a', 'b', 'c']
for n in num_list:
print(n)
for l in char_list:
print(l)
Ex:
list_of_list = [['coke', 'sprite', 'pepsi'],[1, 2, 3],[1.3, 1.4,1.5]]
for list in list_of_list:
for item in list:
print(item)
Python While Loop
In Python, while loop is used to execute number of statements or body till the specified
condition is true. Once the condition is false, the control will come out of the loop.
Syntax:
while <expression>:
Body
i=1
while i<=5:
print("Hello")
i=i+1
With the while loop we can execute a set of statements as long as a
condition is true.
i = 1
while i < 6:
print(i)
i += 1
5. The break Statement
With the break statement we can stop the loop even if the while condition is
true:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
drinks = ["coke", "pepsi", "sprite"]
for x in drinks:
if x == "pepsi":
break
print(x)
The continue Statement
With the continue statement we can stop the current iteration, and continue
with the next:
Continue to the next iteration if i is 3:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)