Control Flow and Loops
Control flow statements are used to control the flow of a program, while loops are used to repeat a section of code multiple times.
Conditional Statements
Conditional statements are used to execute different parts of code based on whether a certain condition is true or false. In Python, conditional statements are created using the if, elif, and else keywords.
Here’s an example of a simple conditional statement:
In this example, the program checks whether x is greater than zero, equal to zero, or less than zero, and prints a corresponding message.
Loops
Loops are used to repeat a section of code multiple times. In Python, there are two types of loops: for loops and while loops.
For Loops
For loops are used to iterate over a sequence of values. Here’s an example of a simple for loop:
In this example, the program iterates over a list of fruits and prints each one.
While Loops
While loops are used to repeat a section of code while a certain condition is true. Here’s an example of a simple while loop:
In this example, the program prints the value of x and increments it by 1 until x is greater than or equal to 10.
Control Flow Keywords
Python has several keywords that can be used to control the flow of a program, including break, continue, and pass.
