Getting started with Python-using the function to advertise the installation of Python
This article is mainly suitable for people with certain programming experience, at least master a programming language to view.
Most of the examples are simple to understand English words can understand the level, mainly speaking of Python's overall use of methods and structure. Not a high-level design, it will help you get started with Python.
Python and Java control. You'll see that Python is designed to be concise, useful, and powerful, and that each program ape is worth learning and mastering.
The definition and usefulness of Python functions
A function in Python is a named block of code. As with Java, you can take 0 or more of these parameters. Main forms such as
def $函数名($參数): ... 函数体 ...
You can see that python replaces the {} in Java with indented statements, grouping the code together.
such as the basic statement in Python:
forin list: ... do something ...while true: ... do something ...if true: ...else: ...
Write a sample that prints a different result by the type of the number of parameters:
###假设是一个列表类型,则循环打印,否则打印当前def print_test(is_list): if isinstance(is_list, list): forin is_list: print(t) print_test("not list") else: print(the_list)
The list in Python can understand the list in the Java, and the tuple as an array in Java (enclosed in parentheses) seems more powerful and concise than the data. We can understand that the "chicken blood" data can be easily scaled. The relevant methods are:
Len (list)
List.insert (1, ")
List.remove (")
List.append (")
In the above example, a recursive call is used. A class that has an incoming parameter type recursively calls itself.
Can see. A def modifier was added to the method name before it was written directly.
The Python design philosophy sees everything as an object or a collection. Type does not care about what kind of interior it is. Variable identifiers do not require a type at all. In Java, you must declare a variable to indicate the type. Being able to think of Python as a high-level collection, for a list, can store different types of values, just give you a name. The rest is done by Python.
The Isinstance function in the sample is a python built-in function. Similar to instanceof in Java.
The invocation of the function. After saving method.py, F5 executes, type directly in Shell and idle:
### 句未加‘。’ 和写多行句子import method.pyprint_test(["item1","item2","item3"])
Advertising and installation of Python programs
Modular python code, like Java, can build complex and powerful systems. Block python code into a class library for easy management and easy code reuse and architecture.
Import SYS; Sys.path to see where Python is stored on the computer.
Encapsulate the example function as a module, and then advertise the installation as an example:
- Create a directory for just-written method files: method
Put the method.py in there.
- Create a new file "setup.py"
In the file for the published metadata, edit for example the following:
# 元数据fromimport setupsetup( name ‘CankingApp‘, version ‘1.0‘, py_modules = [‘method‘], author ‘CankingApp‘, ‘[email protected]‘, url ‘www.baidu.com‘, ‘test‘, )
When you are done, you will see that the directory has more build and dist directories and MANIFEST files.
Test directly in Idle:
import methodmethod.print_test(["item1","item2","item3"])
The test function call needs to add method, which is the namespace rule in Python.
All of the code in Python is associated with a namespace, and the code in the main program is associated with the "main" namespace. Our individual code modules naturally create a namespace of their own, with the same name as the code block.
So I need to bring method.print_test.
From method Import Print_test
Print_test ()
can also be used, but assume that this namespace has the same name when the conflict expires, the individual feels that the first kind of better.
A successful print out item identifies the successful installation.
The example source has been uploaded to GitHub, interested students are welcome to Exchange learning.
Getting started with Python-function usage to the advertised installation of the program