Loop nesting (99 multiplication table) + continue and break statements + list (basic concept of list) 2020-11-6

1. Loop nesting

1.1 Print 5 rows and 5 columns of small stars in a loop

i = 0
while i < 5:
    print('*****')
    i+=1

result

*****
*****
*****
*****
*****

1.2 Print 5 rows and 5 columns of small stars with a nested loop

i=0
while i <5:
    j=0
    while j<5:
        print('*',end='')
        j+=1
    print('')  
    i+=1

result

*****
*****
*****
*****
*****

1.3 Print 5 lines of incremental stars with nested loops

i=0
while i<5:
    j=0
    while j<i+1: 
        print('*', end='')
        j += 1
    print()
    i+=1

result

*
**
***
****
*****

1.4 Print 5 lines with nested loops to decrease the stars

i=0 
while i<5:
    j=0
    while j < 5-i:
        print('*',end='')
        j+=1
    print()   
    i+=1

result

*****
****
***
**
*
  • Summary: The characteristics of nested loops, the outer loop can control the height of the graph, and the inner loop can control the width of the graph.

1.5 Print 99 multiplication table with nested loop

i = 0 
while i < 9:
    i += 1
    j =0
    while j < i :
        j += 1
        print(f'{i}×{j} = {i*j} \t',end='')  # \t是制表符,前面加个空格更好
    print()

Insert picture description here

2. continue 和 break

2.1 continue

Continue means "continue". This clause is also used inside the loop. When a certain condition is met, the continue statement is triggered, the following code will be skipped, and the loop will be returned directly to the beginning.
example:

i=0
while i <6:
    i+= 1
    if i == 2:
        continue
    print(i)
else:
    print('hello')

Results of the
Insert picture description here

2.2 break

Let's take a look at the break statement first. Break means "break" and is used to end the loop, usually written as if...break. It is written like this:

while 条件:
    。。。
    if 条件:
        break

Here, if...break means that if a certain condition is met, the loop ends early. Remember, this can only be used inside the loop. Jump out of the loop statement immediately, including the else statement. The else statement will only be executed when the while loop ends normally. The break force ends the loop, and the Gu else statement will not be executed.
example:

i=0
while i <6:
    i+= 1
    if i == 2:
        break
    print(i)
else:
    print('hello')

Results of the
Insert picture description here

3. List

3.1 Sequence

  • A basic data structure in Python
  • Data structure refers to the way data is stored in the computer
  • Sequence is divided into variable sequence and immutable sequence

For example, the string'python' belongs to an immutable sequence, and the position of elements in the sequence cannot be changed.
List (list) is a variable sequence, the position of the elements in the list can be changed.

3.2 List

Format: list=[]

  • Any object can be saved, such as list = [1,'python', none, True, [1,2,3], a] Variables must be assigned first.
  • The elements in the list are marked with "0, 1, 2, 3, 4..." from left to right, and they are called index values.
  • print(len(list)) can get the length of the list, that is, the number of elements in the list.
  • If there are 5 elements in the list, print(list(6)) will report an error, which is out of range.
  • How to take out elements: print the corresponding index value print(l list name [index value]), if you want to take out 7, print in lst=[1,2,3,4,5,6,7,8,9] (Lst[6]).
  • If the number of marks is negative, such as print(lst=[-2]), it is taken from the right, and -2 represents the second to last one, which is 8.
3.2.1 Slicing

Get a
sublist from the existing list heros = ['Iron Man','Hulk','Spiderman','Pigman','Ant-Man','America's Long']

Slice syntax:
list name [start position: end position]

  • Take a section
    Example:
    Insert picture description here
    Explain that the slice includes the element to the left of the colon, but does not include the element to the right.
  • Take the left side [:index value]
    Insert picture description here
  • Take the right [index value:]
    Insert picture description here
  • Take all: [:]
    Insert picture description here
3.2.2 Step size

Represents the interval of each retrieval element, the default is 1, which can be omitted.
Syntax format: [slice start position: slice end position: step length]
Example:
print(heros[2:3:2])
Insert picture description here

  • Step test point: [::-1] can be used to retrieve elements in reverse order, which is equivalent to .reverse() to explain the principle.
  • Principle, the front colon is the slice knowledge, take all. The colon after the step is -1, and the value is taken from right to left when the step is negative. example.
    Print(heros[::-1])
    Insert picture description here
    can see that the elements in the list are all reversed.
    Equivalent to heros.revers()
    Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46738467/article/details/109529389