Python Learning notes (4)

Source: Internet
Author: User

Functions of Python:

function, is a reusable program segment, we can use the call function to enable the program to achieve the same functionality, thereby greatly reducing the workload.

We can implement multiple methods in a function, by invoking various methods, to implement various functions.

The definition of a function in Python:

def function name ():

Function

When called, only the function name () or functions name is output. method () .


Cases:

Cat sayhello.py

#!/usr/bin/env python

#filename SayHello

def hello ():

print "Hello World!!!"


Def saygood ():

Print "good!!"

Version = ' 1.0 '


Hello ()

Saygood ()

Print "This version is%s"% version


This is a module where we can implement its function by invoking the method


Cat demosay.py


#!/usr/bin/env python

#filename Demosay

Import SayHello


Sayhello.hello ()

Sayhello.saygood ()

Sayhello.version


In this way, we can execute the method inside the sayhello.py to realize its function.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/75/13/wKiom1Yx8Z3AgWfPAAD7PTsXlJM657.jpg "title=" 111. PNG "alt=" wkiom1yx8z3agwfpaad7ptsxljm657.jpg "/>

Hey?!! The function is implemented, but why do we first execute the contents of sayhello.py first and then execute the function we call?

This is because the program will be initialized when loading the module, the initialization of the time, it will call the method, so that the method in our SayHello will print out through print, here is a hint, the method's return value is best not to print the output with print, but with return To return the value, only when the call to print out, it will not appear above the situation!

Then, our SayHello should be modified:

Sed-i ' s/print/return/g ' sayhello.py

Sed-i ' 4,6 s/sayhello/print sayhello/' demosay.py

The following changes are included:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/75/15/wKiom1Yx-OjgYac0AAGTCfhK90o095.jpg "style=" float: none; "title=" 222.png "alt=" Wkiom1yx-ojgyac0aagtcfhk90o095.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/75/13/wKioL1Yx-R7z5l4cAAE3P5VXMOM832.jpg "style=" float: none; "title=" 333.png "alt=" Wkiol1yx-r7z5l4caae3p5vxmom832.jpg "/>


And then execute: Python demosay.py

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/75/13/wKioL1YyAEaCvPPMAADa8QnHLxc469.jpg "title=" 66.png "alt=" Wkiol1yyaeacvppmaada8qnhlxc469.jpg "/>


Okye!!!

This article is from the "Ride a Pig to Travel" blog, please be sure to keep this source http://songqinglong.blog.51cto.com/7591177/1707860

Python Learning notes (4)

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.