Python Control Structures

Control structures allow you to manage the flow of your Python program. They help you make decisions, repeat tasks, and control loop execution. This lesson will cover:

  • Operators: Perform operations on values.

  • If, Else, and Elif Statements: Conditional statements for decision-making.

  • For-loop and While-loop Statements: Repeating code execution.

  • Control Flow Statements: Adjusting loop behavior using break, continue, and pass.

Operators

Python supports various operators that allow you to manipulate data and evaluate conditions.

Arithmetic Operators:

These perform basic mathematical operations.

  • + : Addition

  • – : Subtraction

  • * : Multiplication

  • / : Division

  • ** : Exponentiation

  • % : Modulus (remainder after division)

  • // : Floor division (integer division, without remainder)

Example:


Comparison Operators:

These are used to compare two values and return True or False.

  • == : Equal to

  • != : Not equal to

  • > : Greater than

  • < : Less than

  • >= : Greater than or equal to

  • <= : Less than or equal to

Example:

Tutorials dojo strip

Logical Operators:

These are used to combine conditional statements.

  • and : Returns True if both statements are true.

  • or : Returns True if one of the statements is true.

  • not : Reverses the result.

Example:

If, Else, and Elif Statements

These conditional statements are used to control the flow of execution based on conditions. Python evaluates conditions and executes the corresponding block of code.

Syntax:

Example:

Explanation:

The if block executes if the first condition (age < 18) is true.

The elif block executes if the if condition is false but the second condition (age == 18) is true.

The else block executes if none of the conditions are true.

For-loop and While-loop Statements

Loops allow you to repeat blocks of code multiple times.

For-loop

A for loop is used to iterate over a sequence such as a list, tuple, dictionary, string, or range of numbers. The loop runs once for each item in the sequence.

Looping through a List:

Explanation:

The loop iterates over each item in the islands list and prints it.


Looping through a String:

Explanation:

The loop iterates over each character in the string word and prints it.


Looping through a Dictionary:

Explanation:

The loop iterates through each key-value pair in the person dictionary and prints them.


Using range() in For-loops

The range() function generates a sequence of numbers, often used in for-loops. It can take up to three arguments: start, stop, and step.

Basic Range Example:

Explanation:

The loop runs for each number in the range from 0 to 4.


Specifying Start, Stop, and Step:

Explanation:

The loop starts at 2, increments by 2 each time, and stops before reaching 10.


While-loop

A while loop executes as long as a given condition is true. If the condition never becomes false, the loop will continue indefinitely (becoming an “infinite loop”).

Syntax:

Example:

Explanation:

The loop will keep executing as long as count <= 5 is true. The count variable increments on each iteration.

Control Flow Statements: break, continue, and pass

break Statement

The break statement immediately terminates the loop, regardless of the loop condition.

Syntax:

Example:

Explanation:

The loop stops when number equals 5, even though the loop was designed to run until 9.


continue Statement

The continue statement skips the current iteration and moves to the next iteration of the loop.

Syntax:

Example:

Explanation:

The number 2 is skipped, and the loop continues with the next iteration.


pass Statement

The pass statement is a null operation. It does nothing and is used as a placeholder when a statement is syntactically required but you don’t want to execute any code.

Syntax:

Example:

Explanation:

When number equals 1, the pass statement is executed, which does nothing, and the loop continues.

Tutorials dojo strip