Python Learning Path _python Basics (4)

Source: Internet
Author: User

Learning content:

1. Basic syntax for Python functions

2. The return value and the variable of the Python function

3. Python nesting function

4. Python recursive functions and examples (two-point lookup)

5. Python anonymous function

6. Python built-in method

7. Python instance

First, the basic syntax of the Python function

1, definition: A function refers to a set of statements by a name (function name) encapsulated, to execute this function, just call their function name to

def f (n):

Print (N**n)

Return n**2

2. Characteristics

(i) Reduce duplication of code

(ii) making the program extensible

(iii) Making procedures easier to maintain

3. Parameters

(i) Formal parameters

The parameter that is used when defining the function is called the formal parameter.

(ii) Actual parameters

The argument used when calling the function is called an argument.

(c) Default parameters

The default parameter is to pre-assign the function variable, the position of the default parameter should be placed behind

(iv) Key parameters

Key parameters must be placed behind the position parameter

(v) Non-fixed parameters

*args non-dictionary type **kwargs dictionary type

Second, the return value of the Python function and the scope of the variable

1. After the function executes, the return statement specifies what is returned. The return statement also represents the end of the function. Returns none if not specified.

2. The scope of the local variable is just the function body; the scope of the global variable is the entire program.

Third, Python nesting functions

A function is defined as a nested function.

Iv. python recursive functions and examples (two-point lookup)

The function defined in the function is the function itself, which is the recursive function.

Examples of binary lookup methods:

# data = [1,3,5,7,9,11,13,14,15,17,18,27,34,35,37,38,39,46]
# def func (list,find_num):
# middle_list = Int (len (list)/2)
# If Len (list) >0:
#
# If list[middle_list] = = Find_num:
# print (' Got it .... ', List[middle_list])
# exit ()
# if list[middle_list] > Find_num:
# List=list[:middle_list]
# print (' Going to left side ', List[:middle_list])
# func (List,find_num)
# Else:
# List=list[middle_list+1:]
# print (' Going to right side ', List[middle_list+1:])
# func (List,find_num)
# Else:
# print (' None ... ')
#
# func (data,9)

Five, Python anonymous function

anonymous function Lambda:

Res=map (Lambda X:x**x,range (10))
For I in Res:
Print (i)
Vi. built-in methods of Python
# Print (CHR (98))
# Print (ord (' B '))
# name = "Alex"
# Print (callable (SAYHI))
# Print (callable (name))
# Print (ASCII ("place"))
# Print (Bin (10))
# f = Open ("return value. Py", encoding= "Utf-8")
# Print (dir (f))
# code = Compile (F.read (), ' ', ' exec ')
# Print (code)
# EXEC (code)
# Print (Divmod (10,3))
# for I in filter (Lambda x:x>5, range):
# Print (i)
# a = Frozenset ({1,4,5,5,6})
# b = {1,4,5,5,6}
# Print (a)
# def Sayhi ():
# name= "Alex"
# print (Locals ())
# Print (Globals ()) #把当前程序所在内存里的所有数据都以字典的形式打印出来
# Print (min ([4,2,5,77,2]))
# Print (Oct (8))
# Print (POW (4,9))
# msg = "Back to the beginning"
# f = open ("ToFile", "W", encoding= "Utf-8")
# Print (msg, "Your Sentimental Face in memory", sep= "|", end= "=", file=f)
# data = "ABC"
# data = reversed (data)
# for I in Data:print (i)
# Print (round (10.26,1))
# data = [10,2,3,45,56,3]
# Print (sum (data))
# Print (VARs ())
# Print (set (data))
# a = Range (a)
# pattern = Slice (3,8,1)
# for I in A[pattern]: #等于a [3:8:2]
# Print (i)
# a = "Alex"
# a = sorted (a)
# Print (a)
Vii. examples of Python

Have the following employee information sheet

Of course, this table is what you can say when you store files

1 1,Alex Li,22,13651054608,IT,2013-04-01

Now need to this employee information file, to achieve additions and deletions to change the operation

    1. For fuzzy queries, the syntax supports at least 3 of the following:
      1. Select Name,age from staff_table where age > 22
      2. SELECT * from staff_table where dept = "IT"
      3. SELECT * from staff_table where enroll_date like "2013"
      4. Find the information, after printing, the last side to show the number of found
    2. Can create a new employee record, with the phone to do unique keys, staff_id need to increase
    3. Delete a specified employee information record, enter an employee ID, and delete
    4. To modify employee information, the syntax is as follows:
      1. UPDATE staff_table SET dept= "Market" where where dept = "IT"

Note: The above requirements, to fully use the function, please do your utmost to reduce duplication of code!

Python Learning Path _python Basics (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.