Python study notes - 8. Function definition and use

This is a note made while learning Python, some time, probably according to a very practical entry book, I learned the general idea of ​​programming Is the basic variable type, grammar-branch structure function call class to create a structure definition, record some simple examples, the rest is what you need to use Baidu.

For me, the advantage of Python is that there is no type requirement, no declaration, no pointers, a universal array, and the library is very powerful.

我This order is a bit poor, you should first say the function definition, this is the most basic, on how to define the function, parameters and return value

# Simple definition function 
# region
def greet():
    print('hello')

 # 参数
def greet_parameter(name):
    print('welcome '+name+'!!!')

# 
def describe(name, type = 'dog'):
    print('you have a ' + type + ' name ' + name)
describe('123', 'cat')
describe('lilei')  # with default value 

@# Use the default parameter 
def getfullname(firstname, secondname):
    temp = firstname + ' ' + secondname
    return temp.title()
print(getfullname('song', 'yu'))

# to return the value 
def getnamechoose(first_name, last_name, caps_look = 0):
    full_name = first_name + ' ' + last_name
    if caps_look == 0:
        return full_name.lower()
    else:
        return full_name.title()

# Optional parameter with default placed at the end of the parameter 
def printname(namelist):
    for name in namelist:
        print('welcome ' + name + '!!')
usernames = ['hannah', 'ty', 'margot']
printname(usernames)
# List parameter 
# Prohibited list of changes 

# Input list usernames can be modified in the function input usernames[:] can only modify the copy 
# Incoming any number of parameters 
def make_pizza(*toppings):
    print(toppings)
    s = ' '
    for topping in toppings:
        s = s + topping + '_'
    print(s)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')

 #讲All parameters are encapsulated into tuples 
def meg(name, password, **userinfo):
    info = {}
    info['username'] = name
    info['userpassword'] = password
    for key, value in userinfo.items():
        info[key] = value
    return info


test = meg('songyu', '123', agent = 'moliz', age = '22')
print(test)
# endregion