Python Basics of 2017.07.17 python web crawler 1

Source: Internet
Author: User
Tags strfind python web crawler

1.Python Variable Type:

(1) Number

Type int: A signed integer, which is the integer in the C language, that is, a whole number in mathematics, and its size is related to the amount of bits installed by the interpreter.

To view the maximum value of int under the current system:

Unlike the C language, Python does not need to declare a variable type when assigning a value to a variable, meaning that a number less than 2147483647 is assumed to be of type int when assigning a value to a variable, and is automatically a long type

In addition, octal digits, hexadecimal digits are of type int (Long).

Long type: integer type, integers greater than int are converted to long by default, in general, int is sufficient, as long as the memory is large enough, the long type has no limit size, and most rows are to be assigned.

Float type: Floating-point real numbers, that is, the number of decimal points in mathematics, not including infinite decimals, do not distinguish between precision, as long as the number with a decimal point can be regarded as floating-point data

Complex type: plural, a data type that needs to be customized in the C language, which is listed separately as the base data type in Python

Use a simple program to display the Python number type:

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

Class Shownumtype (object):
def __init__ (self):
Self.showint ()
Self.showlong ()
Self.showfloat ()
Self.showcomplex ()

def showint (self):
Print (U "######### #显示整型 ############")
Print (U "decimal integer")
Print ("%-20d,%-20d,%-20d"% ( -1000,0,1000))
Print (U-binary integer)
Print ("%-20s,%-20s,%-20s"% (Bin ( -1000), Bin (0), Bin (1000)))
Print (u "octal integer")
Print ("%-20s,%-20s,%-20s"% (Oct ( -1000), Oct (0), Oct (1000)))
Print (u "hexadecimal integer")
Print ("%-20s,%-20s,%-20s"% (Hex ( -1000), Hex (0), Hex (1000)))

def showlong (self):
Print (U "####### #显示长整型 ###########")
Print (U "decimal integer")
Print ("%-20ld,%-20ld,%-20ld"% ( -10000000000000000000,0,100000000000000000000000))
Print (u "octal integer")
Print ("%-20s,%-20s,%-20s"% (Oct ( -10000000000000000), Oct (0), Oct (1000000000000000)))
Print (u "hexadecimal integer")
Print ("%-20s,%-20s,%-20s"% (Hex ( -1000000000000000000), Hex (0), Hex (10000000000000)))

def showfloat (self):
Print (U "################ #显示浮点型 ##################")
Print ("%-20.10f,%-20.10f,%-20.10f"% ( -100.001,0,100.001))

