Python learns day-5 variables and recursive functions

Source: Internet
Author: User

First, local variables and global variables

Name= ' LHF ' global variables


Def change_name ():
Global name globally variable without global is local variable
Name= ' Handsome a ratio '
Print (' Change_name ', name)


Change_name ()
Print (name)

Name= ' LHF '

Def change_name ():
Name1= ' Handsome a ratio '
Name2= ' Handsome a ratio '
Name3= ' Handsome a ratio '
Print (' Change_name ', name)

Globals keywords

NAME = "Slash"

Def Yangjian ():
Global NAME
NAME = "Little Northeast"
Print (' I'm going to do ', NAME)

Def Qupengfei ():
Print (' I'm going to do ', NAME)

Yangjian ()
Qupengfei ()
--------------------------------------------------


NAME = "Product Manager"

Def Yangjian ():
# NAME = "History text"
Global name # has declared that name is the global variable
Print (' I'm going to do ', NAME)
NAME = "Little Northeast" # Modify global variables
Print (' I'm going to do ', NAME)

Def Qupengfei ():
NAME = "Base"
Print (' I'm going to do ', NAME)

If the contents of the function have no global keyword,
-have declared local variables
NAME = ["Product Manager", "Riopo Wet"]
Def Qupengfei ():
NAME = "Self"
Print (' I'm going to do ', NAME)
Qupengfei ()
-No declaration of local variables
NAME = ["Product Manager", "Riopo Wet"]
Def Qupengfei ():
Name.append (' Xxoo ')
Print (' I'm going to do ', NAME)
Qupengfei ()

If the contents of the function have the global keyword
-have declared local variables
NAME = ["Product Manager", "Riopo Wet"]
Def Qupengfei ():
Global NAME
NAME = "Self"
Print (' I'm going to do ', NAME)
Qupengfei ()
Error example
NAME = ["Product Manager", "Riopo Wet"]
Def Qupengfei ():
NAME = "Self"
Global NAME
Print (' I'm going to do ', NAME)
Qupengfei ()
-No declaration of local variables
NAME = ["Product Manager", "Riopo Wet"]
Def Qupengfei ():
Global NAME
NAME = ["Mao"]
Name.append (' Xxoo ')
Print (' I'm going to do ', NAME)
Qupengfei ()

####### global variable name capitalization
####### local variable variable name lowercase


Local variables are read first, global variables can be read, global variables cannot be re-assigned name= "FFF", but for mutable types, internal elements can be manipulated
If there is a global keyword in the function, the variable is essentially the global variable and can read the assignable name= "FFF"





NAME = ' Sea Breeze '

Def Huangwei ():
Name = "Huang Wei"
Print (name)
Def Liuyang ():
Name = "Liu Yang"
Print (name)
Def nulige ():
name = ' Prev Flower '
Print (name)
Print (name)
Nulige ()
Liuyang ()
Print (name)

Huangwei ()


Name = "Just Niang"

Def Weihou ():
Name = "Chen Zhuo"
Def Weiweihou ():
nonlocal name # nonlocal, specify the upper-level variable, and if not, continue up until you find it
Name = "Cool"

Weiweihou ()
Print (name)

Print (name)
Weihou ()
Print (name)








Second, forward reference

Example one: Error
def foo ():
Print (' from foo ')
Bar ()

Foo ()
Example 2
def bar ():
Print (' from Bar ')
def foo ():
Print (' from foo ')
Bar ()

Foo ()


Example 3
def foo ():
Print (' from foo ')
Bar ()

def bar ():
Print (' from Bar ')
Foo ()
Example 4: Error
def foo ():
Print (' from foo ')
Bar ()

Foo ()

def bar ():
Print (' from Bar ')

1
Def calc (n):
Print (n)
if int (N/2) = = 0:
return n
Res=calc (int (N/2))
return res



2
Import time

Person_list = [' Alex ', ' Wupeiqi ', ' Linhaifeng ', ' ZSC ']


def ask_way (person_list):
Print ('-' * 60)
If Len (person_list) = = 0:
Return ' No one Knows '
person = person_list.pop (0)
if person = = ' Linhaifeng ':
Return '%s ' said: I know, the old boy is in Shahe Tak Commercial building, under the subway is '% person

Print (' Hi-man [%s], dare to ask where the road is '% person ')
Print ('%s ' replied: "I don't know, but read your eyes to the pig, you wait, I help you ask%s ... '% (person, person_list))
Time.sleep (1)
res = Ask_way (person_list)

Print ('%s ' results were:%res '% (person, res))
return res


res = Ask_way (person_list)
Print (RES)





Python learns day-5 variables and recursive functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.