Assign Multiple Values
1.Many Values to Multiple Variables
x, y, z = "Orange", "Banana", "Cherry"
2.One Value to Multiple Variables
x = y = z = "Orange"3.Unpack a Collection
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
Output Variables
x = "Python is awesome"
print(x)
In the print()
function, you output multiple variables, separated by a comma:
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
You can also use the +
operator to output multiple variables:
x = "Python "
y = "is "
z = "awesome
print(x + y + z)
Global and local variable
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)

The global Keyword
To create a global variable inside a function, you can use the global
keyword.
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)

在def中沒有寫global ,就屬於local的變數