Software Book
Software Book
CONTROL STRUCTURE
A control statement determines the control flow of a set of instructions. There are three
fundamental forms of control statements - sequential control , selection control , and iterative
control.
Sequential control is an implicit form of control in which instructions are executed in the
order that they are written.
Boolean expressions are used to denote the conditions for selection and iterative control
statements.
Logical/Boolean Operators:
Boolean algebra contains a set of Boolean operators denoted by and, or, and not in Python. These
logical operators can be used to construct arbitrarily complex Boolean expressions. The Boolean
operators are shown in Figure 3-5.
Logical and is true only when both its operands are true—otherwise, it is false.
Logical or is true when either or both of its operands are true, and thus false only when both
operands are false.
Logical not simply reverses truth values—not False equals True, and not True equals False.
Selection Control
if statements
if-else statements
if-elif statements
nested if statements
if Statement
Syntax:
if <condition>:
statements-block1
In the above syntax if the condition is true statements - block 1 will be executed.
Example
x=20
y=10
if x > y :
print(" X is bigger ")
Indentation in Python
In Python the amount of indentation of each program line is important.It is used to associate and
group statements, as shown in Figure.
A header in Python is a specific keyword followed by a colon. In the figure, the if-else statement
contains two headers, if and else. Headers that are part of the same compound statement must be
indented the same amount—otherwise, a syntax error will result.
The set of statements following a header in Python is called a suite (commonly called a block ).
The statements of a given suite must all be indented the same amount. A header and its
associated suite are together referred to as a clause .
A compound statement in Python may consist of one or more clauses. While four spaces is
commonly used for each level of indentation, any number of spaces may be used.
if..else statement
The if .. else statement provides control to check the true block as well as the false block.
Syntax:
if <condition>:
statements-block 1
else:
statements-block 2
x=10
y=20
if x > y :
else :
Multi-Way Selection:
Multi-Way Selection is one involving multiple nested if statements, and the other involving a
single if statement and the use of elif headers.
if..elif...statement: When we need to construct a chain of if statement(s) then ‘elif’ clause can
be used instead of ‘else’.
Syntax:
if <condition-1>:
statements-block 1
elif <condition-2>:
statements-block 2
else:
statements-block n
eg)
x=500
if x > 500 :
elif x == 500 :
else :
Nested if statements
Syntax:
if condition:
if condition:
statements
else:
statements
else:
statements
eg) mark = 72
else:
else:
print("You failed!!")
Iterative Control
An iterative control statement is a control statement providing the repeated execution of a set
of instructions. An iterative control structure is a set of instructions and the iterative control
statement(s) controlling their execution. Because of their repeated execution, iterative control
structures are commonly referred to as “loops.”
Syntax:
while <condition>:
statements block 1
[else:
statements block2]
In the while loop, the condition is any valid Boolean expression returning True or False.
The statements block1 is kept executed till the condition is True
If the else part is written, it is executed when the condition is tested Falsei=10 # intializing part
of the control variable
else:
print("hello")
A definite loop is a program loop in which the number of times the loop will iterate can be
determined before the loop is executed.
A indefinite loop is a program loop in which the number of times the loop will iterate is not
known before the loop is executed.
eg1)
sum = 0
current = 1
current =current + 1
eg2)i = input("Enter selection: ")
ii)for loop
Syntax:
for <variable> in <sequence>:
# body_of_loop
On every iteration it takes the next value from <sequence> until the end of sequence is
reached.
Eg1)
for x in "banana":
print(x)
Eg2)
numbers = (1,2,3,4,5)
sum = 0
sum = sum+val
Eg2)
sq = 0
print(sq)
Range function
eg)
print(val)
range(start, stop): generates a set of whole numbers starting from start to stop-1.
eg)
range(5, 9) is equivalent to [5, 6, 7, 8]
range(start, stop, step_size): The default step_size is 1 which is why when we didn’t
specify the step_size, the numbers generated are having difference of 1. However by
specifying step_size we can generate numbers having the difference of step_size.
eg)
range(1, 10, 2) is equivalent to [1, 3, 5, 7, 9]
In Python we can have an optional ‘else’ block associated with the loop.
The ‘else’ block executes only when the loop has completed all the iterations.
for x in range(6):
print(x)
else:
print("Finally finished!")
When a for loop is present inside another for loop then it is called a nested for loop.
Eg)
for num1 in range(3):
The break statement is used for premature termination of the current loop.
The break statement can be used in both while and for loops.
break
if letter == 'h':
break
The continue statement in Python returns the control to the beginning of the current loop.
When encountered, the loop starts next iteration without executing the remaining
statements in the current iteration.
The continue statement can be used in both while and for loops.
Syntax
Continue
if letter == 'h':
continue
The pass statement is also useful in places where your code will eventually go, but has
not been written yet i.e. in stubs.
Syntax
pass
if letter == 'h':
pass
List Structures
A list is a linear data structure , meaning that its elements have a linear ordering in which each
item in the list is identified by its index value .
Operations commonly performed on lists include retrieve, update, insert, delete (remove) and
append
List Traversal list traversal , a way of accessing each of the elements of a given list. A list
traversal is a means of accessing, one-by-one, the elements of a list. For example, to add up
all the elements in a list of integers, each element can be accessed one-by-one, starting with the
first, and ending with the last element.
A list in Python is a mutable, linear data structure of variable length, allowing mixed-type
elements. Mutable means that the contents of the list may be altered. Lists in Python use
zerobased
indexing. Thus, all lists have index values 0 ... n-1, where n is the number of elements
in the list. Lists are denoted by a comma-separated list of elements within square brackets as
shown below,
[1, 2, 3] ['one', 'two', 'three'] ['apples', 50, True]
Sequences
A sequence in Python is a linearly ordered set of elements accessed by an index number. Lists,
tuples, and strings are all sequences. Strings, like tuples, are immutable ; therefore, they cannot
be altered.
We give sequence operations common to strings, lists, and tuples in Figure 4-6.
Python’s for loop and while loop provides a convenient means of iterating over lists (and
other sequences).
Iterate through list in Python using a for Loop
A for statement is an iterative control statement that iterates once for each element in a
specified sequence of elements.
print(k)
print(k)
for k in 'Apple':
print(k)
Python’s range() method can be used in combination with a for loop to traverse and
iterate over a list in Python.
for x in range(len(lst)):
print(lst[x])
In the while loop version, loop variable must be initialized to 0 and incremented by 1
each time through the loop.
while(condition) :
Statement
k=0
print(nums[k])
k=k+1
An index variable is a
variable whose changing value is used to access elements of an indexed data structure .lst = [10,
20,30,40,50]
for k in lst:
sum=sum+k
print(sum)
for k in range(len(lst)):
sum=sum+lst[k]
print(sum)