Python: process control and built-in methods

Posted by melittle on Tue, 08 Mar 2022 11:16:59 +0100

Python: process control and built-in methods

while+else of process control

Yesterday, we talked about adding the keyword "break" to end the while loop. The usage of while+else is that when the while loop ends normally without using the keyword "break", else code can be executed. Take a look at the code structure:

'''
while condition:
    Loop body code
else:
    while Code executed at the normal end of the loop  
'''

We can look at a piece of code and understand it more intuitively:

count = 0
while count < 10:
    print(count)
    count += 1
    # break  # If you add a break here, the following code will not be executed
else:
    print('Code can be executed after normal completion')

while nesting of process control

While nesting literally means that a while loop is nested inside a while loop. Use code to demonstrate:

count = 0
is_teg = True
while is_teg:
    while count < 3:
        username = input('Enter your user name>>:')
        password = int(input('Enter your password>>:'))
        if username == 'oscar' and password == 123:
            print('Login successful')
            break
        else:
            print('Login failed')
            count += 1
            if count == 3:
                again = input('You have run out of times. Do you want to continue(y/n)>>:')
                if again == 'y':
                    count = 0
                else:
                    is_teg = False

Dead cycle

We also want to talk about the dead cycle. The dead cycle is a cycle that cannot be ended all the time. It always occupies the CPU, which will cause the computer to jam and seriously damage the hardware. Look at a simple dead loop code:

# This is the simplest piece of dead loop code. It prints 1 all the time and won't end
while True:
    print(1)

for loop of process control

What the for loop can do is that the while loop can do it. Why learn the for loop? That's because the for loop is more concise and has a higher utilization rate. Let's take a look at its syntax structure:

'''
for Variable name in for Cyclic image:  # Dictionary, string, tuple, set, list
    for Loop body code of the loop
'''
# Each cycle will for The element in the loop object is assigned to the variable name

In general, we use the for loop for values and rarely use the while loop. Let's take an example:

# take out name Elements in
name = ['oscar', 'jason', 'tony']
# while loop
count = 0
while count < 3:
    print(name[count])
    count += 1

# for loop
for i in name:
    print(i)

We can see that the while loop needs four lines of code to complete, and the for loop needs two lines. Moreover, while has some disadvantages. Once the number of "name" elements is relatively large, the disadvantages of the while loop will be more obvious.

for loop string type, print all letters of string

# for Loop string, print all letters
count = 'oscar'
for i in count:
    print(i)

for loop dictionary

 

 

 

for loop set

 

 

 

range keyword

If we want to print 0-100, we can use the while loop and the for loop to look at it first:

# Print 1-100
#while loop
count = 0
while count < 101:
    print(count)
    count += 1

# for loop
for i in range(101):
    print(i)

It can be seen that the for loop is very concise, but it also leads to a new knowledge point "range" keyword. "Range" is an iterator used to generate data sets, but saves memory space. Its value method is "regardless of the head and tail". "Range" is different in python2 and python3:

range in python2 directly generates a list, which occupies a large memory space.

 

 

But "xrange" in python2 is the same as "range" in python3.

 

 

"range" in Python 3 is to generate a data set, which takes up less memory space.

 

 

for+break loop

break ends the cycle of this layer.

# for+break
for i in range(10):
    print(i)
    break  # No, break Print 0 when-9,Add break Encountered after printing 0 break End directly for loop

for+continue loop

continue ends this cycle.

# continue+for
for i in range(10):
    if i == 4:
        continue  # When i Equal to 4 continue End directly and start the next cycle
    print(i)

for+else loop

Execute the else statement after the normal end of the for loop.

# for+else loop
for i in range(10):
    print(i)
else:
    print('It can be printed after normal completion')

Built in method: integer (int)

The built-in methods of integer include type conversion and hexadecimal number conversion.

Type conversion (only strings with pure numbers and no decimal point can be converted):

 

 

Hexadecimal conversion:

1. Convert decimal to other decimal:

print(bin(100))  # 0b1100100 Binary (0) b)
print(oct(100))  # 0o144 Octal (0) o)
print(hex(100))  # 0x64 Hex (0) x)

2. Convert other base numbers to decimal:

print(int('0b1100100', 2))
print(int('0o144', 8))
print(int('0x64', 16))

Built in method: float

Type conversion (only pure numeric strings can be converted):

 

 

Built in method: string (str)

1. Type conversion

String can convert all basic data types.

2. Index value

You can take values according to the subscript index as in the list.

name = 'oscar'
print(name[1])

3. Slice

You can take the specified value.

# take hello
name = 'oscar hello'
print(name[6:11])  # Take care of your head and tail

4. Step size

Take a value for the number of steps between.

5. Count the number of characters in the string

# Count the number of strings
name = 'oscar hello'
print(len(name))

6. Member calculation

Judge whether a string is in a string.

7. Remove the specified character at the beginning of the string

8. Cut the string according to the specified character

URL = '127.0.0.1'
print(URL.split('.'))  # Cut according to period
print(URL.split('.', 2))  # You can specify the number of cuts

 

 

This is IT Xiaobai lulufi, welcome your advice!!!