Python Notes (KNC-402) UNIT-2
Python Notes (KNC-402) UNIT-2
Python Notes (KNC-402) UNIT-2
Decision Making
if statements
1 An if statement consists of a boolean expression followed by one or
more statements.
if...else statements
2 An if statement can be followed by an optional else statement, which
executes when the boolean expression is FALSE.
nested if statements
3 You can use one if or else if statement inside another if or else if
statement(s).
if-else Statement
Input Customer ID
Input Last Meter Reading(LMR)
Input Current Meter Reading(CMR)
Calculate the bill as per given unit consumption—
For first 150 units, charges are Rs. 3.0 per unit
For units >150 and <=300, charges are Rs. 4.5 per unit
For units>300 and <=500, charges are Rs. 6.0 per units
For Units >500, charges are Rs. 8.0 per unit.
Loops
A loop statement allows us to execute a statement or group of statements multiple times.
Python programming language provides the following types of loops to handle looping
requirements.
while Loop
For loop provides a mechanism to repeat a task until a particular condition is True. It is usually
known as a determinate or definite loop because the programmer knows exactly how many
times the loop will repeat. The for...in statement is a looping statement used in Python to iterate
over a sequence of objects.
The range() function is a built-in function in Python that is used to iterate over a sequence of
numbers. The syntax of range() is range(beg, end, [step])
The range() produces a sequence of numbers starting with beg (inclusive) and ending with one
less than the number end. The step argument is option (that is why it is placed in brackets). By
default, every number in the range is incremented by 1 but we can specify a different increment
using step. It can be both negative and positive, but not zero.
If range() function is given a single argument, it produces an object with values from 0 to
argument-1. For example: range(10) is equal to writing range(0, 10).
• If range() is called with two arguments, it produces values from the first to the second. For
example, range(0,10).
• If range() has three arguments then the third argument specifies the interval of the sequence
produced. In this case, the third argument must be an integer. For example, range(1,20,3).
Python allows its users to have nested loops, that is, loops that can be placed inside other
loops. Although this feature will work with any loop like while loop as well as for loop.
A for loop can be used to control the number of times a particular set of statements will
be executed. Another outer loop could be used to control the number of times that a
whole loop is repeated.
Loops should be properly indented to identify which statements are contained within
each for statement.
The break statement is used to terminate the execution of the nearest enclosing loop in which it
appears. The break statement is widely used with for loop and while loop. When interpreter
encounters a break statement, the control passes to the statement that follows the loop in which the
break statement appears.
Like the break statement, the continue statement can only appear in the body of a loop. When the
compiler encounters a continue statement then the rest of the statements in the loop are skipped and
the control is unconditionally transferred to the loop-continuation portion of the nearest enclosing
loop.