Python Control Flow, Functions

Control flow statements are divided into three categories:

  • Conditional statements
  • Iterative statements
  • Transfer statements

Conditional Statements

Conditional statements acts depending on the validity of a statement, whether a statement is True or False. You can execute various blocks depending on the conditions for their execution. Conditional  statements can be if statement, if-else statements, if-elif-else statement, or nested if-else statements.

Iterative Statements

Iterative statements are statements that allows us to execute a block of code repeatedly given that the condition is True. This is also known as a loop statement.

Python provides us with two types of loops and they are:

  • While loop
  • For Loop

Control Flow: Iterative Statement – For Loop

For loop is used to iterate over a sequence such as list, tuples, strings, sets and other iterables. With for loops you can automate repeated tasks.

While Loop

While loop is used to repeatedly execute a statement given the statement is True. If the statement never returns False, the statement continuously gets executed.

Transfer Statement

Transfer statements are ways to alter the execution of the program in a certain way. Python provides us with three types transfer statements:

  • break 
  • continue
  • pass

Break statement is used inside the loop to exit out of the loop.

Continue statement is used inside the loop to skip the current iteration and move to the next iteration.

Pass statement is used to do nothing in a function or in a loop.

Functions

A Function is a block of code defined with a name. Functions can take in arguments and return a value. Function improves efficiency and reduces error because if reusability of the code. The major benefits of using functions is code reusability and  modularity.

The def keyword with the function name is used to create the function. You can tend pass in arguments into the function. The you perform the task you want to perform within the code block of the function. And then you can return a value if you decide to.
Note: A function name should be very descriptive.

Calling a Function

You can call a function by calling its function name and passing the required arguments if needed.

Scroll to Top