0% found this document useful (0 votes)
11 views10 pages

Python Programming Control Structures

The document covers key concepts in Python programming, including control structures, Boolean expressions, indentation, and the range function. It explains the differences between definite and indefinite loops, details the list and string data structures along with their methods, and discusses selection and iterative control statements. The content is structured into parts with varying question formats, providing definitions, examples, and explanations of fundamental programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Python Programming Control Structures

The document covers key concepts in Python programming, including control structures, Boolean expressions, indentation, and the range function. It explains the differences between definite and indefinite loops, details the list and string data structures along with their methods, and discusses selection and iterative control statements. The content is structured into parts with varying question formats, providing definitions, examples, and explanations of fundamental programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON PROGRAMMING Unit-2

PART - A

Two Marks:

1. Define Control Structures.


 Control flow is the order that instructions are executed in a program.
 A control statement is a statement that determines the control flow of a set
of instructions.
 There are three fundamental forms of control that programming languages
provide sequential control , selection control , and iterative control.

2. What is Boolean Expression in Python?


 The Boolean data type contains two Boolean values, denoted as True and
False in Python.
 A Boolean expression is an expression that evaluates to a Boolean value.
 Boolean expressions are used to denote the conditions for selection and
iterative control statements.

3. What is Indentation in Python?


 Indentation refers to the spaces at the beginning of a code line.
 Where in other programming languages the indentation in code is for
readability only, the indentation in Python is very important.
 Python uses indentation to indicate a block of code.

4. What is the use of range ( ) function in Python?


 The python range() function generates a sequence of numbers.
 By default, the sequence starts at 0, increments by 1, and stops before the
specified number.
 The range() function returns an immutable sequence of numbers.
Syntax:
range(start, stop, step)

5. What are the basic building blocks of python programming?


 Variables
 Identifiers
 Keywords
 Operators
 String
 Data Types
 Selection Control
 Looping Control

[Link], Assistant Professor & Head, Department of Computer Science & Applications, Pachaiyappa’s College, Chennai-30.
PYTHON PROGRAMMING Unit-2

PART - B

Five Marks:

6. Differentiate Definite Loop vs. Indefinite Loop.


Definite Loops
 Definite loop is a loop in which the number of times it is going to execute is known in
advance before entering the loop.
 The number of iterations it is going to repeat will be typically provided through an
integer variable.
 In general, for loops are considered to be definite loops.
Example - for Loop

Output

Indefinite Loops
 In an indefinite loop, the number of times it is going to execute is not known in
advance.
 Typically, an indefinite loop is going to be executed until some condition is satisfied.
 While loops are commonly used to implement indefinite loops.
Example - While Loop

Output

[Link], Assistant Professor & Head, Department of Computer Science & Applications, Pachaiyappa’s College, Chennai-30.
PYTHON PROGRAMMING Unit-2

7. Explain the List and its methods in detail.


List:
 A list is a data structure in python that is a mutable or changeable, ordered
sequence of elements.
 Each element or value that is inside a list is called item.
 Lists are defined by having values between square brackets [ ], separated by
commas.
 Each item in a list corresponds to an index number, which is an integer value
starting with the index number 0.
Example:
age=[10,18,20]
print(age)
print(age[1])

Python List Methods:


1. append( )
adds an item to the end of the list.
Example:
fruits=[„apple‟, „banana‟, „orange‟]
print(fruits)
[Link](„mango‟)
print(fruits)
2. insert( )
inserts an item at the specified index in the list.
Example:
fruits=[„apple‟, „banana‟, „orange‟]
print(fruits)
[Link](2, „mango‟)
print(fruits)
3. remove( )
remove the specified value from the list.
Example:
numbers=[2, 4, 7, 8]
[Link](4)
print(numbers)
4. len( )
To find the number of elements (length) of a list.
Example:
numbers=[2, 4, 7, 8]
print(len(numbers))
5. sort( )
sort the list in ascending / descending order
Example:
[Link], Assistant Professor & Head, Department of Computer Science & Applications, Pachaiyappa’s College, Chennai-30.
PYTHON PROGRAMMING Unit-2

numbers=[11, 3, 7, 5, 2]
print([Link]())
6. reverse( )
The reverse() method reverses the elements of the list.
Example:
numbers=[2, 4, 7, 8]
print([Link]())

8. Discuss about the String in Python and explain its method.


String:
 In python, a string is a sequence of characters enclosed within a single
quotes or double quotes.
 In python, Strings are immutable, that means the characters of a string
cannot be changed.
Example:
name= “python”
print(name)

Python String Methods:


