The core notes of concise Python programming (the Book of the Sermon)

Source: Internet
Author: User
Tags natural string

August 20, 2014 "Concise Python Programming" Core notes (note)

Yesterday and today in two days time. The book "Concise Python Programming" is finished, including the code on the book. Now record the core notes so that we can review them later.

The first chapter briefly introduces

Python language is one of the few programming languages that can be called simple and powerful.

You will be pleasantly surprised to find out how simple the Python language is, and it is focused on how to solve the problem rather than the syntax and structure of the programming language.

Features of Python:

  1. Simple
  2. Understand
  3. Free, open source
  4. High-level language: Advanced language
  5. Portability: Can be ported on many platforms.

    Includes Linux, Windows, FreeBSD, Macintosh, Solaris, OS/2, and more;

  6. Explanatory: A program written in the Python language does not need to be compiled into binary code.

    Inside the computer. The Python interpreter converts the source code into an intermediate form called bytecode. It is then translated into the machine language used by the computer and executed.

  7. Object-oriented: Python, which supports process-oriented programming, also supports object-oriented programming.

  8. Extensibility: If you need a piece of your critical code to execute faster or you want some algorithms to be private, you can write some of your programs in C or C + + and then use them in your Python program.
  9. Embeddable: You'll be able to embed python in your C + + program. This provides scripting functionality to your program users.
  10. Rich Library: The Python standard library is really huge. It can help you with all kinds of work, including regular forms, document generation, unit tests, threads, databases, Web browsers, CGI, FTP, e-mail, XML, XML-RPC, HTML, WAV files, password systems, GUI (graphical user interface), TK and other system-related operations. remember. Just have to install Python. All of these features are available. In addition to the standard library. There are more high-quality libraries, WxPython, twisted and Python image libraries, and more.

Chapter II Installation of Python

For Linux systems, it is very likely that you have already installed Python in your system.

Otherwise, you can install Python through the package management software that comes with your distribution. For Windows systems. Installing Python is to download the installer and then double-click it, run it by default, and then configure the environment variable.

Chapter Three The initial steps

Two ways to use Python to ship programs-using an interactive prompt interpreter or using source files.

1. Using an interpreter with a prompt

    • For Windows users, you should be able to start the interpreter from the command line only if you have correctly set the PATH variable.

      Or you can choose to use the idle program. Idle is an abbreviation for an integrated development environment.

      Click Start, program->python 2.3->idle (Python GUI). Linux users are also able to use idle.

    • >>> is the prompt you type in the Python statement.

2. Using source files

    • Start the editor of your choice, enter the following program, and save it as helloworld.py.

   <span style= "FONT-SIZE:18PX;" >    <span style= "color: #006600;" > (source file: code\helloworld.py) </span>      #!/usr/bin/python      # Filename:helloworld.py      print ' Hello World ' </span>
    • Please open the Shell (Linux terminal or DOS prompt). Then type the command Python helloworld.py. If you use idle, use the menu Edit->run script or use the keyboard shortcut ctrl-f5.
<span style= "FONT-SIZE:18PX;" >       <span style= "color: #009900;" > Output:</span>      $ python helloworld.py        Hello World </span>

3. Gaze

    • Use the # symbol to mark the gaze, starting with # until the end of the line is staring.

4. Get Help

    • execute Help (object)-This will show the Boject class Help.

      For example, Help () str, the Str class is displayed.

      The Str class is used to hold the various text (strings) that your program uses.

    • Press Q to exit help.

Chapter Fourth Basic Concepts

1, the literal meaning of the constant

    • An example of a literal constant is a number like 5, 1.23, 9.25e-3, or as ' this is a string ', ' It ' a string! " This string.

2, the number

    • There are 4 types of numbers in Python-integers, long integers, floating-point numbers, and complex numbers.

