"Hello World" 1 Creating hello.py 2 Writing Program: if __name__ = = ' __main__ ': print "Hello World" 3 run Program: Python./hello.py Comments 1 Whether it is a line comment or a paragraph comment, it is annotated with a # plus a space. 2 If you need to use the Chinese annotation in your code, you must precede the Python file with the following note: #-*-Coding:utf-8-*-3 The following comment is used to specify the interpreter #! /usr/bin/python "File Type" 1 python file types are divided into 3 categories, namely source code, byte code, and optimized code. These can be run directly and do not need to be compiled or connected. 2 source code is extended with. PY, which is explained by Python; 3 source files are compiled to generate a. pyc file with a compiled byte file. This file cannot be modified using a text editor. PYC files are not platform-independent and can be run on most operating systems. The following statements can be used to produce PYC files: Import Py_compile Py_compile.compile (' hello.py ') 4 the optimized source file will have the. pyo suffix, that is, optimizing the code. Nor can it be modified directly with a text editor, as the following command can be used to generate PYO files: Python-o-M Py_complie hello.py Variable 1 The variables in Python do not need to be declared, and the assignment of variables is manipulated even though the variable is declared and defined by the procedure. 2 A new assignment in Python creates a new variable. A variable's identity is not the same, even if the variable has the same name. Use the ID () function to get the variable ID: x = 1 Print ID (x) x = 2 Print ID (x) 3 If the variable is not assigned, Python thinks the variable does not exist 4 variables defined outside a function can be called global variables. Global variables can be accessed by any function and external file inside the file. 5 Global variables are recommended at the beginning of a file. 6 You can also place the global variable in a specialized file and then refer to it by import: The contents of the gl.py file are as follows: _a = 1 _b = 2 use_global.py in reference to global variables: Import GL def fun (): Print Gl._a Print Gl._b Fun () constant There are no reserved words in Python that define constants. You can define a constant class to implement the functionality of a constant. ? class _const: Class Consterror (TypeError): Pass def __setattr__ (Self,name,vlaue): If Self.__dict__.has_key (name): Raise self. Consterror, "Can ' t rebind const (%s)"%name Self.__dict__[name]=value Import Sys Sys.modules[__name__]=_const () "Data Type" 1 Python's numeric types are integral, long, floating-point, boolean, and plural. 2 python has no character type 3 There is no normal type within Python, and any type is an object. 4 If you need to see the type of a variable, you can use the type class, which can return the type of the variable or create a new one. 5 Python has 3 ways of representing string types, single quotes, double quotes, and triple quotes. Single and double quotes have the same effect. Python programmers prefer to use single quotes, C/java programmers are accustomed to using double quotes to denote strings. In three quotes, you can enter characters such as single quotes, double quotes, or newline.
|