Python Learning--functions__python

Source: Internet
Author: User
Tags abs function definition
Preface:

This section describes how to customize functions, call functions, and use functions in function libraries


1. Custom Function

A, Format: def + function name (parameters):

def hello_world (word):

Note that there are colon colons: B, optional function annotations

"" "Prints ' Hello world! ' to the console." "

C, Function body

Word = "Hello world!"
Print (word)
Organize the above code as follows:
def hello_world (word): "" "
	prints ' Hello world! ' to the console.
	" " Word = "Hello world!"
	Print (word)


Note: In order to be aesthetically pleasing, the annotation and function body should be distinguished from the function definition line, that is, by using a space indent


2, Call function function name (parameter)

HelloWorld ("I Love python!")

3. function Call FunctionIn line with C + +, call another function directly inside the function
def cube (number): Return
    number*number*number

def by_three (number):
    if (number% 3) = = 0: return
        Cube ( Number)
    else: return
        False

4. module function LibraryImport Module
For example, to use the Math function library, you need to add the import math before the program
Import Math
print math.sqrt
#输入结果为5

The above only uses the mean square function, but uses the MATH.SQRT () the longer expression, appears verbose.
You can use the From module import function, the From math import sqrt, to simplify the invocation expression, that is, sqrt ().
From math import sqrt
print sqrt (25)

5, Universal ImportsTo make it easy to invoke functions in a function library, you can add the From module import * expression
To invoke all functions in the Math function library, you can use the following program code
From Math Import *

6, built-in functions
def biggest_number (*args):
    print max (args) return
    max (args)
    
def smallest_number (*args):
    print min ( args) return
    min (args)

def distance_from_zero (ARG):
    print ABS (ARG) return
    ABS (ARG)


Biggest_ Number ( -10, -5, 5,)
Smallest_number ( -10, -5, 5, ten)
Distance_from_zero (-10)

Function Analysis:
Max ()--Returns the maximum value in a set of integer or floating-point values
Maximum = max (1,2,3,8.8,9.9,4,7,6,9)

print maximum
Min ()--Returns the minimum value in a set of integer or floating-point values
Minimum = min (1,2,3,0.5)

Print minimum
ABS ()--Returns the absolute value of the input value
Absolute = ABS ( -99)

Print Absolute
Type ()--Returns the types of data
Print type        #int
print type (99.9)      #float
Print type (' Python ')  #str

These functions are all built-in functions, that is, you do not need to add a function library

7, ReviewIf/elif/else
def  Shut_down (s):
    if s = = "Yes": Return
        "shutting down"
    elif s = = "No": Return
        "Shutdown aborted" C12/>else: Return
        "Sorry"

Modules
Import Math
Print math.sqrt (13689) #结果是117.0

Built-in functions
def distance_from_zero (number):
    if Type (number) = int or type (number) = = float: return
        ABS (number)
    else : Return
        "Nope"

8, actual combat program--taking a vacation
8.1 Planning Your Trip
def hotel_cost (nights): Return
    140*nights

8.2 Getting There
def plane_ride_cost (city):
    if City = = "Charlotte": Return
        183
    elif City = = "Tampa": Return
    elif City = = "Pittsburgh": Return
        222
    elif City = = "Los Angeles": Return
        475

8.3 Transportation
def rental_car_cost (days):
    If->= 7: Return
        days*40-50
    elif days >= 3: Return
        days*40-20 return
    days*40

8.4 Together
def hotel_cost (nights): Return
    140*nights
    
def plane_ride_cost (city):
    if City = = "Charlotte":
        return 183
    elif City = = "Tampa": Return
    elif city = "Pittsburgh": Return
        222
    elif city = "Los Angeles": Return
        475
    
def rental_car_cost (days):
    if >= 7: Return
        days*40-50
    elif days >= 4/>return days*40-20 return
    days*40
    

def trip_cost (city,days): Return
    Hotel_cost (days) + Plane_ride_ Cost (city) + Rental_car_cost (days)

8.5 plan Your tripsWhat if we went to Los Angeles for 5-and-brought-extra-dollars-money?
def hotel_cost (nights): Return
    140*nights
    
def plane_ride_cost (city):
    if City = = "Charlotte":
        return 183
    elif City = = "Tampa": Return
    elif city = "Pittsburgh": Return
        222
    elif city = "Los Angeles": Return
        475
    
def rental_car_cost (days):
    if >= 7: Return
        days*40-50
    elif days >= 4/>return days*40-20 return
    days*40
    

def trip_cost (City,days,spending_money): Return
    Hotel_cost (days + plane_ride_cost (city) + rental_car_cost (days) + Spending_money
    
print ("Above costs%d dollars!"% trip_cost ("Los A Ngeles ", 5, 600))


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.