SoFunction
Updated on 2024-12-20

Four simple uses of function parameters in python3

The following gives you four simple uses of function parameters in python3, as shown below:

def print_two(*args):
  arg1, arg2 = args
  print "arg1: %r, arg2: %r" % (arg1,arg2)
def print_two_again(arg1, arg2):
  print "arg1: %r, arg2: %r" % (arg1, arg2)
def print_one(arg1):
  print "arg1: %r" % arg1
def print_none():
  print "I got nothin"
print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()

default parameter

Look at the following code

def stu_register(name,age,country,course):
  print("---- Registered Student Information ------")
  print("Name:",name)
  print("age:",age)
  print("Nationality:",country)
  print("Course:",course)
stu_register("Wang Shan Gun",22,"CN","python_devops")
stu_register("Zhang Baochun",21,"CN","linux")
stu_register("Liu Lao Gen",25,"CN","linux")

The country parameter is basically "CN", just like when we register a user on a website, if you don't fill in the nationality information, it will be China by default, which is realized by the default parameter, and it's very simple to make country the default parameter.

def stu_register(name,age,course,country="CN"):

This way, this parameter is not specified in the call, then the default is CN, and if it is specified, the value you specify is used.

Also, you may have noticed that after making country the default parameter, I simultaneously moved its position to the end, why?

Key parameters

Under normal circumstances, to pass parameters to the function to be in order, do not want to be in order, you can use the key parameter, just specify the parameter name can be, but remember a requirement is that the key parameter must be placed in the position of the parameter after the parameter.

stu_register(age=22,name='alex',course="python",)

nonstationary parameter

If your function is not sure how many parameters the user wants to pass in at the time of definition, you can use non-fixed parameters

def stu_register(name,age,*args): # *args will turn multiple incoming arguments into a tuple
  print(name,age,args)
stu_register("Alex",22)
# Output
#Alex 22 () #This () is args, but it's empty because it's not passed a value.
stu_register("Jack",32,"CN","Python")
# Output
# Jack 32 ('CN', 'Python')

There could also be a **kwargs

def stu_register(name,age,*args,**kwargs): # *kwargs will turn incoming arguments into a dict
  print(name,age,args,kwargs)
stu_register("Alex",22)
# Output
#Alex 22 () {}# This {} is kwargs, but it's empty because it's not passed a value.
stu_register("Jack",32,"CN","Python",sex="Male",province="ShanDong")
# Output
# Jack 32 ('CN', 'Python') {'province': 'ShanDong', 'sex': 'Male'}

local variable 

name = "Alex Li"
def change_name(name):
  print("before change:",name)
  name = "The Golden Horn, a man with a Tesla."
  print("after change", name)
change_name(name)
print("Look outside to see if NAME has changed?",name)

exports

before change: Alex Li
after change the Great Golden Horn (mythology),One withTeslamen

Check outside to see if name has been changed? Alex Li

Global and Local Variables

Variables defined in a subroutine are called local variables, and variables defined at the beginning of a program are called global variables.
The global variable scope is the entire program, and the local variable scope is the subroutine in which the variable is defined.
When a global variable has the same name as a local variable:
Local variables act within the subroutine in which they are defined; global variables act elsewhere.

summarize

The above is a small introduction to the four simple uses of function parameters in python3, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. Here also thank you very much for your support of my website!