The way to Big data processing (10 minutes to learn Python)

Source: Internet
Author: User

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
  1. filename = raw_input (' Enter your file name ') #输入要遍历读取的文件路径及文件名
  2. FILE = open (filename,' R ')
  3. Done = 0
  4. While not doing :
  5. ALine = File.readline ()
  6. if (aLine! = "):
  7. Print ALine,
  8. Else:
  9. Done = 1
  10. 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
    1. Import time
    2. From time import strftime
    3. Import Sys
    4. Reload (SYS)
    5. sys.setdefaultencoding (' UTF8 ')
    6. #-*-coding:cp936-*-
    7. Print ("Hello, python!")
    8. #!/usr/bin/python
    9. A =
    10. b = Ten
    11. c = 0
    12. c = A + b
    13. Print "line 1-value of C is", C
    14. c = A-B
    15. Print "line 2-value of C is", C
    16. c = A * b
    17. Print "line 3-value of C is", C
    18. c = A/b
    19. Print "line 4-value of C is", C
    20. c = a% b
    21. Print "line 5-value of C is", C
    22. A = 2
    23. b = 3
    24. c = a**b
    25. Print "line 6-value of C is", C
    26. A = ten
    27. b = 5
    28. c = a//b
    29. Print "line 7-value of C is", C
    30. # for Repeat it
    31. List = [2, 4, 6, 8]
    32. sum = 0
    33. For num in list:
    34. sum = sum + num
    35. Print ("The sum is:", sum)
    36. # Print and Input, assignment
    37. Print ("Hello, I ' m python!")
    38. Name = input (' What's Your name?\n ')
    39. Print (' Hi,%s. '% name)
    40. # test for
    41. Fruits = [' Banana ', ' Apple ', ' Lime ']
    42. Loud_fruits = [Fruit.upper () for fruit in fruits]
    43. Print (loud_fruits)
    44. # Open, write and read file
    45. fo = open ("./tmp/foo.txt","w+")
    46. Fo.write ("Python is a gerat language.\nyeah its great!! \ni am Zhang Yapeng, who is you?\n ")
    47. t_str = u' I am Zhang Yanpeng, what are you? '
    48. Print (T_STR)
    49. Fo.write (T_STR)
    50. Fo.close ()
    51. #read and Write
    52. FR = Open ("./tmp/foo1.txt","r+")
    53. FW = Open ("Foo_rw.txt","WB")
    54. Done = 0;
    55. LocalTime = Time.asctime (Time.localtime (Time.time ()))
    56. Print "Local Current time:", LocalTime
    57. Fw.write (localtime + "\ n")
    58. While not doing :
    59. T_str = Fr.readline ()
    60. if (t_str! = "):
    61. print "Read String is:", T_str
    62. Fw.write (T_STR)
    63. Else:
    64. Done = 1
    65. Fr.close ()
    66. Fw.close ()
    67. # Test Time (Import)
    68. LocalTime = Time.localtime (Time.time ())
    69. Print "Local Current time:", LocalTime
    70. # Format the time from time import strftime
    71. T_time = strftime ( '%y-%m-%d%h:%m:%s ', localtime)
    72. Print "Formatting local Current time:", T_time
    73. # Design the time by yourself
    74. Year = str (localtime.tm_year)
    75. Mon = str (localtime.tm_mon)
    76. Day = str (localtime.tm_mday)
    77. hour = str (localtime.tm_hour)
    78. mins = str (localtime.tm_min)
    79. SEC = str (localtime.tm_sec)
    80. NewTime = u"Time is:" + year + "years" + Mon + "Month" + Day + "days" + Hour + ":" + mins + ":" + sec
    81. Print "Local Current time:", NewTime

The way to Big data processing (10 minutes to learn Python)

Related Article

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.