Home > other >  High school students zero based learning python (4)
High school students zero based learning python (4)

Time:10-24

learning python
Starting today, we need to start learning function, function? In a computer also has a function?
Yes, like the function in the mathematics, have abstractness,

I'd like to add from 1 to 100, for example, which function is more convenient to use? As is known to all, we all learned a symbolic math, high school that it looks like ∑, yes it is the summation notation Sigma,


reference
yes this is the summation formula from + 1 to 100, forgive me for not using HTML code to be typed, consult!


When we see this formula, operations in the history of thought must be tall, rather than a simple low-level addition 1 + 2 + 3 + 4... + 99 + 100

Through the function we don't need to care too much about the underlying calculation process, and focus on a higher level thinking,


Python built-in a lot of function, we can directly call out

When we want to call a function of time, you know? As we buy something, must want to know the name of the commodity and role, and in the python function, so we want to know the name of the function and parameters, such as function for the absolute value of abs , there is only one parameter,

reference
let's call the abs function try

 & gt;> Abs (1) 
1
> Abs (1)
1

Because the function abs only one parameter, when we enter two function program will be an error
 & gt;> Abs (1, 2) 
Traceback (the most recent call last) :
The File "& lt; Stdin>" , line 1, in
TypeError: abs () takes exactly one argument given (2)

That if the parameters are correct, we input parameter types is wrong? For example, we input string a
 & gt;> Abs (' a ') 
Traceback (the most recent call last) :
The File "& lt; Stdin>" , line 1, in
TypeError: bad operand type for abs () : 'STR'

Note unlike the above error, an error message: STR is wrong parameter types:

Like Max and min function Max (), min () can put any number of functions,

With Max function, for example,
 & gt;> Max (1, 2, 3) 
> 3
> ,3,5, Max (- 1-7)
> 5

data type conversion
python can in turn. In function of the data type, like int (), flaot ()...
 & gt;> Int (' 123 ') 
123
> Int (13.14)
13
> Float (' 13.14 ')
13.14
> STR (1.23)
'1.23'
> STR (100)
'100'
> Bool (1)
True
> Bool (' None ')
False

Of course you can also give a function "nickname", for example:
 & gt;> B=abs # variable pointing to the abs function b 
> B (1) # so can b abs function called

Reference
function name is pointing to a function object references, can put the function name is assigned to a variable


Already late at night, it's too late today is write here, want to study together friends can focus on my blog, everyone go to bed early, good night!
  • Related