(0) Folder
High-speed Python and error-prone (text processing)
Python Text processing and JAVA/C
10 minutes Learn the basic types of Python
High Speed Learning Python (actual combat)
The way to Big data processing (10 minutes to learn Python)
One: Python Brief introduction
(1) The origin of Python
Python (English pronunciation:/?pa?θ?n/), is an object-oriented, interpreted computer programming language, invented by Guido van Rossum at the end of 1989, the first public release issued in 1991
Years. The python syntax is concise and clear, with a rich and powerful class library. It is often nicknamed the Glue language, which makes it easy to connect a variety of modules (especially the C + +) made in other languages
together. A common application scenario is to use the Python high-speed build program's prototype (sometimes even the program's last interface). And then rewrite it in a more appropriate language for the parts that have special requirements.
Performance requirements are particularly high compared to graphics rendering modules in 3D games . Can be rewritten in C + + .
(2) Python syntax simple introduction----type conversion
int (x [, Base]) converts x to an integer
Long (x [, Base]) converts x to a long integer
Float (x) converts x to a floating-point number
Complex (real [, Imag]) creates a complex number
STR (x) converts an object x to a string
REPR (x) converts an object x to an expression string
Eval (str) is used to calculate a valid Python expression in a string and returns an object
Tuple (s) converts a sequence s to a tuple
List (s) converts the sequence s to a list
Chr (x) converts an integer to one character
UNICHR (x) converts an integer to a Unicode character
Ord (x) converts a character to its integer value
Hex (x) converts an integer to a hexadecimal string
Oct (x)Converts an integer to an octal string
(3)Python Syntax Simple introduction----type conversion
s + R sequence connection
S * N, n * s S n times copy, n is integer
S% d string formatting (string only)
S[i] Index
S[I:J] Slices
X in S, X isn't in S dependency
For x in S: Iteration
Len (s) length
Min (s) min element
Max (s) max element
S[i] = x is s[i] once again assigned
S[I:J] = R assigns a list fragment once again
Del S[i] Remove an element from the list
Del S[i:j] Delete a fragment from the list
(4)(3)Python Syntax Simple introduction----type conversion
x >> y Shift Right
X & y Bitwise AND
x | Y bitwise OR
x ^ y bitwise XOR (exclusive OR)
~x rollover by bit
x + y Plus
X-y minus
X * y Multiply
X/y general except
x//y floor except
X * * y-exponentiation (XY)
X% y modulo (x mod y)
-X Change the symbol bit of the operand
+x do nothing.
~x ~x=-(x+1)
ABS (x) absolute value
Divmod (x, y) return (int (× x/y), x% y)
Pow (x, y [, modulo]) returns (x * * y) x% modulo
Round (x, [n]) is rounded. n is the number of decimal places
x < y less than
x > Y greater than
x = = y equals
X! = y Not equal (same as <>)
x >= y is greater than or equal to
X <= y is less than or equal to
Two: Python app
(1) File processing
filename = raw_input (' Enter your file name ') #输入要遍历读取的文件路径及文件名称file = open (filename, ' r ') done = 0while not done:< C5/>aline = File.readline () if (aLine! =): print ALine, else: Done = 1file.close ()
Explanation:
The difference between. ReadLine () and. ReadLines () is that the latter reads the entire file one at a time. ReadLines () proactively parses the contents of the file into a list of rows that can be made by Python for ... in ... Structure
Be processed. Another aspect: ReadLine () reads only one line at a time, usually much slower than. ReadLines ().
You should use. ReadLine () only if there is not enough memory to read the entire file at once.
Assuming the Python file reads at the end of the file, an empty string "is returned. The assumption is that you read a blank line. It will return a ' \ n '
Python's ReadLine () method, with a newline character ' \ n ' at the end of each line. Sometimes the last line of a file does not end with ' \ n ' and does not return ' \ n '.
The ReadLines () method returns a list, and ReadLine () returns a string.
(2) Error handling
Python error TypeError: ' str ' object is not callable
This error can occur when a generic intrinsic function is used as a variable name. For example:
Range=1
For I in range (0,1):
.........
Would have reported such a mistake.
This error will be reported on the for line. But the cause of time is in range=1 this line, assuming the two lines are far apart, how very difficult to be found.
so be careful not to define the variable name yourself with internal variables and function names. Or STR is pre-defined.
str=10
For I in Range (1,10):
Print str (i)
(3) Comprehensive application, file reading, console reading, time conversion, encoding conversion
Import timefrom Time import strftimeimport sysreload (SYS) sys.setdefaultencoding (' UTF8 ') #-*-coding:cp936-*-print (" Hello, python! ") #!/usr/bin/pythona = 21b = 10c = 0c = a + bprint "line 1-value of C are", CC = A-bprint "line 2-value of C is", C C = A * bprint "line 3-value of C are", c C = a/bprint "line 4-value of C are", c C = a% bprint "line 5-value of C Is ', CA = 2b = 3c = a**b print "line 6-value of C are", CA = 10b = 5c = a//b print "line 7-value of C is", C # for R Epeat itslist = [2, 4, 6, 8]sum = 0for num in list:sum = sum + numprint ("The sum is:", sum) # Print and Input, Assignme Ntprint ("Hello, I ' m python!") Name = input (' What is your name?\ n ') print (' Hi,%s. '% name) # test forfruits = [' Banana ', ' Apple ', ' Lime ']loud_fruits = [Fruit.upper () for fruit in fruits] Print (loud_fruits) # Open, write and read filefo = Open ("./tmp/foo.txt", "w+") fo.write ("Python is a gerat language.\nyeah it S great!! \ni am Zhang Yapeng, who is you?\n ") t_str = U ' I'm Zhang Yanpeng, what are you? ' Print (T_STR) fo.write (T_STR) fo.close () #read and WRITEFR = Open ("./tmp/foo1.txt", "r+") FW = Open ("Foo_rw.txt", "WB") Done = 0;localtime = Time.asctime (Time.localtime (Time.time ())) print ' Local current time: ', Localtimefw.write (localtime + "\ n") while not done:t_str = Fr.readline () if (t_str! = "): print" Read String is: ", T_str Fw.writ E (t_str) Else:done = 1fr.close () fw.close () # Test time (import) localtime = Time.localtime (Time.time ()) print "Loc Al Current time: ", localtime# format the time from time import strftimet_time = strftime ('%y-%m-%d%h:%m:%s ', localtime ) print "Formatting local current time:", t_time# design the time by yourselfyear = StR (localtime.tm_year) Mon = str (localtime.tm_mon) day = str (localtime.tm_mday) hour = str (localtime.tm_hour) mins = str ( Localtime.tm_min) sec = str (localtime.tm_sec) newtime = u "Time is:" + year + "years" + Mon + "Month" + Day + "days" + Hour + ":" + mins + ":" + secprint "Local Current time:", NewTime
(4) Implementation diagram:
(5) Summary:
(1) Python is a fast-starting language that handles big data in a good language, and some specifications are similar to the C + + language. such as syntax and some function naming. Opening and writing of files. And
Read-write mode, very similar to C + +
(2) As the beginning of the "Python is the glue language, use the Python high-speed generation program prototype (sometimes even the program's last interface), and then have special requirements in the section." With more suitable
of the language rewriting. than the Graphics rendering module in 3D games. Performance requirements are particularly high and can be rewritten in C + +. "
(3) Sharing a very basic system of learning sites
(4) The learning site mentioned in w3cschool.cc (3) is a very basic course for people. If you want to go deep, the detailed content can Baidu
The way to Big data processing (10 minutes to learn Python)