1. upper( )
The upper() method converts all lowercase characters in a string into
uppercase characters and returns it.
Example:
message=”python”
print([Link]())
2. lower( )
The lower() method converts all uppercase characters in a string into
lowercase characters and returns it.
Example:
message=”python”
print([Link]())
3. replace( )
The replace() method replaces each matching occurrences of a
substring with another string.
Example:
text=‟Java programming‟
reptext=[Link](„Java‟, „Python‟)
print(reptext)
4. split( )
The split() method breaks down a string into a list of substrings using
a chosen operator.
Example:
text=‟I Love Python programming‟
[Link], Assistant Professor & Head, Department of Computer Science & Applications, Pachaiyappa’s College, Chennai-30.
PYTHON PROGRAMMING Unit-2

print([Link]())
5. isnumeric( )
The isnumeric() method checks if all characters in the string are
numeric.
Example:
pin=”1527”
print([Link]())

PART - C

TEN Marks:

9. Discuss in detail about the Selection Control in Python.


 In Python, the selection statements are also known as decision making statements or
branching statements.
 The selection statements are used to select a part of the program to be executed based
on a condition.
 Selection control is provided by a control statement that selectively executes
instructions.
Python provides the following selection statements.
 if statement
 if-else statement
 if-elif statement
 Nested if statement
If Statement
 An if statement is a selection control statement based on the value of a given Boolean
expression.
 In Python, we use the if statement to test a condition and decide the execution of a
block of statements based on that condition result.
 The if statement checks, the given condition then decides the execution of a block of
statements.
 If it is True, then the block of statements is executed and if it is False, then the block of
statements is ignored.
Syntax
if condition:
block of statements

Example

Output

[Link], Assistant Professor & Head, Department of Computer Science & Applications, Pachaiyappa’s College, Chennai-30.
PYTHON PROGRAMMING Unit-2

If else Statement
 In Python, we use the if-else statement to test a condition and pick the execution of a
block of statements out of two blocks based on that condition result.
 The if-else statement checks the given condition then decides which block of
statements to be executed based on the condition result.
 If the condition is True, then the true block of statements is executed and if it is False,
then the false block of statements is executed.
 The execution flow of if-else statement is as follows.
Syntax

Example

Output

Nested if statement
 In Python, we have if statements inside if statements, this is called nested if
statements.
 In Python, an if…else statement inside another if…else statement is called nested if
statements.
 In Python, an if…elif…else statement inside another if…elif…else statement is called
nested if statements.

Syntax

[Link], Assistant Professor & Head, Department of Computer Science & Applications, Pachaiyappa’s College, Chennai-30.
PYTHON PROGRAMMING Unit-2

Example

if…elif…else statement
 The if…elif…else statement used in Python helps automate that decision
making process.
 If statements may contain only one else header. Thus, if-else statements
must be nested to achieve multi-way selection.
 Python, however, has another header called elif (“else-if”) that provides
multi-way selection in a single if statement.
 All the headers of an if-elif statement are indented the same amount, thus
avoiding the deeply nested levels of indentation with the use of if-else
statements.
 A final else clause may be used for “catch-all” situations
Syntax

Example

[Link], Assistant Professor & Head, Department of Computer Science & Applications, Pachaiyappa’s College, Chennai-30.
PYTHON PROGRAMMING Unit-2

Output

10. Discuss in detail about the Iterative Control in Python.


 In Python, the iterative statements are also known as looping statements or repetitive
statements.
 An iterative control statement is a control statement providing the repeated execution
of a set of instructions.
 The iterative statements are used to execute a part of the program repeatedly as long
as a given condition is True.
 Python provides the following iterative statements.
 while statement
 for statement
While Statement
 A while statement is an iterative control statement that repeatedly executes a set of
statements based on a provided Boolean expression (condition).
 All iterative control needed in a program can be achieved by use of the while
statement.
 In Python, the while statement is also known as entry control loop statement because
in the case of the while statement, first, the given condition is verified then the
execution of statements is determined based on the condition result. The general
syntax of while statement in Python is as follows.

Example:

Output:

for Statement
 In Python, the for statement is used to iterate through a sequence like a list, a tuple, a
set, a dictionary, or a string.
 The for statement is used to repeat the execution of a set of statements for every
element of a sequence.
 The general syntax of for statement in Python is as follows.
for <variable> in <sequence>:
suite
the variable is stored with each element from the sequence for every iteration.
[Link], Assistant Professor & Head, Department of Computer Science & Applications, Pachaiyappa’s College, Chennai-30.
PYTHON PROGRAMMING Unit-2

for Statement Example


1. Python code to illustrate for statement with List

Output

2. Python code to illustrate for statement with Tuple

Output

3. Python code to illustrate for statement with Set

Output

4. Python code to illustrate for statement with Dictionary

Output

[Link], Assistant Professor & Head, Department of Computer Science & Applications, Pachaiyappa’s College, Chennai-30.
PYTHON PROGRAMMING Unit-2

5. Python code to illustrate for statement with String

Output

6. Python code to illustrate for statement with Range Function

Output

~~~~~~~~~~~~~~~ALL THE BEST~~~~~~~~~~~~~~~~

[Link], Assistant Professor & Head, Department of Computer Science & Applications, Pachaiyappa’s College, Chennai-30.

You might also like