Learning python, from getting started to giving up

Learn python, from getting started to giving up (6)

Yesterday's homework

Yesterday, I left a code assignment of guessing age to consolidate the previous knowledge. Today, I gave the standard answer.

I also gave my answer.

In contrast, the teacher's code is concise and clear, and the goal is achieved with a little less code. Although my code is a little lengthy and has repeated code blocks, it adds age random numbers, and considers inputting other contents when inputting y/n.

Cyclic structure supplement

  • while + else

    While is a commonly used loop body code, while else is used to execute after the loop body code is normally terminated when while is not terminated in advance by the keyword break.

    You can see that the loop break s when count = 4, so the characters in the else statement are not output.

    It can be seen here that the condition starts directly from 5 and does not jump out of the loop at 4 o'clock. The while loop ends normally, so the characters in else are printed normally.

  • Dead cycle

    Dead cycle is a cycle that can never be ended. It will always occupy CPU resources, extremely affect the performance of the computer, and even cause hardware damage.

  • while nesting

    Nested loops are nested when one or more loops are nested in a loop.

    flag = True
        while flag:
            username = input('username>>>:')
            password = input('password>>>:')
            if username == 'jason' and password == '123':
                print('Login successful')
                while flag:
                    cmd = input('Please enter your instructions>>>:')
                    if cmd == 'q':
                        print('Next time')
                        flag = False
                    print('Your instructions are being executed:%s' % cmd)
            else:
                print("Wrong user name or password")
    
  • for loop

    What the for loop can do, although the while loop can also do, and the usage is consistent with the while loop (break, continue, else). However, the for loop is simpler and more efficient to use, so it is used more frequently.

    When designing and getting the value of the loop, it is generally preferred to use the for loop rather than the while loop.

    name_list = ['jason', 'kevin', 'tony', 'tank', 'oscar']
    # Use the while loop to print out all the elements in the list in turn
    count = 0
    while count < 5:
        print(name_list[count])
        count += 1
        
    # Use the for loop to print out all the elements in the list in turn
    for name in name_list:
        print(name)
    '''
    jason
    kevin
    tony
    tank
    oscar
    '''
    # Circular string: take out each character in turn
    for i in 'hello world':
        print(i)
    '''
    h
    e
    l
    l
    o
     
    w
    o
    r
    l
    d
    '''
    # Circular Dictionary (special): Circular dictionary can only get the key value of the dictionary, but not directly
    userinfo_dict = {'username': "jason", 'age': 18, 'gender': 'male'}
    for i in userinfo_dict:
        print(i)
    '''
    username
    age
    gender
    '''
    # Cyclic tuple
    for i in (11, 22, 33, 44, 55):
        print(i)
    '''
    11
    22
    33
    44
    55
    '''
    # Circular collection: the elements inside the dictionary and collection are out of order 
    for i in {11, 22, 33, 44, 55, 66}:
        print(i)
    '''
    33
    66
    11
    44
    22
    55
    '''
    
  • range() function

    The range() function returns a sequence of numbers, starting from 0 by default, increasing by 1 by default, and ending with the specified number.

    The range() function is different between python2 and python3. In python2, the range() function directly generates a list, which will occupy more memory when there are many elements. In python3, the list is represented by a range(a, b), which takes less space.

    But there is an xrange() in python2 that has the same function as range() in python3.

    # Usage: 1 write only one number in parentheses. By default, it starts from 0 and ignores the beginning and end
    for i in range(5):
        print(i)
    '''
    0
    1
    2
    3
    4
    '''
    # Usage 2 write two numbers in parentheses to customize the starting position, regardless of the beginning and end
    for i in range(1, 5):
        print(i)
    '''
    1
    2
    3
    4
    '''
    # Usage 3 write three numbers in parentheses. The third number represents the difference of the arithmetic sequence. By default, it is 1
    for i in range(1, 10, 2):
        print(i)
    '''
    1
    3
    5
    7
    9
    '''
    

Built in method of data type

  • int integer

    1. Type conversion

      Cast the string in parentheses () to an int.

      It should be noted that int() can only convert a pure numeric string, that is, if the string is a decimal, English letter or symbol, it cannot be converted.

    2. Binary conversion

      bin() can convert decimal to binary

      oct() can convert decimal to octal

      hex() can convert decimal to hexadecimal

      int() can also convert it back to decimal.

  • Float float

    The type conversion of floating-point type is similar to that of integer type, but it can convert integer and floating-point strings, but it still cannot convert English letters or other types of strings.

String built-in method

  1. Type conversion

    str() can convert all basic data types.

  2. Indexing and slicing

    The index value of the string is similar to that of the list. You can take out the string that refers to the second location in the string. Like the list, the first place starts with 0.

    Slice is the advanced level of index. It can start from the pointing position, end at the specified position, and take out the characters in the string in turn. Similar to range(), it ignores the head and tail.

    You can also add a step in the slice to make its interval value. If you enter 2, it means every other value. The default value is 1.

    Indexes and slices can also be taken upside down. As long as the - sign is added to the parameter, the value can be taken in the opposite direction to the default order.

  3. Number of characters

    When calculating the number of characters in a string, you can use the len() function. (space is also a character)

  4. Member operation

    You can use the member operation in to determine whether the specified character or string exists in the string.

  5. Removes the specified characters at the beginning and end of a string

    The. strip() function is used to remove the specified characters at the beginning and end of the string. When the parenthesis is empty, the space is removed by default.

  6. Cut the string according to the specified character

    The. split() function cuts the string from left to right according to the specified character. After cutting, it can be stored in a list or decompressed and assigned. Can be used rsplit() cuts from right to left.

summary

Today's content is basically new. It's a little hard at first, and there are more and more knowledge points. Because I can't remember the function name, I still need to use it more in the future.

Added by saad|_d3vil on Tue, 08 Mar 2022 10:44:11 +0200