python basis tutorial: Python basic grammar

Python language with Perl, C and Java languages ​​have many similarities. However, there are some differences.

In this chapter we learn Python future of basic grammar, lets you quickly learn Python programming.

First Python Program
Interactive programming
interactive programming not need to create a script file that is coming through interactive mode Python interpreter code.

On linux you just type Python commands in the command line to start the interactive programming, prompt as follows:

     
 
    

 

Window on when installing Python interactive programming client has been installed, the following prompt:

 

Enter the following text message python prompt, and then press Enter to view operating results:

1 >>> print "Hello, Python!"

 

In Python version 2.7.6, the above examples of output results are as follows:

 

Scripted programming
call script parameters through the interpreter starts executing the script until the script is finished. When the script finishes, the interpreter is no longer valid.

Let's write a simple Python script. All files will be Python .py extension. The following test.py to copy the source code file.

print "Hello, Python!"

 

Here, assuming you've set up a Python interpreter PATH variable. Use the following command to run the program:

$ python test.py

Output:

 

Let's try another way to execute Python scripts. Test.py modify file as follows:

Examples

1  # ! / Usr / bin / Python 
2  
3  Print  " the Hello, Python! " 
4 Here, suppose your Python interpreter in the / usr / bin directory, execute the script using the following command:
 5  
6 $ chmod + the X-test.py # script file add executable permission 
7 $ ./test.py

Output:

1 Hello, Python!

Python identifiers
in Python, identified by letters, numbers, underscores.

In Python, all identifiers can include letters, numbers, and the underscore (_), but can not start with a number.

Python identifier is case sensitive.

Identifiers beginning with an underscore have a special significance. _Foo begin single underline represent can not directly access the class attributes, accessed through the classes provided from xxx import * can not be introduced.

In the private members of class representatives __foo beginning double underlined, double underlined to __foo__ Representative Python in particular beginning and end of specific identification method, such as __init __ () representative of the class constructor.

Python same row can display multiple statements, method is to use a semicolon; separated, such as:

1 >>> print 'hello';print 'runoob';
2 hello
3 runoob

Python reserved characters

The following list shows the reserved words in Python. These words can not be used to retain a constant or variable, or any other identifier names.

All Python keywords contain only lowercase letters.

Row and indent
with Python and the biggest difference is in other languages, the Python code blocks without using braces {} class controlled, functions, and other logic judgment. python most unique is to use indentation to write modules.

The number of spaces to indent is variable, but all of the code block statement must contain the same number of spaces to indent, this must be strictly enforced. As follows:

Examples

1 if True:
2     print "True"
3 else:
4   print "False"

The following code will execute error:

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 # 文件名:test.py
 4 
 5 if True:
 6 print "Answer"
 7 print "True"
 8 else:
 9 print "Answer"
10 # 没有严格缩进,在执行时会报错
11 print "False"

 


执行以上代码,会出现如下错误提醒:

1 $ python test.py 
2 File "test.py", line 10
3 print "False"
4 ^
5 IndentationError: unindent does not match any outer indentation level

IndentationError: unindent does not match any outer indentation level错误表明,你使用的缩进方式不一致,有的是 tab 键缩进,有的是空格缩进,改为一致即可。

如果是 IndentationError: unexpected indent 错误, 则 python 编译器是在告诉你"Hi,老兄,你的文件里格式不对了,可能是tab和空格没对齐的问题",所有 python 对格式要求非常严格。

因此,在 Python 的代码块中必须使用相同数目的行首缩进空格数。

建议你在每个缩进层次使用 单个制表符 或 两个空格 或 四个空格 , 切记不能混用

Guess you like

Origin www.cnblogs.com/xiaoyiq/p/11145139.html