copy list and process control of the while loop

How to copy a list

1, the shallow copy: the first layer is the address list of the original full copy indiscriminately

Copy of the original list, generate a new list

 

 

 1.2 shallow copy method

list1 = [ 'Egon', 'lxx', [1,2]]

list2 = list1.copy()

2, deep copy

Wanted list two completely independent open and operating for change rather than read

 

 

 

 

 

 2.2 deep copy method

list1 = [ 'Egon', 'lxx', [1,2]]

 

import copy

list2 = copy.deepcopy(list1)

 

the difference:

When shallow copy is at the same address, it is to re-apply deep copy address, wherein a change of variable type, not both simultaneously changed

Process control loop while the two

1, the basic syntax and use cycle

while conditions:

  Code 1

  Code 2 

  Code 3

print()

 

 

 

 Such as:

count = 0

while count < 5:

  print(count)

  count += 1

print ( 'top-level code')

 

2, the cycle of death and efficiency

count = 0

while count < 5:

  print(count)

count = 0

while count < 5:

  print(count)

 

while True:

  name = input('yourr name')

  print(name)

All of the above does not occur efficiency

while True:

  1+1

No output has been running background

Pure calculation, there is no IO cycle of death can lead to deadly efficiency

3, cycle applications

= username 'abc' 
pwd = '123'

inp_name the INPUT = ( 'Please enter your account number:')
inp_pwd the INPUT = ( 'Please enter your password:')

IF username and pwd == == inp_name inp_pwd:
Print ( ' available for purchase ')
the else:
Print (' return Back ')

Requirements: input error, can then lose two times, not while you will need to re-enter the same procedure three times, very cumbersome, and would also like to lose but lost to the sides.

Repeat code, and the need for repeated lost


= username 'abc'
pwd = '123'

the while 1:
inp_name the INPUT = ( 'Please enter your account number:')
inp_pwd the INPUT = ( 'Please enter your password:')

IF username and pwd == == inp_name inp_pwd:
Print ( 'available for purchase')
the else:
Print ( 'return Back')

4, two ways to exit the loop

4.1, the conditions changed to False: does not take effect until the next cycle to determine when conditions

username = 'abc'
pwd = '123'
tag = True
Tag the while: 
  Tag = False

inp_name the INPUT = ( 'Please enter your account number:')
inp_pwd the INPUT = ( 'Please enter your password:')

IF username and pwd == == inp_name inp_pwd:
Print ( 'is available for purchase')
     # tag = False (after the code will be run, take effect until the determination condition for the next cycle)
the else:
Print ( 'return Back')

4.2 break, just run to break the cycle will immediately terminate this layer

username = 'abc'
pwd = '123'
True the while: 
inp_name the INPUT = ( 'Please enter your account number:')
inp_pwd the INPUT = ( 'Please enter your password:')

IF username and pwd == == inp_name inp_pwd:
Print ( 'is available for purchase')
     BREAK terminate this # layer cycle
the else:
Print ( 'return Back')
Print ( 'End ==== ====')

That is, if if the result is True, the ending will not be printed ==== end ====, if it is False, there will be

5, while the nested loop

5.1 tag type

tag = True

while tag:

  while tag:

    while tag:

       tag = False

 

username = 'abc'
pwd = '123'
tag = True
Tag the while: 
inp_name the INPUT = ( 'Please enter your account number:')
inp_pwd the INPUT = ( 'Please enter your password:')

IF username and pwd == == inp_name inp_pwd:
Print ( 'is available for purchase')
     the while Tag:
        cmd = input ( 'enter command:')
        IF cmd == 'Q':
          tag = False
        else
          print('order {x} is loading'.format(x = cmd))
    else:
print('return back')
print('====end====')

  

5.2 break: Each layer must be equipped with a break

while True:

  while True:

    while True:

    break

  break

break

username = 'abc'
pwd = '123'
True the while: 
inp_name the INPUT = ( 'Please enter your account number:')
inp_pwd the INPUT = ( 'Please enter your password:')

IF username and pwd == == inp_name inp_pwd:
Print ( 'is available for purchase')
     the while True:
        cmd = input ( 'enter command:')
          IF cmd == 'Q':
        print('order {x} is loading'.format(x = cmd))
        
          break

     terminate this layer break # cycle
the else:
Print ( 'return Back')
Print ( 'End ==== ====')

 

6, while + continue: the end of the cycle, directly into the next

Add similar code is meaningless after continue, because never run

 

 

count = 0

while count < 6:

  if count == 4:

    continue

  print(count)

  count +=1

7、while + else(针对break)

Code else will be included at the end of the while loop, and in the case while it does not like to be interrupted break the normal end, it will not run

while count < 6:

  if count == 4:

    break (behind the program is not running)

    count += 1

    continue

  print(count)

  count+=1

else:

  print('...')

Applications:


username = 'abc'
pwd = '123'

count = 0
tag = True
Tag the while: 
  IF COUNT == 3:
    BREAK
inp_name the INPUT = ( 'Please enter your account number:')
inp_pwd the INPUT = ( 'Please enter your password:')

IF username and pwd == == inp_name inp_pwd:
Print ( ' available for purchase ')
     the while Tag:
        cmd = iNPUT (' input commands: ')
        IF cmd ==' Q ':
          tag = False
        else
          print('order {x} is loading'.format(x = cmd))
    else:
print('return back')
    count += 1

 

 

Guess you like

Origin www.cnblogs.com/NevMore/p/12452872.html