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)

Two days yesterday and today, the book "Concise Python Programming" is finished, including the code in the book. Now record the core notes so that we can review them later.

Introduction to the first chapter

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 focuses 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. Including Linux, Windows, FreeBSD, Macintosh, Solaris, OS/2, etc.;
  6. Explanatory: A program written in the Python language does not need to be compiled into binary code. Inside the computer, the Python interpreter translates the source code into an intermediate form called bytecode, and then translates it into the machine language used by the computer and runs it.
  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 run 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 can embed python in your C/C + + program to provide 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 expressions, document generation, unit tests, threads, databases, Web browsers, CGI, FTP, e-mail, XML, XML-RPC, HTML, WAV files, cryptography systems, GUI (graphical user interface), TK, and other system-related operations. Remember that all of these features are available as long as Python is installed. In addition to the standard library, there are many other high-quality libraries, WxPython, twisted, and Python image libraries, among others.

Chapter II Installation of Python

For Linux systems, it is likely that you have already installed Python in your system. Otherwise, you can install Python through the package management software that came with your release. For Windows systems, installing Python is downloading the installer and then double-clicking it, performing by default, and then configuring environment variables.

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 as long as 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 can also use idle.
    • >>> is the prompt you type in the Python statement.

2. Using source files

    • start the editor of your choice, enter the program below, 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) and 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. Comments

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

4. Get Help

    • run 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! " Such a 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 three quotation marks ("' or" "): using three quotation marks, you can indicate a multiline string.
      • escape character: Suppose you want to include a single quotation mark (') 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 understand where the string begins and where it ends. So, you need to specify the single quotation mark instead of the end of the string. This task can be accomplished with an escape character. You use \ ' to indicate single quotes--notice the 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: Natural strings are 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 allows you to work with Unicode text-you only need to prefix the string with u or U. For example, U "This is a Unicode string.".
      • literal-hyphen string

4, note the place:

    • There is no dedicated char data type in Python.
    • Remember, single and double quote strings are exactly the same-they are not different in any way.
    • Be sure to use a natural string to process the regular expression. Otherwise, you will need to use a lot of back slashes.

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).
    • The identifier name is case-sensitive. For example, MyName and myname are not an identifier. Notice the lowercase n in the former and the uppercase N in the latter.

7. Data type

variables can handle different types of values, called data types. The basic type is the number and string .

8. Objects

Python calls anything that is used in the program as an object .

9, the standard procedure of writing Python program:

    1. Open your favorite editor;
    2. Enter the program code in the example;
    3. Save it as a file with the file name given in the comment. I follow the convention to keep all Python programs in the extension. py;
    4. Run the interpreter command Python program.py or use the idle run program.
Notice two points:
    • Python only needs to assign a value to them when using variables. You do not need to declare or define a data type.
    • If you want to use more than one logical line in a physical line, you need to use a semicolon (;) to specifically indicate this
      The use of. A semicolon represents the end of a logical line/statement.

10. Indent

Whitespace is important in python. In fact the gap at the beginning of the line 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, which is used to determine the grouping of statements. Do not use tabs and spaces to indent, as this is not working correctly across different platforms. It is strongly recommended that you use a single tab or two or four spaces at each indent level. Choose one of these three indentation styles. More importantly, choose a style and use it consistently.


Fifth operator and expression

The function of an operator is to accomplish something that is represented by a symbol such as + or another specific keyword. The operator requires data to be manipulated, and such data 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) <p Re name= "code" class= "python" ><span style= "color: #006600;" > Output:</span>     $ python expression.py area is     14 Perimeter

Note:python prints "pretty" output. Although we did not specify a space between ' area ' and the variable area, Python automatically placed a space there, so that we could get a clear and beautiful output, and the program became easier to read.



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.