2017.07.22 Python web crawler's simple python script

Source: Internet
Author: User
Tags python script python web crawler

1.99 Multiplication Table:

#!usr/bin/env Python
#-*-Coding:utf-8-*-

Class PrintTable (object):
"" Print 99 multiplication Table "" "
def __init__ (self):
Print (u "Start printing 9x9 multiplication table")
Self.print99 ()

def print99 (self):
For I in Xrange (1,10):
For j in Xrange (1,i+1):
Print ('%d X%d =%2s '% (j,i,i*j))
Print (' \ n ')

if __name__ = = ' __main__ ':
Pt=printtable ()

Execution Result:

2. Fibonacci Series: Also known as the Golden Section series (Rabbit series): 0,1,1,2,3,5,8,13,21,34 ...

Recursive way definition: F (0) =0,f (1) =1,f (n) =f (n-1) +f (n-2)

The most orthodox method is recursive function, but Python, a unique data type-list, you can use the Append method to append data at the end of the list

#!usr/bin/env Python
#-*-Coding:utf-8-*-

Class Fibonacci (object):
"" Returns a Fibonacci sequence "" "
def __init__ (self):
self.flist=[0,1]
Self.main ()

def main (self):
Listlen=raw_input ("Please enter the length of the Fibonacci series (3-50):")
Self.checklen (Listlen)
While Len (self.flist) < int (Listlen):
Self.fList.append (Self.flist[-1]+self.flist[-2])
Print (' Get fibonaccii sequence: \ n%s '%self.flist)

def checklen (Self,lenth):
Lenlist=map (Str,xrange (3,51))
If lenth in Lenlist:
Print (U "input length conforms to standard, continue running")
Else
Print (U "can only enter 3-50, too long is not to be counted, just not necessary")
Exit ()

if __name__ = = ' __main__ ':
F=fibonacci ()

Execution Result:



3. Probability calculation: Randomly taking small ball problem
#!usr/bin/env Python
#-*-Coding:utf-8-*-

Import Random

Class Selectball (object):
def __init__ (self):
Self.run ()

def run (self):
While True:
Numstr=raw_input ("Number of input tests")
Try
Num=int (NUMSTR)
Except ValueError:
Print (U "required to enter an integer")
Continue
Else
Break
ball=[0,0,0,0,0,0,0,0,0,0]
For i in Xrange (num):
N=random.randint (1,10)
Ball[n-1]+=1
For I in Xrange (1,11):
Print (U "probability of getting the ball%d is%f"% (i,ball[i-1]*1.0/num))

if __name__ = = ' __main__ ':
Sb=selectball ()

Execution Result:

4. Read and write files: Read and write files are the most common I/O operations, the modern operating system does not allow the normal program to operate the disk directly, so, read-write file is to request the operating system to open a file object (often referred to as a file descriptor), and then, Read data from this file object via an interface provided by the operating system (read file), or write data to this file object (write file)

Python built-in open function to read and write files:

Mode: 7 modes can be used in combination

R: Open the file in read mode to read the file information

W: Open the file in writing, can write information to the file, such as the file exists, then empty the file, then write new content

A: Open the file in Append mode, if the file does not exist, create

r+: Open files in read and write mode, can read and write to the file operation

w+: Eliminate file content and open files as read and write

A +: Open the file as read-write and move the file pointer to the end of the file

B: Open the file in binary mode instead of text mode

Write operafile.py:

#!usr/bin/env Python
#-*-Coding:utf-8-*-

Import OS

Def operafile ():
Print (U "creates a file named Test.txt and writes Hello Python in it.")
Print (U "' first guaranteed Test.txt not present")
Os.system (' rm test.txt ')
Os.system (' ls-l test.txt ')
Print (U "now to create file and write content \ n")
Fp=open (' Test.txt ', ' W ')
Fp.write (' Hello Python ')
Fp.close ()
Print (U "Don't forget to close the file with close")
Print (U "to see if test.txt exists, and what the content is \ n")
Os.system (' ls-l test.txt ')
Os.system (' Cat test.txt ')
Print (' \ n ')

Print (U "How to avoid files where the open file fails?") ")
Print (U "to use with as")
With open (' Test.txt ', ' R ') as FP:
St=fp.read ()

Print (' test.txt content is:%s '%st)


if __name__ = = ' __main__ ':
Operafile ()





2017.07.22 Python web crawler's simple python script

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.