Today this article is to share some of the cloud computing development tutorial, explaining that today: Python development actual combat operation and maintenance of automation process control.
Python by a conditional statement or statements of the execution result (True or False) to determine the execution code block.
Python programming language specify any non-empty and non-0 (null) is true, 0 is null or false.
if statement for executing the control program, substantially in the form of:
if the condition is determined:
执行语句……
else:
执行语句……
Wherein when the "determination condition" is satisfied (non-zero), the latter statement is executed, the content may be performed multiple lines, indented to distinguish the same range.
else statement is optional, when necessary conditions are not set up to perform content-related statements can be executed
Analyzing the condition of the if statement may be represented by the relationship>, <, ==,> =, <=.
When the determination condition is a plurality of values, the following form:
if Condition 1 is determined:
执行语句1……
elif judgment condition 2:
执行语句2……
elif judgment condition 3:
执行语句3……
else:
执行语句4……
Because the switch statement does not support python, so multiple conditional, can only be achieved with elif.
If the condition is determined to simultaneously requires a plurality of judgment, may be used or (or) representing the judgment condition is successful establishment of a two conditions;
Use and when (and), represents the only case where two conditions are satisfied, the judgment condition was successful.
#!/usr/bin/python
a = 9
if num >= 0 and num <= 10:
print 'hello'
a = 10
if num < 0 or num > 10:
print 'hello'
else:
print 'undefine'
a = 8
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):
print 'hello'
else:
print 'undefine'
Simple IF statement sets
you can use conditional statement if a position on the same line, the following examples:
was = 100
if (var == 100): print "variable var is 100"
print "Good bye!"
PYTHON loop
loop allows us to perform a statement or group of statements repeatedly
Python is provided for and while loops (not do..while loop in Python):
while loop
在给定的判断条件为 true 时执行循环体,否则退出循环体。
for loop
重复执行语句
Nested loop
你可以在while循环体中嵌套for循环
Loop control statements
Loop control statements can change the order of statement execution. Python loop control supports the following statement:
break statement
在语句块执行过程中终止循环,并且跳出整个循环
continue Statement
在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环。
The pass statement
pass是空语句,是为了保持程序结构的完整性。
Python While Loops
The basic form:
while judgment conditions:
执行语句……
Execute statement may be a single statement or a block. Determination condition can be any expression, in any non-zero value, or a non-empty (null) are true.
When the determination condition false false, the cycle ends.
Example:
#!/usr/bin/python
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
print "Good bye!"
for skipping the cycles continue, break is used to exit the loop, in addition to "determination condition" may also be a constant value, indicating the cycle must be established, are used as follows:
continue and break usage
i = 1
while i < 10:
i += 1
if i%2 > 0: # 非双数时跳过输出
continue
print i # 输出双数2、4、6、8、10
i = 1
while 1: # 1 cycling conditions must be established
print i # 输出1~10
i += 1
if i > 10: # 当i大于10时跳出循环
break
Infinite loop
If the conditional statement is always true, infinite loop will execute it, the following examples:
#!/usr/bin/python
-- coding: UTF-8 --
was = 1
while var == 1: # This condition is always true, an infinite loop will execute down
num = raw_input("Enter a number :")
print "You entered: ", num
print "Good bye!"
Recycled else statement
In the python loop in, else statements will be finished in normal execution cycle, the next cycle of the case that is not interrupted by the break out of the
#!/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"
While simple statement group
The syntax is similar to the if statement, the while loop if you only one statement, you can write the statement and while on the same line, as follows:
flag = 1
while (flag): print 'Given flag is really true!'
Python For loop
Python for loop can iterate any sequence of items, such as a list or a string.
for loop syntax is as follows:
for iterating_var in sequence:
statements(s)
Example:
#!/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
Examples of the above output:
Current letter: P
Current letters: y
Current letter: t
Current letter: h
Current letter: o
Current letters: n
Current letter: banana
Current letter: apple
Current letter: mango
By iterative sequence index
Further traversing the way through the execution cycle index, the following examples:
#!/usr/bin/python
-- coding: UTF-8 --
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print 'Current fruit:', fruits [index]
Examples of the above output:
Current fruit: banana
Current fruit: apple
Current fruit: mango
The above examples we use the built-len () function and Range (), len () function returns the length of the list, i.e. the number of elements. range returns a sequence number.
Recycled else statement
Following examples:
#!/usr/bin/python
-- coding: UTF-8 --
for num in range (10,20): # iteration number between 10-20
for i in range(2,num): # 根据因子迭代
if num%i == 0: # 确定第一个因子
j=num/i # 计算第二个因子
print '%d 等于 %d * %d' % (num,i,j)
break # 跳出当前循环
else: # 循环的 else 部分
print num, '是一个质数'
Examples of the above output:
10 等于 2 * 5
11 是一个质数
12 等于 2 * 6
13 是一个质数
14 等于 2 * 7
15 等于 3 * 5
16 等于 2 * 8
17 是一个质数
18 等于 2 * 9
19 是一个质数
Python loop nest
Python language allows a circulation loop embedded inside another.
Python For nested loop syntax:
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
Python While nested loop syntax:
while expression:
while expression:
statement(s)
statement(s)
Continue and Break statement:
continue statement to jump out of this cycle, and break out of the cycle.
continue statement is used to tell Python to skip the remaining statements in the current cycle, and then proceed with the next cycle.
PYTHON PASS statement
Python pass is an empty statement, in order to maintain the integrity of the program structure.
pass without doing anything, generally used as a placeholder statement.
The pass statement in Python syntax is as follows:
pass
Example:
#!/usr/bin/python
-- coding: UTF-8 --
Each letter of the output Python
for letter in 'Python':
if letter == 'h':
pass
print '这是 pass 块'
print 'current letter:', letter
print "Good bye!"
The results of the above examples:
Current letter: P
Current letters: y
Current letter: t
This is a pass block
Current letter: h
Current letter: o
Current letters: n
Good bye!