Big Data Processing (learn Python in ten minutes), data processing python
I. Introduction to python
(1) Python
Python is an object-oriented, interpreted computer programming language, invented by Guido van rosum at the end of 1989, the first public release was released at 1991.
Year.The Python syntax is concise and clear, and has rich and powerful class libraries. It is often called the glue language. It can easily connect various modules (especially C/C ++) made in other languages.
Together.A common application scenario is,Quickly generate a program prototype using Python(Sometimes even the final interface of the Program), and then rewrite the parts with special requirements in a more appropriate language,
Such as the graphic rendering module in 3D games,If the performance requirement is extremely high, you can use C ++ to rewrite it..
(2) Introduction to Python syntax ---- 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 plural number
Str (x) converts object x to a string
Repr (x) converts object x to an expression string
Eval (str) is used to calculate a valid Python expression in a string and return an object.
Tuple (s) converts the sequence s into a tuples
List (s) converts sequence s into a list
Chr (x) converts an integer to a character
Unichr (x) converts an integer to a Unicode Character
Ord (x) converts a character into its integer
Hex (x) converts an integer into a hexadecimal string.
Oct (x) converts an integer into an octal string
(3) Introduction to Python syntax ---- type conversion
S + r sequential join
S * n, n * s n copies, n is an integer
S % d string formatting (string only)
S [I] Index
S [I: j] Slice
X in s, x not in s subordination
For x in s: Iteration
Len (s) Length
Min (s) minimum element
Max (s) maximum element
S [I] = x is s [I] re-assigned
S [I: j] = r: returns the list fragment value.
Del s [I] deletes an element from the list.
Del s [I: j] deletes a clip from the list.
(4) (3) Introduction to Python syntax ---- type conversion
X> y shift right
X & y
X | y by bit or
X ^ y exclusive or)
~ X flip by bit
X + y plus
X-y Subtraction
X * y Multiplication
X/y
X // y floor Division
X ** y multiplication (xy)
X % y modulo (x mod y)
-X changes the symbol bit of the operand.
+ X does nothing.
~ X ~ X =-(x + 1)
Abs (x) absolute value
Divmod (x, y) returns (int (x/y), x % y)
Pow (x, y [, modulo]) returns (x ** y) x % modulo
Round (x, [n]) Rounding, n is the number of decimal places
X <y is less
X> y is greater
X = y equals
X! = Y is not equal to (same as <>)
X> = y is greater than or equal
X <= y is less than or equal
Ii. python applications
(1) File Processing
Filename = raw_input ('enter your file name') # Enter the file path and file name to be retrieved. file = open (filename, 'R') done = 0 while not done: aLine = file. readline () if (aLine! = ''): Print aLine, else: done = 1file. close () # close the file
Explanation:
. Readline () and. the difference between readlines () is that the latter reads the entire file at a time ,. readlines () automatically analyzes the file content into a row list, which can be composed of Python's... in... structure
. On the other hand,. readline () reads only one row at a time, which is usually much slower than. readlines.Use. readline () only when there is not enough memory to read the entire file at a time ().
If the Python file reads the end of the file, an empty string ''will be returned, and if it reads an empty line, a '\ n' will be returned'
The readline () method of Python. a newline character '\ n' is added at the end of each line'. Sometimes, when the last line of some files does not end with '\ n',' \ n' is not returned '.
The readlines () method returns a list, while readline () returns a string.
(2) handle errors
Python reports TypeError: 'str' object is not callable
This error may occur when an internal function is used as a variable name. For example:
Range = 1
For I in range (0, 1 ):
.........
This error will be reported.
This error will be reported in the for line, but the reason for the time is that in the range = 1 line, if the two rows are far apart, how can it be difficult to find.
Pay special attention not to use the existing internal variables and function names as custom variable names. Or str is pre-defined
Str = 10
For I in range (1, 10 ):
Print str (I)
(3) integrated applications, file reading, console reading, time conversion, and 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 is ", cc = a-bprint "Line 2-Value of c is", c = a * bprint "Line 3-Value of c is ", c = a/bprint "Line 4-Value of c is", c = a % bprint "Line 5-Value of c is ", ca = 2b = 3c = a ** B print "Line 6-Value of c is ", ca = 10b = 5c = a // B print "Line 7-Value of c is", c # for repeat itslist = [2, 4, 6, 8] sum = 0fo R num in list: sum = sum + numprint ("The sum is:", sum) # print and Input, assignmentprint ("Hello, I'm Python! ") Name = input ('What is your name? \ N') print ('Hi, % s. '% name) # test forfruits = ['bana', '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 its great !! \ NI am zhang yapeng, who are you? \ N ") t_str = U' I'm Zhang yanpeng. what are you like? 'Print (t_str) fo. write (t_str) fo. close () # read and writefr = open (". /tmp/foo1.txt "," r + ") fw = open (" foo_r0000txt "," 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. write (t_str) else: done = 1fr. close () fw. close () # test time (import) localtime = time. localtime (time. time () print "Local 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:" + year + "year" + mon + "month" + day + "day" + hour + ":" + mins + ": "+ secprint" Local current time: ", newtime
(4) operation diagram:
(5) summary:
(1) Python is a very fast-paced language that processes big data. Some standards are very similar to c ++, such as syntax and function naming, file opening and reading/writing, and
Read/write mode, very similar to c ++
(2) As stated at the beginning, "python is the glue language. It uses Python to quickly generate a prototype of the Program (sometimes even the final interface of the Program) and then has a special requirement on it, more suitable for use
For example, if the graphic rendering module in 3D games has high performance requirements, you can use C ++ to rewrite it ."
(3) share a very basic system learning website
(4) The learning website mentioned in W3CSchool. cc (3) is a very basic course for people.