Switch to Python Full Stack Lessons (Note 3)

Say nothing but applause!

Applause is the feeling in my heart that blogging every day is really awesome

Luckily I don't have any fans, no one rushes, just lazy! Hahaha, write the third article

----------------------------------------------------------------------------------------------------------------------

Chapter V

The main point is cycle (I find this chapter interesting personally)

  • Built-in function range
    • range(stop), for example range(10), is 1-9, run once, step 1
    • range(start,stop), for example range(2,8), is 2-7, run once, step 1
    • range(start,stop,step), for example range(3,10,2), is 3-9, run once, step interval 2. 3.5.7.9 This
    • The return value is an iterator object

LookThis is absolute! Easy to understand

    • #Three ways to create range()
      '''First, there is only one parameter (only one number is given in parentheses)'''
      r=range(10)   #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], default start at 0, default difference of 1 is called step
      print(r)  #range(0, 10)
      print(list(r)) #Integer sequence used to view range objects -->list means list
       
      '''The second is created by giving two parameters (two numbers in parentheses)'''
      r=range(1,10)  #A starting value is specified, starting at 1 and ending at 10 (excluding 10), with a default step of 1
      print(list(r)) #[1, 2, 3, 4, 5, 6, 7, 8, 9]
       
      '''The third way is to give three parameters (three numbers in parentheses)'''
      r=range(1,10,2)
      print(list(r)) #[1, 3, 5, 7, 9]
       
      '''Determines whether the specified integer exists in the sequence in ,not in'''
      print(10 in r) #False, 10 is not in the current r integer sequence
      print(9 in r) #True,9 is in the relative r sequence

The loop structure while and for-in

  • Difference between if of selection structure and while of loop structure
    • if is judgement once, condition is True to execute a line
    • while is the judgment N+1, if True executes N times (the last N+1 is exit)

while loop:

for-in loop:

  • The in expression takes values from (strings, sequences, etc.) in turn, also known as traversal
  • Objects traversed by for-in must be iterative
  • for custom variable in iterative object:
    • Circulatory body
  • Custom variables do not need to be accessed within a loop, they can be replaced with underscores.

break statement

  • Used to end a loop structure, usually with branch structure if

continue Statement

  • Used to end the current loop and move to the next one, usually with if in a branch structure

Watch the horseEveryone

for i in range(5):  #Represents an outer loop to be executed five times
    for j in range(1,11):   #Do 1 to 10
        if j%2==0:
            #break
            continue        #Continue when j is divided by the remainder of 2
        print(j,end='\t')   #end=no line break'\t'is the tab key
    print()

else statement

Drive~

'Like the previous questions, it's easy to do this function'
for _ in range(3):
    mima=input('Please input a password')   #input outputs a string, followed by a string
    if mima=='123':
        print('Password is correct')
        break                 #End
    else:
        print('Incorrect password')    #The first pass is 0. The second pass is 1. The third pass is 2
else:
    print('Sorry, the password was entered incorrectly three times')

Do it!

'Topic 1'

for it in range(97,123):
    print(ord(it))
    

'Question 2'
name=input('enter one user name')
pwd=int(input('Please input a password'))
n=3
for _ in range(2):
    if name=='admin' and pwd==8888:
        print('Landing Success')
        break
    else:
        n-=1
        print('Username or password incorrect\n You still have'+str(n)+'Second chance!!')
        name=input('enter one user name')
        pwd=int(input('Please input a password'))
else:
    print('Sorry, three typing errors')

Chapter VI

list

Lists need brackets [], with elements separated by English commas

List built-in function list()

  • lst=list(['Saint','Sauvignon'])

Features of lists

  • The elements of the list are arranged in order
  • Index unique data
  • Can store duplicate data
  • Any data type can exist
  • Dynamically allocate and reclaim memory as needed

Notice in the figure below that the number starts at 0 and the negative side starts at -1

List Query

  • index() Gets the specified element index
    • If the same element exists, only the first index is returned
    • If this element is not present, a ValueError will appear
    • You can also check between points
  • Get Elements
    • Forward index is from 0 to N-1, which is followed by a number minus one
    • Reverse index is full-N to-1
    • Throw IndexError if the specified index does not exist
  • Get multiple elements: list name [strat:stop:step]
    • Section
      • The result is a copy of the original fragment
      • Scope in [strat,stop)
      • The default step number is 1, which can be abbreviated [start:stop]
      • When step is positive
        • [: stop:step], default from the first one after going
        • [start::step], default from go-back to end-last
      • When step is negative
        • [: stop:step], default from the first forward
        • [start::step], default from back to front to last

Traverse through list elements:

  • for iteration variable in list name

List Element Increase

  • append(), add an element to the end of the list
  • extend(), add at least one element to the end of the list
  • insert(), which adds an element anywhere in the list
  • Slice, adding at least one element anywhere in the list

List element deletion

  • remove()
    • Delete one element at a time
    • Duplicate elements delete only the first
    • Element does not exist throw ValueError
  • pop()
    • Delete an element at a specified index location
    • Specified index does not exist throwing IndexError
    • Delete the last element in the list without specifying an index
  • Section
    • Delete at least one element at a time
  • clear(), empty list
  • del, delete list

sort list

  • With sort(), the default elements are sorted from smallest to largest (reverse=False) in ascending order and sort(reverse=Ture) in descending order

List Generation

  • [i*i for i in range(1,10)]

Brain burning link, done a problem

 

Keywords: Python Back-end

Added by lisnaskea on Tue, 04 Jan 2022 14:02:12 +0200