Here you can understand the basic concepts of python programing & then move towards advanced programing.
Python Control Statements

Control Statements

In Python, control statements are statements that allow you to control the flow of execution in the program. These statements alter the order in which instructions are executed in your code, based on certain conditions.

Python has three main types of control statements:

  1. Conditional statements: These statements are used to make decisions in your code. The most common conditional statement is the if statement, which allows you to execute a block of code if a certain condition is true, and another block of code if the condition is false. The if statement can be extended with elif (short for else if) and else statements to provide additional conditions to test.

    Python has three types of conditional statements:
    if, elif, and else. These statements are used to control the flow of execution in a program based on certain conditions.

    1. The if statement: The if statement is used to test a single condition. If the condition is true, then the block of code inside the if statement is executed. If the condition is false, then the block of code is skipped.

      Example:

      x = 5
      if x > 0:
          print("x is positive")

      In this example, the condition x > 0 is true, so the block of code inside the if statement is executed, which prints "x is positive" to the console.

    2. The else statement: The if-else statement is used to execute a block of code if the condition in the if statement is false.

      Example:

      x = 0
      if x > 0:
          print("x is positive")
      else:
          print("x is zero")

      In this example, the condition x > 0 is false, so the block of code inside the else statement is executed, which prints "x is zero" to the console.

    3. The elif statement: The elif statement is short for "else if". It is used to test multiple conditions, one after the other. If the first condition is false, then the next condition is tested. If that condition is true, then the block of code inside the elif statement is executed. If the second condition is also false, then the next elif statement (if there is one) is tested, and so on. If all the conditions are false, then the block of code inside the else statement (if there is one) is executed.

      Example:

      x = 0
      if x > 0:
          print("x is positive")
      elif x < 0:
          print("x is negative")
      else:
          print("x is zero")

      In this example, the condition x > 0 is false, and the condition x < 0 is true, so the block of code inside the elif statement is executed, which prints "x is negative" to the console.

  2. Loop statements: These statements allow you to execute a block of code repeatedly, based on a certain condition. The most common loop statements in Python are the for loop and the while loop. The for loop is used to iterate over a sequence of values, such as a list or tuple, while the while loop is used to repeatedly execute a block of code as long as a certain condition is true.

    In Python, there are three main types of looping statements: for, while, and nested loops. Each type of loop serves a specific purpose and can be used to accomplish a variety of tasks.

    1. For Loops: For loops are used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). The syntax for a for loop is as follows:

      for variable in sequence:
      # do something with variable

      Here, the variable takes on the value of each item in the sequence, and the loop body is executed once for each item in the sequence.

      fruits = ["apple", "banana", "cherry"]
      for x in fruits:
          print(x)

      1. For Loop with Range: The range() function generates a sequence of numbers that can be used with a for loop. The syntax is:

        for i in range(start, stop, step):
        # do something with i

        Example

        for i in range(1, 5):
            print(i)

        OUTPUT:
        1
        2
        3
        4

      2. For Loop with Enumerate: The enumerate() function is used to iterate over a sequence with an index. The syntax is:

        for i, item in enumerate(sequence):
        # do something with i and item

        Example:

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

        OUTPUT:

        0 apple
        1 banana
        2 cherry

      3. For Loop with Zip: The zip() function is used to iterate over two or more sequences at the same time. The syntax is:

        for item1, item2, ... in zip(sequence1, sequence2, ...):
        # do something with item1, item2, ...

        Example:

        fruits = ["apple", "banana", "cherry"]
        colors = ["red", "yellow", "green"]
        for fruit, color in zip(fruits, colors):
            print(fruit, color)

        OUTPUT:

        apple red
        banana yellow
        cherry green

      4. For Loop with List Comprehension: List comprehension is a concise way of creating a new list by iterating over an existing list. The syntax is:

        new_list = [expression for item in sequence if condition]

        Example:

        numbers = [1, 2, 3, 4, 5]
        squares = [num**2 for num in numbers]
        print(squares)

        OUTPUT:

        [1, 4, 9, 16, 25]

    2. while loop is used to execute a block of code repeatedly as long as a certain condition is true. Here are some examples of while loops in Python:
      1. The most common use of a while loop is to execute a block of code repeatedly until a certain condition is no longer true.
        The syntax is:

        while condition:
        # do something

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

        OUTPUT:

        0
        1
        2
        3
        4

  3. Control transfer statements: These statements allow you to transfer control of the program to a different part of the code, based on certain conditions. The most common control transfer statements in Python are the break and continue statements. The break statement is used to exit a loop prematurely, while the continue statement is used to skip over certain iterations of a loop.Control transfer statements in Python are used to change the normal flow of execution in a program. There are three types of control transfer statements in Python: break, continue, and pass. Here are some examples of each:
    1. break statement: The break statement is used to exit a loop prematurely. It is often used in combination with a conditional statement to stop the loop when a certain condition is met. Here's an example:

      for i in range(10):
          if i == 5:
              break
          print(i)

      In this example, the loop will print the values of i from 0 to 4, and then exit when i equals 5. The output will be:

      0
      1
      2
      3
      4

    2. continue statement: The continue statement is used to skip the current iteration of a loop and move on to the next iteration. It is often used in combination with a conditional statement to skip over certain values. Here's an example:

      for i in range(10):
          if i == 5:
              continue
          print(i)

      In this example, the loop will print the values of i from 0 to 9, but skip over the value of 5. The output will be:

      0
      1
      2
      3
      4
      6
      7
      8
      9

    3. pass statement: The pass statement is used as a placeholder when a statement is required syntactically, but no code needs to be executed. It is often used when writing stub functions or defining empty classes. Here's an example:

      def my_function():
          pass

      In this example, my_function() is defined but does not contain any code. This can be useful when you want to write the function skeleton before filling in the details.


Advertisement

Advertisement