A: Introduction to Python
(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, and it is able to easily connect various modules made in other languages, especially in C + +.
together. A common application scenario is to use Python to quickly build a prototype of a program (sometimes even the final interface of the program), and then rewrite it in a more appropriate language with the parts that are specifically required.
Compared with the Graphics rendering module in 3D games, performance requirements are particularly high and can be rewritten in C + + .
(2) Introduction to Python syntax----type conversions
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)Introduction to Python syntax----type conversions
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] re-assigned
S[I:J] = R re-assigns the list fragment
Del S[i] Remove an element from the list
Del S[i:j] Delete a fragment from the list
(4)(3)Introduction to Python syntax----type conversions
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]) 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:< C2/>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 at once,. ReadLines () automatically parses the contents of the file into a list of rows that can be used by Python for ... in ... Structure
Be processed. On the other hand,. 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.
If the Python file reads at the end of the file, an empty string "is returned, and if a blank line is read, a ' \ n ' is returned
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.
Such a mistake will be reported in the for line, but the time is caused by the range=1 line, if the two lines are far apart, how difficult to find.
so pay special attention to not customizing the variable name 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 = [fr Uit.upper () for fruit in Fruits]print (loud_fruits) # Open, write and read filefo = Open ("./tmp/foo.txt", "w+") fo.write ("Pyt Hon is a gerat Language.\nyeah it 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.write (t_str ) Else:done = 1fr.close () fw.close () # Test time (import) localtime = Time.localtime (Time.time ()) print "Local Curr Ent 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:", new Time
(4) Operation diagram:
(5) Summary:
(1) Python is a very fast-starting language that handles big data, and some specifications are very similar to the C + + language, such as syntax and some function naming, file opening and writing, and
Read-write mode, very similar to C + +
(2) As the beginning of the "Python is the glue language, using Python to quickly generate a prototype of the program (and sometimes even the final interface of the program), and then to the special requirements of the part, with a more appropriate
of the language Rewriting, than such as the Graphics rendering module in 3D games, performance requirements are particularly high, can be rewritten in C + +. "
(3) share a very basic system of learning sites
(4) w3cschool.cc (3) mentioned in the study site is very basic people course, if you want to go deep, specific content can Baidu
The way to Big data processing (10 minutes to learn Python)