Python study notes (three)-basic grammar

table of Contents

一、Python程序执行(编程方式)
二、Python标识符 
三、Python保留字符(字段)
四、行和缩进
五、多行语句
六、Python的引号,用来表示字符串、注释(多行) 
七、Python注释
八、print输出 
九、Python等待用户输入 
十、Python多个语句组成代码组
十一、Python命令行参数

1. Python program execution (programming method)

  • Interactive programming

1. There is no need to create a script file and execute it through the interactive mode of the Python interpreter;

2. Linux, mac, enter python in the command line to enter interactive programming mode

3. Under windows: When installing python, install the default python interactive client IDLE (PythonGUI), or set python environment variables, enter python commands in the cmd console window that comes with windows, and enter the interactive programming mode (command line)

  • Scripted programming

1. Call the python interpreter to execute through the script parameters, the script execution is complete, the interpreter is no longer valid

2. Create a new test.py script file with the following content:

print “Hello,World!”

Set the environment variables of the python interpreter and execute the command: python test.py

3. Specify the python interpreter in the test.py script, the code is as follows:

#!/usr/bin/python 
#Set the path of the pyhton interpreter, here is /usr/bin/python, set
print "Hello World!"  according to the actual situation 
to grant executable permissions to the test.py file, and execute 
chmod +x test.py 
./test.py

Two, Python identifier

1. Identifier composition: letters, numbers, underscores

2. Identifier rules: case-sensitive, not starting with a number

3. Special rules for python identifiers:

  • Beginning with a single underscore, _foo: represents a class attribute that cannot be directly accessed, which needs to be accessed through the interface provided by the class, and cannot be imported with from xxx import *;
  • Beginning with a double underscore, __foo: represents a private member of the class;
  • The double underscore at the beginning and end foo : represents a special method in Python, such as  init () represents the constructor of the class.

Three, Python reserved characters (fields)

1. Reserved fields cannot be used for constants, variables, and any other identifier names (function names, class names, etc.)

2. Python reserved fields and only contains lowercase letters

3. The reserved fields are as follows:

Four, line and indent

1. The biggest difference between python and other languages ​​is that {} is not used to control classes, functions, logical judgments, and indentation is used to write modules

2. Rules:

All code blocks must contain the same number of indentation blanks 
error: IndentationError: unexpected indent It is 
recommended to use the tab key,
2/4 space keys for indentation, and the two cannot be mixed  error: IndentationError: unindent does not match any outer indentation level

Five, multi-line statements

1. Write multiple sentences in one line, the method is to use; separate

print 'hello';print 'python';

2. Write a statement in multiple lines:

  • Slash (\) divides a sentence into multiple lines for display
  • Use [], {} or () parentheses in the statement to wrap directly, without the need to use multi-line connectors
item1 = 1; item2 = 2; item3 = 3
total = item1 + \
        item2 + \
        item3;
print total

days = ['Monday', 'Tuesday', 'Wednesday',
        'Thursday', 'Friday']
print days[0]

Six, Python quotation marks, used to indicate strings, comments (multiple lines)

1. Single quotation mark (')

2. Double quotation marks (")

3. Three quotation marks ("'or "" ")

The usage of single quotation marks is the same as double quotation marks. Python supports two ways of writing. The advantages are as follows: 

  • Represents the string Let's go
  • Single quotation mark, need to escape characters:'Let\' go'
  • Double quotes, no need to escape characters: "Let's go"

4. When the string requires multiple lines, there are three differences:

  • Single quotation marks and double quotation marks indicate multiple lines, and a newline connector is required /
  • Three quotes wrap directly

Seven, Python comments

  1. Single line comment, use # to indicate
  2. Multi-line comments, using triple quotation marks (3 single quotation marks, 3 double quotation marks)
  3. Python blank lines, code indentation 
  • Use blank lines to separate functions or class methods;
  • Use a blank line to separate the class and the function entry to highlight the beginning of the function entry;
  • Blank lines are not part of the python syntax. Even if they are not separated by blank lines, the Python interpreter will not report an error. The blank lines are separated for better code structure and better code maintenance.
  • Indentation is part of python syntax
  • Remember: blank lines are also part of the python program

Eight, print output

  1. The default output of print is newline
  2. To achieve no line break, add a comma after the variable,
x="Hello"
y="World!"
#默认换行
print x
print y
#不换行
print x,
print y

Nine, Python waits for user input

  1. Get user input string
  2. \n\n" will output two new blank lines before the result is output;
  3. Once the user presses the enter key to exit
enter_string = raw_input("\n\nPress the enter key to exit.")
print  enter_string

Ten, Python multiple statements form a code group

  1. Code group: a code block composed of a group of statements with the same indentation;
  2. For compound statements like if, while, def, and class, the first line starts with a keyword and ends with a colon (: ). One or more lines of code after this line constitute a code group;
  3. The first line and the following code group is called a clause (clause), for example:
 if expression :
   suite
 elif expression :
   suite
 else :
   suite
myAge = 25
if myAge < 25:
    print("我的年龄小于25")
elif myAge > 25:
    print("我的年龄大于25")
elif myAge == 25:
    print("我今年25岁")
else:
    print "我也不知道你多少岁了……"

Eleven, Python command line parameters

  1. When executing Python in script form, you can receive parameters input from the command line
  2. View the parameters that can be passed in: python -h
  3. Use the sys module to get the incoming parameters as follows:
    import sys
     print sys.argv
    #sys.argv[0] 代表文件本身路径,所带参数从 sys.argv[1] 开始

 

Guess you like

Origin blog.csdn.net/weixin_38452841/article/details/108367369