Python basic functions

Source: Internet
Author: User

The value of the integer sequence generated by range is read out one by one.

Please write it as a function to complete the 1-100 cumulative calculation
1. Read the topic, select a set of data, complete your function with fragmented code
2. Become a function Def function name (): Turn scattered code into his function body
3. Ways to improve the reusability of functions
Def sum_1 ():
sum = 0
For I in range (101):
sum + = i
Print (sum)
Sum_1 ()

Def sum_2 (M, N):
sum = 0
For I in range (M, n+1):
sum + = i
Print (sum)
Sum_2 (1, 6)
Please turn any string into a list
str_1= "Hello" list_1=[' h ', ' e ', ' l ', ' l ', ' O ']

def str_to_list_1 (str_1):
List_1 = []
For I in str_1:
List_1.append (i)
Print (list_1)

Str_to_list_1 ("string")

def str_to_list_2 (str1, str2= "Hello"):
Print (list (str1) +list (STR2))
Str_to_list_2 ("World")

Return keyword Returns a value
List_1 = [1, 2, 3, 4]
Print (List_1.pop ())
def add (A, B):
Print (A+B) #输出数据到控制台
Add (1, 2)

Def add_1 (A, B):
Return a+b #return Returns a value into your hand.
Print (Add_1 (1, 2))
1. Only the value returned by return is yours, and you can do anything for the value returned by return
The code behind the 2.return is no longer executed, and return inside the function is understood as a closing sentence
Def add_2 (A, B):
Print (A+B) #输出数据到控制台
Return A+b #返回一个值, but the value is not output to the console
Print ("Print return value returned:", Add_2 (1, 5)) #针对于return返回的值, printing output to the console

Def add_3 (A, B):
Return a+b
Print (A+B)
Print ("Print return value returned:", Add_3 (1, 2)) #针对于return返回的值, printing output to the console

Function name requirements See name
There are different letters that are separated by underscores
Letters, numbers, underscores. Cannot start with a number
Variable function name Module name naming specification is the same
Upgrade location parameters for the first time improve our reusability
Def sum_1 (A, B):
Print (A+B)
Sum_1 ()
# Call function function name ()
Second upgrade default parameter positional parameter with default value

Def sum_2 (A, B, c=1):
Print (A+B+C)
Sum_2 (1, 2, c=3)
A parameter with a default value, you can still specify its value
The third upgrade return keyword returns a value that waits for your processing
def sum_3 (b, a=2, c=6):
Return A+b+c
Print ("Printing", a+b+c) #return Subsequent statements do not execute
Print ("Prints return value", Sum_3 (1, 2, 8))

Dynamic parameters * Dynamic parameter names see the name of the known
def fun_1 (*args): #arguments
Print (args)
Fun_1 ((1, 2, 3), 4, 5, [9, 6, "Hello"], "world", 0.01, True, {"Age": 18})
Variable length parameter, you can enter any type of data, data and data directly separated by commas
Unlimited number of parameters and types
The inside of the function automatically puts these indefinite, indefinite types of data into a single tuple.
Sum of arbitrary integers
def sum_10 (*args):
sum = 0
For i in args:
sum = sum + I
Print (sum)
SUM_10 (2, 9, 6)

Def fun_3 (A, b=5, *args): # positional parameter default parameter dynamic parameter #顺序取值
Print (a)
Print (b)
Print (args)

Fun_3 (1, 2, 3, 4, 5, 6)

List type of data can be carried off the coat operation
A = [[1, 2, 3], [4, 5]] #列表类型的数据
b = [8, 9, 10]
c = [(' JADKF ')]
D = [{"Name": "Lucy"}, {"Age": 18}]
Print (*a)
Print (*B)
Print (*C)
Print (*D)
Keyword parameter key word
def fun_3 (**kwargs):
Print (Kwargs)
Fun_3 (X=1, y=2) #key-value input parameters as key-value pairs
info = {' name ': ' XY ', ' Age ': 20}
Fun_3 (**info)

Python basic functions

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.