networktestinggenius.tk
Your networking tutor
Exclusive networking testing notes
Learn Networking & Testing topics

Decision making
Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions.
The if…elif…else statement is used in Python for decision making.
1. if statements
2. if....else statements
3. nested if statements
Python if Statement Syntax
if test expression:
statement(s)
a = input("Enter your age: ")
if a < 18:
print("You must be 18 to sign in")
op:
Enter your age: 12
You must be 18 to sign in
Syntax of if...else
if test expression:
Body of if
else:
Body of else
a = int ( input ( "Enter a first number: " ) )
b = int ( input ( "Enter second number: " ) )
if a is b :
print ( "a and b are same" )
else :
print ( "a and b are not same" )
op:
Enter a first number: 1
Enter second number: 2
a and b are not same
Syntax of if...elif...else
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
Enter a first number: 1
Enter second number: 1
a and b are same
a = input("Enter a number: ")
b = input("Enter another number: ")
if a>b:
print("First number is greater then Second number")
elif a==b:
print("Both number is equal")
else:
print("Second number is greater than First number")
op:
Enter first number: 1
Enter second number: 1
Both number is equal
Enter first number: 2
Enter second number: 1
First number is greater then Second number
Enter first number: 1
Enter second number: 2
Second number is greater than First number
Looping
Python programming language provides the following types of loops to handle looping requirements.
1. while loop
2. for loop
3. nested loop
Syntax of while Loop in Python
while test_expression:
Body of while
print("Print 10 pages of a book")
a=1
while a<10:
print "Page" ,a
a=a+1
op:
Print 10 pages of a book
Page 1
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
# Program to find the sum of given digit
n=153
sum=0
while n>0:
r=n%10
sum+=r
n=n/10
print sum
op:
9
Syntax of for Loop
for val in sequence:
Body of for
for i in range(1,5):
print(i)
op:
1
2
3
4
# Program to find the sum of all numbers stored in a list
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0
for val in numbers:
sum = sum+val
print "The sum is", sum
op:
48
# Program to find all the possible substrings
string = "Sujatha"
length = len(string)
alist = []
for i in range(length):
for j in range(i,length):
alist.append(string[i:j + 1])
print alist
op:
'S', 'Su', 'Suj', 'Suja', 'Sujat', 'Sujath', 'Sujatha', 'u', 'uj', 'uja', 'ujat', 'ujath', 'ujatha', 'j', 'ja', 'jat', 'jath', 'jatha', 'a', 'at', 'ath', 'atha', 't', 'th', 't
ha', 'h', 'ha', 'a']
Control statements
Python supports the following control statements.
1. break statement
2. continue statement
3. pass statement
1. break statement:
Terminates the loop statement and transfers execution to the statement immediately following the loop.
2. continue statement:
Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
3. pass statement:
The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.
for val in [1,2,3,4,5,6]:
if val is 4:
break
print(val)
print("The end")
for val in [100,20,35,40,50,60]:
if val >= 35:
print(val)
continue
print("The end")
for age in [10,20,18,17,10,60]:
if age <18:
pass
print 'Website cannot be displayed'