Python's official website links in various functions described: https: //docs.python.org/3/library/functions.html
A few common words:
- def meaning (ie define, definitions) is to create a function, that is the definition of a function.
- Arg (ie, argument, parameter) sometimes you can still see: parameter such an approach
- return return results
咒语: Define a function named 'function' which has two arguments : arg1 and atg2, returns the result——'Something'
note:
1) def and return is the key word (keyword)
2) in brackets after the closing of the colon is essential, and is in English
3) After the IDE colon carriage, automatically get an indented. Indent function is called back statements statement block (Block), the indentation is to demonstrate and affiliation logic statements, is one of the remarkable features of the Python.
4) must have a return, if we remove the last function return into a direct output (hereinafter, conversion temperature for example), will find that there is a plurality None. This is because the print function is a function designed by man, the value is 95.0 ° F actually generated after the function call, and C2F None variable values are returned. This is like
def fahrenheit_converter (C): fahrenheit = C * 9/5 + 32
# return fahrenheit print (str (fahrenheit) + ' ° F ' )
C2F = fahrenheit_converter (35)
print (C2F)
#运行结果
# 95.0 ° F
# None
Can not write return successfully define a function and use, but the return value is 'None' Bale.
Problem one: a weight transducer design, in order to enter the "g" is converted into the number returned in units of "kg" results.
def kilogram_converter(G): kilogram = G / 1000 return kilogram G2K = kilogram_converter(500000) print(G2K)
Problem two: a function of design requirements hypotenuse length (as a function of right-angle side, find the longest side)
import math def calculate_bevel_edge(a, b): c = math.sqrt(a*a + b*b) return c edge = calculate_bevel_edge(3, 4) print(edge)
(I found out that my code appears faint yellow line, but do not know the reason, PEP8)
| Pass parameters and parameter types
Passing parameters in two ways: a position parameter, parameter Image
To seek hypotenuse above as an example, correct wrong input mode:
edge = calculate_bevel_edge (3, 4) // first Edge = calculate_bevel_edge (A =. 3, B =. 4) // second Edge = calculate_bevel_edge (A =. 3,. 4) // third Edge = calculate_bevel_edge (B = 4, 3) // error
One proposed design sensitive word filters:
First, master the basic usage of open and write
1. Create a file test.txt on the desktop
2, the use of open open. Because my IDE disk in E, and my test file on the desktop, so I wrote a C drive represents the position.
file = open('C:/Users/asus/Desktop/test.txt','w') file.write('Hello world')
Second, the design function
Incoming msg parameter name and can control the file name in the function test and write the contents of the desktop, which is not need to return may also play a role in the function
def test_create(name, msg): desktop_path = 'C:/Users/asus/Desktop/' full_path = desktop_path + name + '.txt' file = open(full_path,'w') file.write(msg) file.close() print('Done') test_create('hello','hello world')
Third, sensitive word filtering function
def text_filter(word, cencored_word = 'lame', changed_word = 'Awesome'): return word.replace(cencored_word, changed_word) text_filter('Python is lame!')
Fourth, the above two functions merge
def text_filter(word, cencored_word = 'lame', changed_word = 'Awesome'): return word.replace(cencored_word, changed_word) def test_create(name, msg): desktop_path = 'C:/Users/asus/Desktop/' full_path = desktop_path + name + '.txt' file = open(full_path,'w') file.write(text_filter(msg)) file.close() print('Done') test_create('hello','Python is lame!')
Finally, Python can solve some math problems symbols used