Python ===if while for statement and a small web crawler instance

Source: Internet
Author: User

If branch statement

>>> count=89

>>> if count==89:

Print Count

#单分支

>>>

#coding: Utf-8

Count=int (raw_input (' Please enter a number '))

Print Count

If count>80:

print ' bigger than 80 '

Else

If count<80:

print ' smaller than 80 ' #多分支

======= Custom Function while if else===========

#coding: Utf-8

Count=int (raw_input (' Please enter a number '))

Print Count

If count>80:

print ' bigger than 80 '

Else

print ' smaller than 80 '

print ' End '

Sex=raw_input (' Please enter your gender ')

def inputsex (Sex):

while (sex!= ' Male ' and sex!= ' female '):

Print Sex

Sex=raw_input (' Please enter gender as male or female ')

If sex== ' Male ':

print ' gentleman! '

Else

If sex== ' female ':

print ' Lady '

Inputsex (Sex)

=====if else's relational expression bool judgment non 0 is true! =====

#coding: Utf-8

If True:

print ' 1True '

Else

print ' False '

If 0:

print ' 2True '

Else

print ' 2False '

If 1:

print ' 3True '

Else

print ' 3False '

If 298:

print ' 4True '

Else

print ' 4False '

If-2:

print ' 5True '

Else

print ' 5False '

1True

2False

3True

4True

5True

If

if (A and B):

if (A or B):

If not A:

=======================while Circulating Body ========================

======== crawler ======= of network Brush Browsing volume

#coding: Utf-8

Import time

Import WebBrowser

Import OS

Import Random

Count=random.ranint (2,8)

I=1

J=0

While J<count:

While i<=3:

Webbrowser.open_new_tab (' www.baidu.com ')

I=i+1

Time.sleep (3)

Else

Os.system (' taskkill/f/im iexplore.exe ')

J=j+1

Search the Internet when #windows下用taskkill.

#linux系统下用kill-pid or Killall Chrome

#ranint就是随机整数

=======for Loop Statement ============

For Val in Sth. Where Val does not have to be stated beforehand

For traversal string

#coding: Utf-8

s1= ' www.baidu.com '

I=0

For N in S1:

Print format (i, ' 2d '), n

I=i+1

Else

print ' Out for '

#format (i, ' 2d ') makes I a two output bit

For traversal list array

#coding: Utf-8

list1=[0,11,45, ' Dkfjah ', 12.5] list type

I=0

For Val in List1:

Print format (i, ' 2d '), Val

I=i+1

You can also write it directly

#coding: Utf-8

I=1

For Val in [11,23,0, ' dfadf ', ' Mandarin ', 12.45]:

Print format (i, ' 2d '), Val

I=i+1

Converts a string to a list list (str)

#coding: Utf-8

s1= ' www.baidu.com '

I=1

List1=list (S1)

Print List1

For Val in List1:

Print format (i, ' 2d '), Val

I=i+1

Tuple for traversal tuple tuple

#coding: Utf-8

#用圆括号括起来的是元组, the data in the tuple is read-only and cannot be modified.

tup= (1,2,3,4,5) tuple type

For T in Tup:

Print T

Else

print ' Out Tup '

For traversal file for Val in File.readlines ()

#coding: Utf-8

#如果所读的文件与此py程序文件所在地址一样, write the file name directly

For s in open (' 11.txt ', ' R '). ReadLine ():

Print S

Li3=open (' 11.txt ', ' R '). ReadLines ()

For a in open (' 11.txt ', ' R '). ReadLines ():

Open (' Tmp.txt ', ' A + '). Write (a) #a + is append write R Read W write w+ If no this file is first created and then written

Print a

Print Len (LI3)

How long is the #len (li3) output list here that's how many lines the article has

#readline () returns a string that returns the first row by default

#readlines () returns the list default to all rows in the file

#用help (file.readline) View Help

Python ===if while for statement and a small web crawler instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.