今天说到全局变量和本地变量,老外老师说了,全局变量就好像好莱坞的明星一样,每个人都知道。而本地变量需要声明才能用,不然人们不知道。
#The function,jennifer is local
def God();
global Jennifer
Jennifer='pretty'
#Main program,always global,Hollywood becomes famous,and everybody knows
God()
print Jennifer
这段输出的结果是:pretty
#The function,jennifer is local
def God();
global Jennifer
Jennifer='pretty'
#Main program,always global,Hollywood becomes famous,and everybody knows
Jennifer='beautiful'
God()
这段输出的结果是:beautiful
知道原因么??因为Jennifer是全局变量了,所以后面本地变量一律无效。。。只认前面的。
顺便老师还说了各种变量的类型。。。。。
>>> number =5
>>> fl=4.3
>>> sentence ='you have smelly boots'
>>> b= False
>>> type(number)
<type 'int'>
>>> type(fl)
<type 'float'>
>>> type(sentence)
<type 'str'>
不过我有个疑问是?
>>> 2.34
2.3399999999999999
2.34怎么等于2.3399999999999999??
虚心向各位兄弟姐妹叔叔阿姨请教。