Theme II. Basics of conditional statements and looping statements

The main contents include:

1.条件语句:包括单分支、双分支和多分支语句,if-elif-else
2.循环语句:while的使用及简单网络刷博器爬虫
3.循环语句:for的使用及遍历列表、元组、文件和字符串

Introduction: statement block

很多语言特殊单词或字符(如begin或{)来表示一个语句块的开始,用另外的单词或字符(如end或})来表示语句块的结束.
而在Python中使用冒号(:)来标识语句块的开始,块中每一个语句都是缩进的(缩进量相同).当回退到和已经闭合的块一样的缩进量时,就表示当前块已经结束.

A. Conditional statement if

条件判断通常有布尔表达式(True|False 0-假|1-真 非0即真)、关系表达式(>= <= == !=)和逻辑运算表达式(and or not).

1. Single branch statement

2. Double branch statement

3. Multi-branch statement, if-elif-else

II. Loop while

判断条件语句condition可以为布尔表达式、关系表达式和逻辑表达式,else可以省略(此处列出为与C语言等区别).

while-else

Example: Open a new window by calling the webbrowser browser open_new_tab
Import webbrowser AS Web
web.open_new_tab ( ' http://andy111.blog.sohu.com/46684846.html ')
os.system ( 'taskkill / F / IM iexplore . .exe ') - could be forced to shut down the application, where / F represents the forced termination of the program, / IM represents the image in the program's main role is to clear the memory, prevent memory consumption is too big crash phenomenon; but need to call import os the system () function to open, and Linux using the kill command (kill -pid or killall) to terminate the process.

III. Loop for

Sequences in target for:
target represents a variable name, sequences indicate sequence, common types of list (list), tuple (tuple), strings (strings) and files (files).
Note: Python's for not reflect the number of cycles whether, in target refers to each value in the sequence of data items from the inside of the sequences, i.e. the end to take complete, taken many times how many cycles, where in is the membership operator, a check value may also be used in the sequence break and continue out of the loop.

1. string loop

2. The list of circulation, range

3. tuple cycle

Tuple of tuples each data item can not be modified, read only, and the sequence list [1,2,3,4] can be modified

4. File cycle

in R & lt Open for ( 'test.txt', 'R & lt') the readlines ():.
. Open ( 'test.txt', '+ A') Write (C)
. Open (File.read) Returns a string first a read () output: each inter-character spaces have
open (file.readlines) returns a list of n rows corresponding to reading by n times readline composition, constituting a list of strings read out..
Open (FILE.readline) from reading a file line.

Guess you like

Origin www.cnblogs.com/chenhuan123/p/12036636.html