Python3 命名空间
Python3 命名空间 #!/usr/bin/python3 Money = 2000 def AddMoney(): # Uncomment the following line to fix the code: # global Money Money = Money + 1 print (Money) AddMoney() print (Money)
阅读全文 ↗Python3 命名空间 #!/usr/bin/python3 Money = 2000 def AddMoney(): # Uncomment the following line to fix the code: # global Money Money = Money + 1 print (Money) AddMoney() print (Money)
阅读全文 ↗Python3 执行 Import #!/usr/bin/python3 # Fibonacci numbers module def fib(n): # return Fibonacci series up to n result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a + b return result if __name__ == "__main__": f = fib(100) print(f)
阅读全文 ↗Python3 Import 语句 #!/usr/bin/python3 # Fibonacci numbers module def fib(n): # return Fibonacci series up to n result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a + b return result >>> from fib import fib >>> fib(100) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
阅读全文 ↗Python3 全局和局部变量 #!/usr/bin/python3 total = 0 # This is global variable. # Function definition is here def sum( arg1, arg2 ): # Add both the parameters and return them." total = arg1 + arg2; # Here total is local variable. print ("Inside the function local total : ", total) return total # Now you can call sum function sum( 10, 20 ) print ("Outside the function global total : ", total )
阅读全文 ↗Python3 Return 语句 #!/usr/bin/python3 # Function definition is here def sum( arg1, arg2 ): # Add both the parameters and return them." total = arg1 + arg2 print ("Inside the function : ", total) return total # Now you can call sum function total = sum( 10, 20 ) print ("Outside the function : ", total )
阅读全文 ↗Python3 匿名函数 #!/usr/bin/python3 # Function definition is here sum = lambda arg1, arg2: arg1 + arg2 # Now you can call sum as a function print ("Value of total : ", sum( 10, 20 )) print ("Value of total : ", sum( 20, 20 ))
阅读全文 ↗Python3 可变参数 #!/usr/bin/python3 # Function definition is here def printinfo( arg1, *vartuple ): "This prints a variable passed arguments" print ("Output is: ") print (arg1) for var in vartuple: print (var) return # Now you can call printinfo function printinfo( 10 ) printinfo( 70, 60, 50 )
阅读全文 ↗Python3 默认参数 #!/usr/bin/python3 # Function definition is here def printinfo( name, age = 35 ): "This prints a passed info into this function" print ("Name: ", name) print ("Age ", age) return # Now you can call printinfo function printinfo( age = 50, name = "miki" ) printinfo( name = "miki" )
阅读全文 ↗Python3 关键字参数 #!/usr/bin/python3 # Function definition is here def printinfo( name, age ): "This prints a passed info into this function" print ("Name: ", name) print ("Age ", age) return # Now you can call printinfo function printinfo( age = 50, name = "miki" )
阅读全文 ↗Python3 运算符优先级 #!/usr/bin/python3 a = 20 b = 10 c = 15 d = 5 print ("a:%d b:%d c:%d d:%d" % (a,b,c,d )) e = (a + b) * c / d #( 30 * 15 ) / 5 print ("Value of (a + b) * c / d is ", e) e = ((a + b) * c) / d # (30 * 15 ) / 5 print ("Value of ((a + b) * c) / d is ", e) e = (a + b) * (c / d) # (30) * (15/5) print ("Value of (a + b) * (c / d) is ", e) e = a + (b * c) / d # 20 + (150/5) print ("Value of a + (b * c) / d is ", e)
阅读全文 ↗Python3 关键字参数 #!/usr/bin/python3 # Function definition is here def printme( str ): "This prints a passed string into this function" print (str) return # Now you can call printme function printme( str = "My string")
阅读全文 ↗Python3 必需参数 #!/usr/bin/python3 # Function definition is here def printme( str ): "This prints a passed string into this function" print (str) return # Now you can call printme function printme()
阅读全文 ↗