Module II
Module II
Logical operators, if, If-Else, While loop, For loop, List value, length,
operation and
deletion, Dictionary operation & methods, Tuples
Flow of Control
The order of execution of the statements in a program is known as flow of
control. The flow of
control can be implemented using control structures. Python
supports two types of control
structures—selection and repetition.
1. SELECTION
1. Simple if statement
if condition:
statement(s)
In the following example, if the age entered by the user is greater than
18, then print that the
user is eligible to vote. If the condition is true, then the indented
statement(s) are executed.
Example 6.1
age = int(input("Enter your age "))
if age >= 18:
print("Eligible to vote")
2. if .. else Statement
if condition:
statement(s)
else:
statement(s)
Let us now modify the example on voting with the condition that if the age
entered by the user
is greater than 18, then to display that the user is eligible to vote.
Otherwise display that the
user is not eligible to vote.
4. Nested if Statements
Nested if .. statements is one if… statement inside another if… statement.
In the program, for the operators "-" and "/", there exists an if..else
condition within the elif
block. This is called nested if. We can have many levels of nesting inside
if..else statements.
2. INDENTATION
3. REPETITION
Often, we repeat a tasks, for example, payment of electricity bill, which is
done every month.
Consider the life cycle of butterfly that involves four stages, i.e., a
butterfly lays eggs, turns into
a caterpillar, becomes a pupa, and finally matures as a butterfly. The
cycle starts again with
laying of eggs by the butterfly.
This kind of repetition is also called iteration. Repetition of a set of
statements in a program is
made possible using looping constructs.
Program to print the characters in the string ‘PYTHON’ using for loop.
Example
#start and step not specified
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#default step value is 1
>>> list(range(2, 10))
[2, 3, 4, 5, 6, 7, 8, 9]
#step value is 5
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
#step value is -1. Hence, decreasing sequence is generated
>>> list (range (0, -9, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8]
The function range() is often used in for loops for generating a sequence
of numbers.
2. while loop
while test_condition:
<body of while>
else:
<body of else>
The else block just after for/while is executed only when the loop is
NOT terminated by a
break statement
#Python program to show how to use else statement with the while loop
counter = 0
Jumps in Loops
Looping constructs allow programmers to repeat tasks efficiently. In
certain situations, when some
particular condition occurs, we may want to exit from a loop or skip some
statements of the loop before
continuing further in the loop. These requirements can be achieved
by using break and continue
statements, respectively. Python provides these statements as a tool
to give more flexibility to the
programmer to control the flow of execution of a program. Jumps
statements in Python are:
1. break
2. continue
3. pass
1. Break Statements
The break statement alters the normal flow of execution as it terminates
the current loop and resumes
execution of the statement following that loop.
sequence = {‘p’,’a’,’s’,’s’}
for val in sequence:
pass
STRINGS
String is a sequence which is made up of one or more UNICODE
characters. Here the character
can be a letter, digit, whitespace or any other symbol. A string can be
created by enclosing one
or more characters in single, double or triple quote.
Example
>>> str1 = 'Hello World!'
>>> str2 = "Hello World!"
>>> str3 = """Hello World!"""
>>> str4 = '''Hello World!''’
str1, str2, str3, str4 are all string variables having the same value 'Hello
World!'. Values stored in
str3 and str4 can be extended to multiple lines using triple codes as can
be seen in the following
example: