Explain the custom functions in Python with the story of "restaurant packaging"

Note: The blogger is not a Python professional programmer. He is 12 years old and Python is less than 1 year old. πŸ˜ƒ Hope to help some Python novices through this column!

Hi~ Hello everyone! As we said in the last blog post, if there are some functions that can only be implemented with tens of thousands of lines without a library, you can only write them yourself, then how to simplify the process of copying and pasting tens of thousands of lines?

What is a function?


Well, don't worry, the function is actually we go to the restaurant to eat and ask the waiter to pack.

No no no, that's it, at least in Python. Let's illustrate with an example.

standard name practical meaning
function Pack
inside the function packaged food

How to define a function

Well, let’s talk about how to define a function first.

def blablabla():
    print('I am the content of the function! Oye!')
    a = True
    while a:
        print('Follow me now!')
        a = False

A few points to note

1. Naming conventions for functions

same as variable

  1. To be understood.
  2. The function name must be a combination of English letters, numbers and underscores. Numbers cannot be the first character of variable names.
  3. Variable names also cannot have the same names as Python built-ins (that is, built-in functions). Otherwise, Python students should be confused again.

2. Punctuation

The following format must be followed when defining a function:

def functionname():

And the code inside the function must be indented. If you forget to indent or delete the indent, just press the Tab key before that line.

How to run a function

very simple~
After defining the function name, enter it directly

Function name()

For example mine: blablabla()
The result of running is:

Remember: there is one and only this way to run a function! !
At this point, you may find a problem.

Yes, so how to solve this problem?

Yes, we can put things inside parentheses that are "things" like variables, but not variables. What is this like? When you go to pack, you tell the waiter:

So, how to define such a function? let's go~

How to define a function with parameters

def blablabla(name):
    print('I am the content of the function! Oye!')
    a = True
    while a:
        print('Follow me now!',name)
        a = False

Just put the name of the variable element in parentheses, and it works the same:

How to run a function with parameters

blablabla('Tool man 001 A')

Running it will get:

I am the content of the function! Oye!
Follow me now! Tool man 001A

Pay attention to several common bugs

1. Where you give parameters, but not at runtime

Negative example:

def blablabla(name):
    print('I am the content of the function! Oye!')
    a = True
    while a:
        print('Follow me now!',name)
        

blablabla()

Error message:


Note: "Variable elements" are informally called "parameters".

2. Conversely, you have given the parameters, but not the place where the parameters are given

Negative example:

def blablabla():
    print('I am the content of the function! Oye!')
    a = True
    while a:
        print('Follow me now!',name)
        a = False

blablabla('hhh')

Error message:


Note: "Variable elements" are informally called "parameters".

How to define and run a function with multiple arguments

Hey this is interesting. In fact, it is separated by commas.

def all_about_you(name,sex,age,hobby):
    print('wow, your name is',name,'and your sex is',sex,'and you are',age,'years old and you like',hobby)


all_about_you('xiaocao162020','boy',12,'writing Python')

Running this will get:

wow, your name is xiaocao162020 and your sex is boy and you are 12 years old and you like writing Python

Ah, don't worry about the problem of no punctuation, it's just for fun, this one also has several bug s, for example, if you give 3 variable elements, only 2 variable elements are entered at runtime, or vice versa. will report errors. Let me show you another BUG, ​​to be precise, it is not a BUG:

def all_about_you(name,sex,age,hobby):
    print('wow, your name is',name,'and your sex is',sex,'and you are',age,'years old and you like',hobby)


all_about_you('writing Python','12','boy','xiaocao162020')

Run, no error, but display:

wow, your name is writing Python and your sex is 12 and you are boy years old and you like xiaocao162020

The translation will definitely kill you with laughter:


To make sure this doesn't happen, we can enter in order or like this:

def all_about_you(name,sex,age,hobby):
    print('wow, your name is',name,'and your sex is',sex,'and you are',age,'years old and you like',hobby)


all_about_you(hobby='writing Python',age='12',sex='boy',name='xiaocao162020')

Do you have a very intimate feeling?
yes! we Manipulating files with Python The code for opening the file in that article is exactly:

file1 = open('blablabla.txt',encoding='utf-8',mode='r')

That's right, exactly what you think it is! open is actually a built-in function of Python! Built-in functions and custom functions work exactly the same way!
Well, this seems to be the end, if you have any questions, please contact me~

Tags: Python

Posted by dkode on Wed, 25 May 2022 08:36:48 +0300