Python Learning Note: Day2 's Python Foundation

Source: Internet
Author: User

some basic syntax for Python

Unlike Java,c/c++, the block of statements is distinguished by curly braces {}. Python is a statement block that is indented to represent a statement block, with the same indentation level as the same level. A 0-level indentation in a script file is a statement that is executed when the file is loaded.

such as print. Opening a new indentation requires the use of: (colon), which represents the next level of statement blocks, such as conditions, loops, or function definitions. Indentation is best used with four spaces. And be aware that indentation is consistent, using spaces to use spaces,

Use tab to use tab and mix to get indentation error: Indentationerror:unindent does not match no outer indentation level

Finally, it is important to note that the Python program is case-sensitive, and if the case is written incorrectly, the program will error.

operator

Very similar to Java and C, + (plus),-(minus), * (multiply),/(except),% (remainder), * * (exponential operation), = (Assignment). And the reduction operations, such as + =,-=, *= and/=. Assignment operations are consistent with other languages. Logical Operations > < <= >=! = = = = as in other languages.

Different have not logical non, and logical with and or logical OR.

Notes and Documents

In a row, the beginning of the # Place is the comment. does not affect the next line. The quotation marks are placed at the beginning of the file, at the beginning of the function, or at the beginning of a class, which is the document comment, with the/** in Java ... * * Function and purpose are the same.

Fold Line

If the line is too long and cannot be written, you need to continue writing on the next line, and you can use \ To tell Python that the next line continues.

write multiple statements on one line

Python is a statement placed on a line, the end of the line can be optional plus, but if you want to put multiple statements on one line, you need to separate the statement: a = 1; b = 2; c = 3; Although this is syntactically feasible, it is not a good habit, and the vast majority of programming specifications are to write a single statement on one line.

Basic data Types
    • Int
    • Long
    • bool
    • Float

Very close to Java. can be approximated as consistent. The value of bool is true and false, or 0 (false), not 0 is true

Output

With print A string, you can output the specified text to the screen. For example ‘hello, world‘ , the output is implemented in code as follows:

Print ' Hello world! ' Hello world! Print " Hello world! " Hello world!

printEach string will be printed in turn, and a comma "," will output a space, so the output string is spelled like this.

Print '  One ','both','three',' Four ' One, three four

printYou can also print an integer or calculate the result

Print Print 100+200300

Combine calculations and strings with print

Print '  ', 100+200100 +  300
input
>>>raw_input ("\n\npress the"enter key to exit. ) ")   # "\ n \ nthe" outputs two new blank lines before the result output. Once the user presses the key, the program exits

use input result as variable output

>>>name = raw_input (')>>>print'  Hello,', name
Please enter your NAME:ABC
Hello, ABC

python identifiers

In Python, identifiers are composed of letters, numbers, and underscores.

In Python, all identifiers can include English, numeric, and underscore (_), but cannot begin with a number.

Identifiers in Python are case-sensitive.

Identifiers that begin with an underscore are of special significance. A class attribute that begins with a single underscore (_foo) cannot be accessed directly, and is accessed through the interface provided by the class and cannot be imported with "from XXX import *";

A double underscore (__foo) represents a private member of a class; a double underscore (__foo__) represents a special method-specific identifier for Python, such as __init__ (), which represents the constructor of a class.

python reserved characters

The following list shows the reserved words in Python. These reserved words cannot be used as constants or variables, or as any other identifier name. All Python keywords contain only lowercase letters

and Exec Not
Assert Finally Or
Break For Pass
Class From Print
Continue Global Raise
Def If Return
Del Import Try
Elif Inch While
Else Is With
Except Lambda Yield
Multi-line statements

In Python statements, a new line is generally used as the Terminator of a statement, but we can use a slash (\) to divide a line of statements into multiple lines, as follows:

>>> total="One,"+","+"  three"print  totalone,two,three
>>> one=" You">>> two=" is">>> three=" the">>> four=" Best">>> str=one+two+. .. three+... four>>>Printstryouarethebest>>> str=one,two,... three,... four>>>PrintStr (' You',' is',' the',' Best')

The statement contains [], {}, or () parentheses do not need to use a multiline connector. The following example:

>>> days = ['Monday','Tuesday','Wednesday',... 'Thursday','Friday']>>>Printdays['Monday','Tuesday','Wednesday','Thursday','Friday']
Python Quotes

Python receives single quotation marks ('), double quotation marks ("), and three quotation marks (" ' "" ") to denote the string, and the beginning of the quotation mark must be the same type as the end.

Where three quotation marks can be composed of multiple lines, writing the shortcut syntax of multiple lines of text, common language file strings, at a specific location of the file, as a comment.

Single quotation mark ('):

>>> sentence = ' This is \ ' a sentence. '
Printis' a sentence.

Double quotation marks ("):

" This is ' a sentence. " Print  is'  a sentence.

Three quotation marks ("" "" "")

""" This is \ ' a paragraph. It is ... made up of multiple lines and sentences. """ Print  is'  a paragraph. It is and sentences.
python comments

A single line comment in Python starts with #

Python does not have a block comment, so now the recommended multiline comment is the #比如:

# First Comment  >>> Print " Hello, python!. ";  # Second comment
Python empty line

A blank line separates between functions or methods of a class, representing the beginning of a new piece of code. The class and function entries are also separated by a line of blank lines to highlight the beginning of the function entry.

Blank lines are different from code indentation, and empty lines are not part of the Python syntax. When you write without inserting a blank line, the Python interpreter runs without errors. However, the purpose of a blank line is to separate the code of two different functions or meanings, which facilitates the maintenance or refactoring of future code.

Show multiple statements on the same line

Python can use multiple statements in the same row, with semicolons (;) split between statements, and here's a simple example:

Name= "ABC";p rint name
Abc
multiple statements form a code group

Indenting the same set of statements constitutes a block of code, which we call the code group.

Compound statements such as if, while, Def, and class, the first line begins with a keyword, ends with a colon (:), and one or more lines of code after that line form the code group.

We refer to the first line and the following code group as a clause (clause).

The following example:

# Print absolute value of an integer:a =if a >= 0:    print  aelse< /c7>:    Print -A

Python Learning Note: Day2 's Python Foundation

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.