Python syntax rules

Python basic syntax

Python syntax as opposed to C, C ++, Java is more concise, more in line with people's normal thinking. Python basic syntax of this introduction, through this article you can learn the following.

  • Master the basic syntax of Python
  • Recognition Python keywords

Python is a scripting language that has the following characteristics:

  • Object-Oriented: Class
  • Grammar: marked indentation
  • Notes: # single-line comments, "" "multi-line comments" "" '' 'I am also a multi-line comment' '
  • Print and Output: print (), input ()
  • Variables: variable determines the type of a variable at the time of the assignment
  • Modules: Module loaded through import module name

    Python identifiers

    Identifier is the name used by the user program, for to variables, constants, functions, and so named statement blocks, in order to establish a relationship between the name and use. Identifier is generally composed of letters and numbers and other characters.

Identifier naming the following provisions:

  • Start with a letter or underscore _, and the rest of alphanumeric characters or underscore
  • Python follows the small hump nomenclature
  • Instead of using Python keywords are named

Code Example:

num = 10 # 这是一个int类型变量

Error name Example:

123rate(数字开头)、 mac book pro(含有空格),class(关键字)

Python keyword
in the following list of keywords can not use as identifiers. Keywords Python language contains only lowercase letters.

and exec not
assert finally or
break for pass
class from print
continue global raise
def if return
of the import try
elif in while
else is with
except lambda yield

Python indentation statement

Python Compared with traditional languages (C, Java) the biggest difference is that there is no braces {}, but use code logic indentation alignment means. End of the line there is no semicolon ;directly from downlink to another, close to the pseudo-code. Code style with 'elegant', 'simple' is known.

Code Example:

def main():
    # 通过缩进标记函数代码块
    print("这是一个主函数")
    print("我是函数内部的代码块")

If no indentation aligned length corresponding to a syntax error is generated or indentation errors.

Python multi-line statement

Bank of China is no semicolon at the end Python statements ;directly from the downstream to another.

Slash can be used in multiple lines, as follows:

num1 = 10
num2 = 20
num3 = 30
result = num1 + \
        num2 + \
        num3

If list, dict, tuple do not need multi-line connector. As follows:

nums_list = [1, 2, 3, 4
5, 6, 7]

Python's use in quotes

Python language, use single quotes 'and double quotes "represent a string data type. NOTE: The quotation marks must appear in pairs. code show as below:

string = "python666"
string1 = 'python'
string2 = """我是字符串内容"""
string3 = '''我是字符串内容'''

Comments in Python

Python language, two kinds of comments: single-line comments (#) and a multi-line comments (single and double quotes), the following examples:

# 你好,我是单行注释
'''
我是多行注释
'''
"""
我也是多行注释
"""

Python spaces and line breaks

The reason is simple Python language, because the added spaces and blank lines in the code. Do not add space and blank lines will not complain when you write the code, and then add just for readability.

string = "abc"

Variables, using both sides of the equal sign spaces look more beautiful.

def func():
    pass


def main():
    pass

Two blank spaces between the function and the function, more beautiful.

print

Python is a built-in print function. print () default line feed, if you do not wrap plus end parameters.

print('hello world', end='')

Entry

Python is a built-in input function. input () function, note that a default string is input.

str_name = input("请输入你的名字")

Note: inside the brackets is a message.

Guess you like

Origin www.cnblogs.com/liudemeng/p/12052485.html