Stupid way to learn Python (Third edition) Learn note 1

Source: Internet
Author: User

 1. Rounding floating-point numbers: Round (1.7333)  2. Format character: What is the difference between%s%d%r%r and%s? %r is good for debugging because it shows the raw data of the variable, while the other symbols are used to show the output to the user. Remember:%r is used as debug,%s for display. The escape sequence after using%r is not working. Because%r prints out the original string you wrote in the code, which contains the original escape character. line1 = Raw_input ("Line 1:") line2 = Raw_input (' Line 2: ') Line3 = Raw_input ("Line 3:") lines = "%s \ n%s \ n%s \ n"% (line 1, line2, Line3)  3. Escape sequences: NewLine characters: \ n (there is also a way to extend a string to multiple lines by using the three-quote "" ", which allows you to put any number of lines of text between a set of three quotes) special escape sequences: double backslash \ \ These two character combinations will print a backslash. For single and double quote escapes: "I am 6 ' 2\" tall. "# with \" to escape the double quotes in the string 6 ' 2 "' I am 6\ ' 2" tall. ' # to escape the single quotes in a string 6 ' 2 '   doubt: while True:for I n ["/", "-", "|", "\ \", "|"] Print "%s\r"% i, where \ r is the function?  4. The string is quoted, and the variable is not quoted  5. Add a comma after the print statement so that print does not output a new line character and ends up running to the next line. Print "How is old is you?", when age = Raw_input () adds a comma, the result is one line: How old is You? 27 without commas, run the result in two lines: how old is you?27 6. Raw_input () and input () raw_input () are used to get input to the console, treat all inputs as strings, and return string types. Note that both input () and raw_input () functions can receive strings, but raw_input () directly reads the console input (any type of input it can receive). For input (), it wants to be able to read a valid Python expression, that is, you must use quotation marks when you enter a stringEnclose it, or it will trigger a syntaxerror. Input () has its own attribute when dealing with a pure numeric input, and it returns the type of the number entered (int, float) (For example: input (1 + 3) returns an int of type 4). Input () handles what you enter as Python code, which can be a security issue. Unless there is a special need for input (), it is generally recommended to use raw_input () to interact with the user. Note: Python3 input () receives the STR type by default. x = Int (raw_input ()) Converts the user input string int () to an integer raw_input () in parentheses to enclose the hint message y = raw_input ("Name?")  7. Python programmer style: No more than 80 characters per line of  8. Pydocpydoc is a python-brought module that is used to automatically generate documents from Python modules that can be rendered based on text, can also generate Web pages, and can be rendered on the server as a browser. Under Windows: D:\>python-m pydoc <modulename> # For example: python-m pydoc raw_input-m parameter: Python runs the module under the script Linux/unix: $ Pydoc <modulename> # For example, Pydoc raw_input enter Q to exit pydoc 9. We call the import-in function a module (modules). From sys import argv this sentence means to remove the argv module from the SYS code base. ARGV is a "parametric variable" that contains the arguments you pass to Python. From sys import Argvscript, first, second, third = Argvprint ' The script is called: ', Scriptprint ' Your first variable is: ", Firstprint" Your second variable is: ", Secondprint" Your third variable is: ", third the second statement is to unpack the argv, assigning all parameters to the left variable name in turn: SCRI PT, first, second, thiRd To be executed with the command line, note that three parameters must be passed: What is the difference between argv and Raw_input ()? The difference is the timing of the user's input. If the parameter is entered when the user executes the command, that is argv; if the command line argument is a string. argv and Raw_input () cannot be used together. The  from os.path Import existsexists command takes the file name string as an argument, and if the file exists returns true, there is no return false. Print "Does the output file exist? %r "% exists (To_file)  10. Read File txt = open ("file name") print Txt.read () What you get from Open is something called "file object" that can be imagined as a tape drive or DVD player. You can freely access it anywhere and read it, but the object itself is not its content. This "file object" itself also supports some commands. The way it accepts commands is to use a period ".", followed by your command, followed by parameters like Open and raw_input. Open is cautious about writing to files, and writes only when specifically specified. such as: Target.open (filename, ' W ') ' W ' is just a special string that represents the access mode of the file, except that there is ' r ' for reading, and ' a ' means append. If you write only open (filename) then use the ' R ' mode, which is the default way to work with the open () function.   What modifiers are available to control file access? The most important is the + modifier, the wording is "w+", ' r+ ', ' A + ', so that the file will be read and write at the same time open, and the use of the file location is also a bit different.  11. Enter Python code at the command line: Enter python at the command line and hit Enter, then you are in the Python environment. Exit input quit ().  12. FILE-related commands (methods/functions) close-close the file. Save it with your editor's file. a meaning. Read-reads the contents of the file. You can assign the result to a variable. Readline-reads a line in a text file. By scanning each byte of the file until a \ n is found, and then stops reading and returns the contents of the previous file. truncate-empty the file, use this command with care. Write (stuff)-writes stuff to the file. You need to receive a string as a parameter to write the string to the file. F.seek(0)-F here is a variable name, which refers to a file. Files in Python can be imagined as an old-fashioned tape drive or DVD player, which has a "head" to read the data and can manipulate the file through the "head". Each time you run F.seek (0), you go back to the beginning of the file, and run F.readline () reads a line of the file, and the "head" moves to \ nthe back. The Seek () function deals with bytes rather than rows, and seek (0) only hands to the 0byte of the file, which is the position of the first byte.  13. The command to print the contents of the file to the screen under Windows is type.  14. Len () function: Returns the length of the string you passed as a number.  15. def print_two (*args): arg1, arg2 = Argsprint "arg1:%r, arg2:%r"% (arg1, arg2) *argv in * The function is to tell Python to let it take all the parameters of the function, and then put it in the name called List of args to go. Similar to argv, except that the former is used on functions. There is no special case in general.  16. x + = y equals x = x + y 

Stupid way to learn Python (Third edition) Learn note 1

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.