Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the process control in the development of Python automatic operation and maintenance

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, Xiaobian will bring you about how the process control in Python automation operation and maintenance development is. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.

Python automated operation and maintenance development actual combat process control.

Python conditional statements are blocks of code whose execution is determined by the result (True or False) of one or more statements.

The Python programming language specifies that any non-zero and non-null values are true, and 0 or null are false.

The if statement is used to control the execution of the program. The basic form is:

If judgment conditions:

Execution of statements...

else:

Execution of statements...

When the "judgment condition" is true (non-zero), the following statement is executed, and the execution content can be multiple lines, indented to distinguish the same range.

else is an optional statement. When it is necessary to execute the content when the condition is not true, the relevant statement can be executed.

If statement can be judged by>,=,= 0 and num 10:

print 'hello'

else:

print 'undefine'

num = 8

if (num >= 0 and num = 10 and num 0: #skip output if non-even

continue

print i #Output even numbers 2, 4, 6, 8, 10

i = 1

while 1: #loop condition 1 must be true

print i #Output 1~10

i += 1

if i > 10: #Jump out of loop when i is greater than 10

break

infinite loop

If the conditional statement is always true, the loop will continue indefinitely, as in the following example:

#!/ usr/bin/python

# -*- coding: UTF-8 -*-

var = 1

while var == 1 : #This condition is always true and the loop will continue indefinitely

num = raw_input("Enter a number :")

print "You entered: ", num

print "Good bye! "

Loop through else statements

In python loops, statements in else are executed after the loop finishes normally, i.e., the loop is not broken by breaking out.

#!/ usr/bin/python

count = 0

while count < 5:

print count, " is less than 5"

count = count + 1

else:

print count, " is not less than 5"

Simple while statement group

Similar to the syntax of the if statement, if you have only one statement in the body of your while loop, you can write that statement on the same line as while, as follows:

flag = 1

while (flag): print 'Given flag is really true! '

Python For Loop Statement

Python for loops can traverse any sequence of items, such as a list or a string.

The syntax for a for loop is as follows:

for iterating_var in sequence:

statements(s)

Examples:

#!/ usr/bin/python

# -*- coding: UTF-8 -*-

for letter in 'Python':

print 'current letter:', letter

fruits = ['banana', 'apple', 'mango']

for fruit in fruits:

print 'current letter:', fruit

The output of the above example:

Current letter: P

Current letter: y

Current letter: t

Current letter: h

Current letter: o

Current letter: n

Current letter: banana

Current letter: apple

Current letter: mango

Iteration through sequence indexing

Another way to perform a loop traversal is by indexing, as shown in the following example:

#!/ usr/bin/python

# -*- coding: UTF-8 -*-

fruits = ['banana', 'apple', 'mango']

for index in range(len(fruits)):

print 'current fruit:', fruits[index]

The output of the above example:

Current fruit: banana

Current fruit: apple

Current fruit: mango

In the above example we use the built-in len() and range() functions, len() returns the length of the list, i.e. the number of elements. range Returns the number of a sequence.

Loop through else statements

Examples include:

#!/ usr/bin/python

# -*- coding: UTF-8 -*-

for num in range(10,20): #Iterate numbers between 10 and 20

for i in range(2,num): #Iterate by factor

if num%i == 0: #Determine the first factor

j=num/i #Calculate the second factor

print '%d equals %d * %d' % (num,i,j)

break #Jump out of the current loop

else: #else part of the loop

print num, 'is a prime number'

The output of the above example:

Ten equals two times five.

Eleven is a prime number.

Twelve equals two times six.

Thirteen is a prime number.

Fourteen equals two times seven.

Fifteen equals three times five.

Sixteen equals two times eight.

Seventeen is a prime number.

Eighteen equals two times nine.

19 is a prime number.

Python loop nesting

Python allows you to embed a loop inside the body of another loop.

Python For Loop Nested Syntax:

for iterating_var in sequence:

for iterating_var in sequence:

statements(s)

statements(s)

Python While loop nesting syntax:

while expression:

while expression:

statement(s)

statement(s)

Continue and Break statements:

The continue statement jumps out of the loop, and break jumps out of the loop.

The continue statement tells Python to skip the rest of the current loop and continue on to the next loop.

PYTHON PASS statement

Python pass is an empty statement to preserve the integrity of the program structure.

Pass doesn't do anything. It's usually used as a placeholder.

Python pass statement syntax format is as follows:

pass

Examples:

#!/ usr/bin/python

# -*- coding: UTF-8 -*-

#Output each letter of Python

for letter in 'Python':

if letter == 'h':

pass

print 'This is a pass block'

print 'current letter:', letter

print "Good bye! "

Results of the implementation of the above examples:

Current letter: P

Current letter: y

Current letter: t

This is the pass block.

Current letter: h

Current letter: o

Current letter: n

The above is how the process control in Python automation operation and maintenance development shared by Xiaobian is. If there is a similar doubt, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report