Python's function learning

Source: Internet
Author: User
Tags variable scope

Functions and procedures are functions that have no return value, both of which can be called

in python , the function return value is The value after return, and the procedure return value is None

Programming: Object-oriented, process-oriented, functional programming

Process-oriented programming: It is a small process defined by a def that is stitched together into a piece

Functional Programming:F =2x The mathematical function has an x, there will be a corresponding f - value such as:lisp, Erlang

For example , a function like the Python language is a true or false judgment, that is, an x does not necessarily correspond to an F

The following example: To calculate (1+2) *3-4

Traditional way:

var a =1+2;

var b =a*3;

var c=b-4;

Functional programming requires functions, which we can define as different functions, and then write the following styles:

var result =subtract (ultiply (Add (1+2), 3), 4)

This is functional programming.

Three great benefits of using functions:

reusability, consistency, and extensibility of code

returned value return

returns 0 values ( no return value ): Return to none

returns 1 values: returned as object (all data types in python are objects)

return multiple values : returns a tuple(tuple)

If this is: the return function now results in the address of the function

writing python files from Linux

Way:

First: Create a file vim test.py

Next: Write a program in the Vim file editor

Eg:def test2 (X,*args,**kwargs): print (x)
Print (args)
Print (Kwargs)
Test2 (" haha ", 4,sex = "F", name = "Zhangsan")

Then : Press:WQ Complete program writing

finally : results can be run by writing python test.py in terminal

The process of writing a function:

Define and write the function first

In writing the arguments of a function for delivery

Run the process: read the actual parameters first, and then pass the actual parameters to the function for processing

# Local Variables but for lists, dictionaries, collections, classes can be changed in local variables
School = "Oldboy" # global variable
def test4 (name):
Global School # The keyword allows you to change globals within a function
School = "Mage Linux"
Name = "Lisi" # Default: This function is the scope of the variable scope can be changed by the global keyword
Print (Name,school)

Name = "Zhangsan"
TEST4 (name)
Print (name)
Print (school)

Python's function learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.