Unit II: Flow of Control and Strings

Table of Contents

1. Introduction to Flow of Control

Flow of Control refers to the order in which statements are executed in a program. By default, Python executes statements sequentially, from top to bottom.


2. Conditional Statements

Conditional statements allow the program to make decisions and branch to different sections of code.

Practical Examples:


3. Iterative Statements (Loops)

Loops are used to repeat a block of code multiple times.

The for Loop

The for loop is used for iterating over a sequence (like a list, tuple, or string).

The while Loop

The while loop repeats a block as long as a specified condition remains true.

[Image of while loop flowchart in Python]

Suggested Programs:


4. Break and Continue Statements

These statements provide finer control over loop execution.


5. Strings: Operations and Methods

A String is a sequence of characters.

Basic Operations:

String Traversal and Methods:

Common Mistakes


Frequently Asked Questions

Q: What happens if I forget the colon (:) after an 'if' statement?
Python will raise a SyntaxError because the colon is required to signal the start of a code block.

Q: How does range(5) differ from range(1, 5)?
range(5) produces 0, 1, 2, 3, 4. range(1, 5) produces 1, 2, 3, 4 (it stops before the end value).