3. String

    • A string is a sequence of characters.
  • use of strings:
    • Use single-quote ('): You can indicate a string with a single-argument, just like ' Quote me on this '. All whitespace, that is, spaces and table breaks are preserved as is.
    • Use double-Cited ("): Same as single-cited.

    • Use the three-lead ("' or" "): Using the three-lead, you can indicate a multiline string.

    • Escape character: If you want to include a single quote (') in a string, how do you indicate the string? For example, what is the string of what ' s your name?

      Are you sure you won't use ' What ' your name?

      ' To instruct it, because Python will not be clear where this string begins. Where to end.

      So, you need to specify a single argument instead of the end of the string. This task can be completed by escaping the character. You use \ ' to indicate a single quote--pay attention to that backslash. Now you can represent the string as ' what\ ' your name? '. Note: In a string, a single backslash at the end of the line indicates that the string continues on the next line instead of starting a new row.

    • Natural string: A natural string is specified by prefixing the string with R or R.

      For example R "Newlines is Indicatedby \ n".

    • Unicode string: Unicode is the standard way to write international text.

      Python agrees that you work with Unicode text-you only need to prefix the string with u or U. For example, U "This is a Unicode string.".

    • The string is immutable.
    • String literals by literal meaning

4, note the place:

    • There is no dedicated char data type in Python.
    • Remember. The single-and double-argument strings are exactly the same-they are not different in any way.
    • Be sure to use natural strings to handle the normal table.

      Otherwise, you will need to use a very good number of backslashes.

5. Variables

6, the name of the identifier

    • The first character of an identifier must be a letter in the alphabet (uppercase or lowercase) or an underscore (' _ ').

    • Other parts of the identifier name can consist of a letter (uppercase or lowercase), an underscore (' _ '), or a number (0-9).

    • Identifier names are uppercase and lowercase sensitive. For example, MyName and myname are not identifiers.

      Notice the lowercase n in the former and the uppercase N in the latter.

7. Data type

variables can handle different types of values. is called a data type. The main types are number and string .

8. Objects

Python calls whatever is used in the program as objects .

9, the standard procedure of writing Python program:

    1. Open your favorite editor;
    2. Enter the program code in the sample;
    3. Save it as a file with the name of the file given in the gaze. I routinely keep all Python programs in the extension. py;
    4. Execute the Interpreter command Python program.py or use the idle execution program.

Notice two points:
    • Python only needs to assign a value to them when using variables.

      There is no need to declare or define a data type.

    • Assuming you want to use more than one logical line in a physical line, you need to use a semicolon (;) to specifically indicate this
      Method of use. A semicolon represents the end of a logical line/statement.

10. Indent

Whitespace is important in python.

In fact, the beginning of the gap is important. It is called indentation . Whitespace (spaces and tabs) at the beginning of a logical line is used to determine the indentation level of logical lines. This is used to determine the grouping of statements.

Do not use tabs and spaces in combination to indent. As this is across the different platforms of the waiting. does not work correctly. It is strongly recommended that you use a single tab or two or four spaces at each indent level. Choose one of these three types of indentation styles.

More importantly, choose a style and use it consistently.


Fifth operator and expression

The function of an operator is to complete something, which is represented by a symbol such as + or other specific keyword. The operator needs data to perform the operation, which is called the operand.

1. Operators

Operators include + 、-、 *,/,%, * *,//, <<, >>, >>=, <<=, &, |, ^, ~, <, >, = =,!

=, and, or, not.

2, operator precedence: slightly

3. Expression

<span style= "color: #009900;" > (source file: code\expression.py) </span>     #!/usr/bin/python     # Filename:expression.py     length = 5     breadth = 2 Area     = length * Breadth     print ' area are ', area     print ' Perimeter is ', 2 * (length + breadth ) <pre name= "code" class= "python" ><span style= "color: #006600;" > Output:</span>     $ python expression.py area is     14 Perimeter

Note:How Python Prints the "beautiful" output.

Although we did not specify a space between ' area is ' and the variable area. Python took the initiative to put a space there. So that we can get a clear and beautiful output, and the program becomes more readable.



The core notes of concise Python programming (the Book of the Sermon)

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.