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
[Python]View Plaincopy
- filename = raw_input (' Enter your file name ') #输入要遍历读取的文件路径及文件名
- FILE = open (filename,' R ')
- Done = 0
- While not doing :
- ALine = File.readline ()
- if (aLine! = "):
- Print ALine,
- Else:
- Done = 1
- File.close () #关闭文件
Explain:
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. Like what:
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
[Python]View Plaincopy
- Import time
- From time import strftime
- Import Sys
- Reload (SYS)
- sys.setdefaultencoding (' UTF8 ')
- #-*-coding:cp936-*-
- Print ("Hello, python!")
- #!/usr/bin/python
- A =
- b = Ten
- c = 0
- c = A + b
- Print "line 1-value of C is", C
- c = A-B
- Print "line 2-value of C is", C
- c = A * b
- Print "line 3-value of C is", C
- c = A/b
- Print "line 4-value of C is", C
- c = a% b
- Print "line 5-value of C is", C
- A = 2
- b = 3
- c = a**b
- Print "line 6-value of C is", C
- A = ten
- b = 5
- c = a//b
- Print "line 7-value of C is", C
- # for Repeat it
- List = [2, 4, 6, 8]
- sum = 0
- For num in list:
- sum = sum + num
- Print ("The sum is:", sum)
- # Print and Input, assignment
- Print ("Hello, I ' m python!")
- Name = input (' What's Your name?\n ')
- Print (' Hi,%s. '% name)
- # test for
- Fruits = [' Banana ', ' Apple ', ' Lime ']
- Loud_fruits = [Fruit.upper () for fruit in fruits]
- Print (loud_fruits)
- # Open, write and read file
- fo = open ("./tmp/foo.txt","w+")
- Fo.write ("Python is a gerat language.\nyeah its great!! \ni am Zhang Yapeng, who is you?\n ")
- t_str = u' I am Zhang Yanpeng, what are you? '
- Print (T_STR)
- Fo.write (T_STR)
- Fo.close ()
- #read and Write
- FR = Open ("./tmp/foo1.txt","r+")
- FW = Open ("Foo_rw.txt","WB")
- Done = 0;
- LocalTime = Time.asctime (Time.localtime (Time.time ()))
- Print "Local Current time:", LocalTime
- Fw.write (localtime + "\ n")
- While not doing :
- T_str = Fr.readline ()
- if (t_str! = "):
- print "Read String is:", T_str
- Fw.write (T_STR)
- Else:
- Done = 1
- Fr.close ()
- Fw.close ()
- # Test Time (Import)
- LocalTime = Time.localtime (Time.time ())
- Print "Local Current time:", LocalTime
- # Format the time from time import strftime
- T_time = strftime ( '%y-%m-%d%h:%m:%s ', localtime)
- Print "Formatting local Current time:", T_time
- # Design the time by yourself
- Year = 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 + ":" + sec
- Print "Local Current time:", NewTime
The way to Big data processing (10 minutes to learn Python)