# Control Flow

Control flow in Python refers to the order in which statements are executed within a program. Python provides several control flow structures that allow you to make decisions, repeat actions, and control the flow of your code. The primary control flow structures in Python include:

* **Conditional Statements (`if`, `elif`, `else`):**

  * Conditional statements are used to make decisions in your code based on certain conditions.
  * The `if` statement allows you to execute a block of code if a condition is true.
  * The `elif` (short for "else if") statement is used to test multiple conditions if the preceding `if` condition is false.
  * The `else` statement defines what should happen if none of the preceding conditions are true.

  Example:

  ```python
  x = 10 
  if x > 5: 
      print("x is greater than 5") 
  elif x == 5: 
      print("x is equal to 5")
  else: 
      print("x is less than 5")
  ```

* **Loops (`for` and `while`):**

  * Loops allow you to repeat a block of code multiple times.
  * The `for` loop is typically used for iterating over sequences (e.g., lists, strings) or for a specified number of times.
  * The `while` loop continues executing a block of code as long as a specified condition is true.

  Example (for loop):

  ```python
  fruits = ["apple", "banana", "cherry"] 
  for fruit in fruits: 
      print(fruit)
  ```

  Example (while loop):

  ```python
  count = 0 
  while count < 5: 
      print(count) 
      count += 1
  ```

* **Break and Continue Statements:**

  * The `break` statement is used to exit a loop prematurely, even if the loop condition is still true.
  * The `continue` statement is used to skip the rest of the current iteration of a loop and continue to the next iteration.

  Example (break):

  ```python
  numbers = [1, 2, 3, 4, 5] 
  for number in numbers: 
      if number == 3: 
          break 
      print(number)
  ```

  Example (continue):

  ```python
  numbers = [1, 2, 3, 4, 5] 
  for number in numbers: 
      if number == 3: 
          continue 
      print(number)
  ```

* **Comprehensions:**

  * Comprehensions are concise ways to create lists, dictionaries, and sets using a single line of code. They often involve a `for` loop and an optional `if` condition.

  ```python
  # Example - list comprehension:
  squares = [x ** 2 for x in range(1, 6)]

  # Example - list comprehension with if statement:
  even_squares = [num**2 for num in range(10) if num%2==0]

  # Example - list comprehension with if-else statement:
  even_squares = [num**2 if num%2==0 else 0 for num in range(10)]


  # Example - dictionary comprehension:
  fruit_lengths = {fruit: len(fruit) for fruit in fruits}
  ```

These control flow structures enable you to write flexible and dynamic code in Python, allowing you to handle various scenarios, make decisions, and perform repetitive tasks. Understanding how to use these constructs effectively is essential for writing efficient and readable Python programs.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dshub.gitbook.io/ds-hub/python/basics/control-flow.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
