#程式碼
num_1 = 123 num_2 = num_1 # 改变num_2值前 print 'num_1 = {0}, num_2 = {1}'.format(num_1, num_2) num_2 = 0 # 改变num_2值后 print 'num_1 = {0}, num_2 = {1}'.format(num_1, num_2)
num_1 = 123, num_2 = 123 num_1 = 123, num_2 = 0
str_1 = 'hello python' str_2 = str_1 # 改变str_2值前 print 'str_1 = {0}, str_2 = {1}'.format(str_1, str_2) str_2 = 'hello' # 改变str_2值后 print 'str_1 = {0}, str_2 = {1}'.format(str_1, str_2)
str_1 = hello python, str_2 = hello python str_1 = hello python, str_2 = hello
鍊錶
l_1 = [1, 2, 3, 4] l_2 = l_1 print 'l_1 = {0}, l_2 = {1}'.format(l_1, l_2) l_2[0] = 100 # 改变l_2的第一个元素 print 'l_1 = {0}, l_2 = {1}'.format(l_1, l_2) l_2 = [1,1,1] # 改变l_2的全部元素 print 'l_1 = {0}, l_2 = {1}'.format(l_1, l_2)
l_1 = [1, 2, 3, 4], l_2 = [1, 2, 3, 4] l_1 = [100, 2, 3, 4], l_2 = [100, 2, 3, 4] l_1 = [100, 2, 3, 4], l_2 = [1, 1, 1]
字典
d_1 = {'a': 1, 'b': 2, 'c': 3} d_2 = d_1 print 'd_1 = {0}, d_2 = {1}'.format(d_1, d_2) d_2['c'] = 10 print 'd_1 = {0}, d_2 = {1}'.format(d_1, d_2) d_2 = {'e': 12, 'f': 15} print 'd_1 = {0}, d_2 = {1}'.format(d_1, d_2)
d_1 = {'a': 1, 'c': 3, 'b': 2}, d_2 = {'a': 1, 'c': 3, 'b': 2} d_1 = {'a': 1, 'c': 10, 'b': 2}, d_2 = {'a': 1, 'c': 10, 'b': 2} d_1 = {'a': 1, 'c': 10, 'b': 2}, d_2 = {'e': 12, 'f': 15}
class Point: def init(self, x, y): self.x = x self.y = y def str(self): return ''.join(['x = ', str(self.x), ' ', 'y = ', str(self.y)]) p_1 = Point(12,34) p_2 = p_1 print 'p_1: {0}; p_2: {1}'.format(p_1, p_2) p_2.x = 122 print 'p_1: {0}; p_2: {1}'.format(p_1, p_2) p_2 = Point(89, 978) print 'p_1: {0}; p_2: {1}'.format(p_1, p_2)
p_1: x = 12 y = 34; p_2: x = 12 y = 34 p_1: x = 122 y = 34; p_2: x = 122 y = 34 p_1: x = 122 y = 34; p_2: x = 89 y = 978
函數。
以上是python變數傳遞的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!