Python scripts are widely used. In this process, we will see various problems. In fact, you only need to study it carefully. Next we will take a detailed look at the relevant technical information. I hope you will have some gains.
Currently, many third-party scripting languages are available for direct use, such as Tcl and Lua. This article describes Python scripts. Python has more than a decade of history. It is an explanatory and object-oriented scripting language. Python interpreters can run on most operating systems, such as Windows, Linux, Solaris, and Mac.
1. installation and configuration
The latest version of Python script is 2.3.2. This article uses 2.2.2. After running the installation program, it will install the Python interpreter, documentation, extension module, and so on to your computer. After the installation is complete, the Python Graphical Editor (IDLE, but Chinese characters are not supported in the current version), Python command line interpreter, and user manual will be displayed in the Start Menu.
To call the Python API function in a C ++ program, you need to add the header file and lib path to the search directory of VC ++, the header file path is the include directory under the Local Python installation directory, and the lib path is the libs directory under the Local Python installation directory. Note that the installation package only provides the lib and dll of the release version. If debugging is required, you must download the Python source code to compile the lib and dll of the debug version.
2. Syntax Introduction
For detailed syntax instructions, refer to the documentation that comes with the Python script installation package. Here I will only introduce some common keywords and precautions.
Python does not include {And} in C ++. It is replaced by indentation. Variables do not need to be declared separately, but cannot reference unassigned variables.
The Python script introduces the concept of a module, similar to the concept of Library in C ++. A module can contain functions, variables, and classes. A script file is a module that needs to be imported before use. Python does not have a switch. if is used instead:
- if ( num==1 ):
- print "1"
- elif ( num==2 ):
- print "2"
- else:
- print "unknown"
While is a loop Statement of Python. In the while LOOP, you can use continue to jump to the next loop, and use break to jump out of the entire loop:
- cnt = 5
- while ( cnt > 0 ):
- print cnt
- cnt -= 1
For Loop:
- list = ["test1", "test2", "test3"]
- for str in list:
- print str
A dictionary is a type of ing data for Python scripts. It can be mapped from a key to the actual content (value ):
- accounts = {'tom':'123456', 'mike':'654321'}
- print accounts['tom']
- print accounts['mike']
The above describes how to install and configure Python scripts.