A Quick 10-Minute Introduction to Python
Category Programming Techniques
Introduction
Python is a dynamically interpreted programming language. Python can be used on various operating systems such as Windows, UNIX, MAC, and also on development platforms like Java and .NET.
Features
Python is developed using the C language, but it no longer has complex data types such as pointers found in C.
Python has strong object-oriented features and simplifies the implementation of object orientation. It eliminates elements such as protected types, abstract classes, and interfaces.
Python code blocks are separated by indentation using spaces or tabs.
Python has only 31 reserved words and does not have markers like semicolons, begin, or end.
Python is a strongly typed language. Once a variable is created, it corresponds to a data type. Variables of different types in the same expression require type conversion.
Setting Up the Development Environment
You can download the installation package from www.python.org and install it using configure, make, and make install.
You can also download the ActivePython component package from www.activestate.com. (ActivePython is a binary packaging of the Python core and commonly used modules. It is a Python development environment released by ActiveState. ActivePython makes the installation of Python easier and can be applied to various operating systems. ActivePython includes some commonly used Python extensions, as well as programming interfaces for the Windows environment). For ActivePython, if you are a Windows user, simply download the msi package and install it; if you are a Unix user, download the tar.gz package and unzip it directly.
Python IDEs include PythonWin, Eclipse with PyDev plugin, Komodo, and EditPlus.
Versions
Python 2 and Python 3 are the two main versions currently.
Under the following two conditions, it is recommended to use Python 2:
When you cannot fully control the environment you are about to deploy;
When you need to use some specific third-party packages or extensions;
Python 3 is the officially recommended version and will be fully supported in the future. Many functional improvements are currently only carried out on the Python 3 version.
Hello World
Create hello.py.
Write the program:
if __name__ == '__main__': print "hello world"
Run the program:
python ./hello.py
Comments
Both line comments and block comments are made with a # followed by a space.
If you need to use Chinese comments in the code, you must add the following comment at the beginning of the Python file:
# -* - coding: UTF-8 -* -
The following comment is used to specify the interpreter
#! /usr/bin/python
File Types
Python file types are divided into three types: source code, byte code, and optimized code. All of these can be run directly without the need for compilation or linking.
Source code has the .py extension and is interpreted by Python;
After compilation, the source file generates a file with the .pyc extension, which is the compiled byte file. This file cannot be modified using a text editor. The pyc file is platform-independent and can run on most operating systems. The following statement can be used to generate a pyc file:
import py_compile py_compile.compile('hello.py')
The optimized source file will have the .pyo suffix, which is the optimized code. It also cannot be directly modified with a text editor. The following command can be used to generate a pyo file:
python -O -m py_compile hello.py
Variables
Variables in Python do not need to be declared. The assignment operation of a variable is the process of declaring and defining the variable.
A new assignment in Python will create a new variable. Even if the variable names are the same, the variable identifiers are not the same. The id() function can be used to obtain the variable identifier:
x = 1 print id(x) x = 2 print id(x)
If a variable is not assigned, Python considers the variable to be non-existent.
Variables defined outside a function can be called global variables. Global variables can be accessed by any function within the file and by external files.
It is recommended to define global variables at the beginning of the file.
Global variables can also be placed in a dedicated file and referenced through import:
The content of the gl.py file is as follows:
_a = 1
_b = 2
Referencing global variables in use_global.py:
``` import gl def English: cursor=conn.cursor() sql='insert into address(name, address) values(%s, %s)' value=(("zhangsan","haidian"),("lisi","haidian")) try cursor.executemany(sql,values) except Exception as e: print e sql='select * from address' cursor.execute(sql) data=cursor.fetchall() if data: for x in data: print x[0],x[1] cursor.close() conn.close()
>
Continue your studies, please see here: Python Tutorial.
Original source: http://roclinux.cn/?p=2338
**Click here to share notes
-
-
-