Unit II: Flow of Control and Strings
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.
- Indentation: Python uses indentation to define blocks of code. Consistent use of whitespace is mandatory for determining the structure of conditional and iterative statements.
- Sequential Flow: Execution of statements one after another.
- Conditional Flow: Execution based on whether a condition is true or false.
- Iterative Flow: Execution of a set of statements multiple times.
2. Conditional Statements
Conditional statements allow the program to make decisions and branch to different sections of code.
- if Statement: Executes a block if the condition is true.
- if-else Statement: Provides an alternative block if the condition is false.
- if-elif-else Statement: Used to check multiple conditions sequentially.
Practical Examples:
- Calculating the absolute value of a number.
- Sorting 3 numbers.
- Checking the divisibility of a number.
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).
- range() function: Generates a sequence of numbers, commonly used to control the number of iterations in a for loop.
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:
- Generating Patterns: Using nested loops to print geometric shapes.
- Summation of Series: Calculating the sum of a mathematical progression.
- Factorial: Finding the factorial of a positive number.
4. Break and Continue Statements
These statements provide finer control over loop execution.
- break: Terminates the current loop and moves execution to the next statement outside the loop.
- continue: Skips the remaining code in the current iteration and jumps to the next iteration of the loop.
- Nested Loops: Loops placed inside other loops.
5. Strings: Operations and Methods
A String is a sequence of characters.
Basic Operations:
- Concatenation: Joining two or more strings together using the
+ operator.
- Repetition: Repeating a string multiple times using the
* operator.
- Membership: Checking if a substring exists in a string using
in or not in.
- Slicing: Extracting a specific portion of a string using index ranges (e.g.,
string[start:stop]).
String Traversal and Methods:
- Traversing: Using loops to access each character in a string one by one.
- Built-in functions/methods: Python provides numerous methods like
.upper(), .lower(), .find(), and .replace() to manipulate strings.
Common Mistakes
- Indentation Errors: Mixing tabs and spaces or inconsistent levels of indentation.
- Infinite Loops: Forgetting to update the loop variable in a
while loop.
- String Immutability: Attempting to change a character in a string directly (strings are immutable).
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).