This is the 11th day of my participation in Gwen Challenge

An overview of the

Now that we’re done with the data types, we’re ready for the second step of programming, which is flow control statements.

The process control outline diagram is as follows:

1. Condition control, use if… else.. Statement evaluates the conditional result (True/False)

2. Loop statements are used to execute one or more statements. There are two forms, while and for.

Can often be used in conjunction with conditional control statements to implement complex code logic such as

  • If the conditions are correct, the statements in the body of the loop are repeatedly executed
  • The condition is checked to see if it is True after each execution
  • If True, the statement in the body of the loop continues to be repeated.

1. Conditional branch types

Conditions execute different code depending on the result, which is called a selection structure or branch structure.

If else statements in Python can be broken down into three forms

  • If statement
  • If the else statement
  • If elif else statement
1.1 single branch

The single-branch structure only evaluates if and executes the statement if the condition is True, otherwise the conditional block is not executed

1.2 double branch

Double branch structure, when the condition is True, statement block 1 is executed. If the condition is False, statement block 2 is executed.

More than 1.3 branch

Multi – branched structure is a multi – conditional judgment.

2. Loop statements

Python loop statements have while and for bodies. Their use will be described in more detail below.

Before we start, let’s find a little practice to print out the multiplication table, with a problem solving mind, and use two loops.

2.1 the while loop

While loop writing format

While condition: body of loopCopy the code

Row = 1 while row <= 9: col = 1 while col <= row: print('%d * %d = %d ' %(col,row,col*row),end='') col += 1 print('') row += 1Copy the code

2.2 a for loop

The Python for loop can iterate over any iterable, such as a list or a string.

For loop writing format

For variable in iterable: body of loopCopy the code

# using a for loop to achieve the following for x in range (1, 10) : for y in range (1, 10) : print (" {0} {1} = {2} ". The format (x, y, (x * y)), end = "\ t") print ()Copy the code
2.3 Nested loops

Incredible, is a cycle embedded in a cycle.

For variable in iterable: for variable in iterable: loop body 2 Loop body 1Copy the code

While condition: while condition body The body of a loopCopy the code

3. Matching statements in the loop

3.1 break

The break statement can be used for while and for loops to end the loop.

When there are nested loops, the break statement can only break out of the nearest loop.

For letter in 'letter ': if letter == 'n': break print(' current letter :', letter)Copy the code

3.2 contiue

The continue statement is used to end the loop and continue to the next.

When multiple loops are nested, continue is also applied to the nearest loop.

For letter in 'JueJing': if letter == 'u': continue print(' current letter :', letter)Copy the code

3.3 the else

While,for loops can optionally be accompanied by an else statement.

If the for,while statement is not terminated by a break statement, the else clause is executed, otherwise not.

The syntax is as follows

While condition expression: loop body else: statement blockCopy the code

Or:

For variable in iterable: loop body else: statement blockCopy the code

For I in range(4): I = input() if I == "Q": break print(I) else: for I in range(4): I = input() if I == "Q": break print(I) else: print("Over")Copy the code

3.4 pass

Python pass is an empty statement intended to preserve the integrity of the program structure.

Pass does nothing and is usually used as a placeholder statement.

For letter in 'JueJing': if letter == 'u': pass print(' this is pass block ') print(' current letter :', letter)Copy the code

3.5 Loop code optimization

Although computers are getting faster and more space, we still have to think about performance. When writing loops, following three principles can greatly improve performance and avoid unnecessary inefficient calculations

  1. Minimize unnecessary calculations inside the loop
  2. In the nested loop, the inner loop calculation should be reduced as far as possible, and the outer loop should be lifted as far as possible
  3. Local variable query is faster, so use local variables as much as possible
import time start = time.time() for i in range(1000): res = [] for j in range(10000): Res.append (I *1000+j*100) end = time.time() print(" time :{0}". Format (end-start)) start2 = time.time() for I in range(1000): Res = [] C = I *1000 for j in range(1000): res.append(C+j*100) end2 = time.time() print(" format :{0}".Copy the code

4. Comprehensive practice

Given an array of integers nums and a target value target, find the two integers in the array and return their array subscripts. You can assume that there is only one answer for each type of input. However, the same element in an array cannot be used twice.

Example: Given nums = [2, 7, 11, 15], target = 9

Nums [0] + nums[1] = 2 + 7 = 9

Answer:

Using the two-pointer concept, the brute method is the simplest, with two for loops nested

def twoSum_wayOne(nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        n=len(nums)
        for x in range(n):
            for y in range(x+1,n):
                if nums[x]+nums[y] == target:
                    return [x,y]
                    break
                else:
                    continue

Copy the code