def showcomplex (self):
Print (U "############ #显示复数型 ################")
Print (U "variable assignment complex var=3+4j")
Var=3+4j
The real part of print (U "var" is: The imaginary part of the%d\tvar is:%d "% (VAR.REAL,VAR.IMAG))


If __name__== ' __main__ ':
Shownum=shownumtype ()

2. A string is a contiguous set of characters that are defined between quotation marks and double quotes, which can be any visible character on the keyboard, or it can be invisible, such as a carriage return, "" tab, and so on.

The classic string manipulation methods are:

(1) Case Conversion of strings:

S.lower (): letter capitalization converted to lowercase

S.upper (): Lowercase letter converted to uppercase

S.swapcase (): letter capitalization convert lowercase, lowercase to uppercase

S.title (): Capitalize the first letter

(2) string search, replace:

S.find (Substr,[start,[end]): Returns the label of the first letter of the substr that appears in S, and returns -1,start and End If no substr in S is the equivalent of searching in s[start:end]

S.count (Substr,[start,[end]): Calculates the number of occurrences of substr in S

S.replace (Oldstr,newstr,[count]): Replace Oldstr in S with Newstr,count as the number of replacements

S.strip ([chars]): The left and right sides of the chars all the characters are removed, generally used to remove space

S.lstrip ([chars]): Remove all the characters from the left side of the s chars

S.rstrip ([chars]): Remove all characters from the right side of the s chars

(3) string segmentation, combination:

S.split ([Sep,[maxsplit]]): Use Sep as a delimiter, divide s into a list,maxsplit to represent the number of splits, the default delimiter is a blank character

S.join (seq): A sequence of strings represented by a seq---string sequence, connected by S.

(4) string encoding, decoding:

S.decode ([encoding]): Decoding encoding encoded s to Unicode encoding

S.encode ([encoding]): encoding s encoded in Unicode as encoding,encoding can be gb2312,gbk,big5 ....

(5) String test:

S.isalpha (): s are all letters, at least one character

S.isdigit (): s are all numbers, at least one character

S.isspace (): s are all whitespace characters, at least one character

S.islower (): Whether the letters in s are all lowercase

S.isupper (): Whether the letters in s are all uppercase

S.istitle (): s whether the first letter is capitalized

Write a showstroperation.py experiment to test:

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

Def strcase ():
"" "String Case Conversion" ""
Print "Demo string case Conversion"
Print "Demo string s assignment: ' This is PYTHON '"
S= ' This is a PYTHON '
Print "Uppercase converted to lowercase: \ts.lower () \t=%s"% (S.lower ())
Print "Lowercase converted to uppercase: \ts.upper () \t=%s"% (S.upper ())
Print Case conversion: \t\ts.swapcase () \t=%s "% (S.swapcase ())
Print "Capitalize: \t\ts.title () \t=%s"% (S.title ())
print ' \ n '

Def strfind ():
"" "String search, replace" ""
Print "Demo string search, replace, etc."
Print "Demo string s assignment: ' This is a PYTHON '"
S= ' This is a PYTHON '
Print "String search: \t\ts.find (' is ') \t=%s"% (S.find (' is '))
Print "String statistics: \t\ts.count (' s ') \t=%s"% (S.count (' s '))
Print "String substitution: \t\ts.replace (' is ', ' was ') =%s"% (S.replace (' is, ', ' is '))
Print "Go left and right space: \t\ts.strip () \t=#%s#"% (S.strip ())
Print "Go Left Space: \t\ts.lstrip () \t=#%s#"% (S.lstrip ())
Print "Go to right Space: \t\ts.rstrip () \t=#%s#"% (S.rstrip ())
print ' \ n '

Def strsplit ():
"" "String split, combination" ""
Print "Demo string split, combo"
Print "Demo string s assignment: ' This is a PYTHON '"
S= ' This is a PYTHON '
Print "string split: \t\ts.split () \t=%s"% (S.split ())
Print "string combination 1: ' # '. Join ([' This ', ' was ', ' a ', ' Python ']) \t=%s"% (' # '). Join ([' This ', ' is ', ' a ', ' Python '])
Print "string combination 2: ' $ '." Join ([' This ', ' was ', ' a ', ' Python ']) \t=%s "% (' $ '). Join ([' This ', ' is ', ' a ', ' Python '])
Print "string combination 3:". Join ([' This ', ' is ', ' a ', ' Python ']) \t=%s "% (' '. Join ([' This ', ' is ', ' a ', ' Python '])
print ' \ n '

Def strcode ():
"" "String Encoding, decoding" ""
Print "Demo string encoding, decoding"
Print "Demo string s assigned to: ' Encoding decoding test '"
S= ' coded decoding test '
Print "GBK encoded s \t=%s"% (s)
Print "GBK encoded s conversion to Unicode encoding"
Print "S.decode (' GBK ') =%s"% (S.decode ("GBK"))
Print "GBK encoded s converted to UTF8"
Print "S.decode (' GBK '). Encode (' UTF8 ') =%s"% (S.decode ("GBK"). Encode ("UTF8"))
Print "Note: either encoding or decoding is for Unicode character encoding, \ n so the source string must first be converted to Unicode encoding before encoding or decoding"
print ' \ n '

Def strtest ():
"" "String Test" ""
Print "Demo string test"
Print "Demo string s assigned to: ' ABCD '"
s1= ' ABCD '
Print "Test s.isalpha () =%s"% (S1.isalpha ())
Print "Test s.isdigit () =%s"% (S1.isdigit ())
Print "Test s.isspace () =%s"% (S1.isspace ())
Print "Test s.islower () =%s"% (S1.islower ())
Print "Test s.isupper () =%s"% (S1.isupper ())
Print "Test s.istitle () =%s"% (S1.istitle ())

If __name__== ' __main__ ':
Strcase ()
Strfind ()
Strsplit ()
Strcode ()
Strtest ()


Python Basics of 2017.07.17 python web crawler 1

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.