Variables Flashcards
What is an example of a variable?
x = 5
y = “jhon”
print (x)
print (y)
what is another example of a variable?
x = 5
y = 6
z = x + y
print (z)
How are variables sensitive?
They are case sensitive.
What must a variable start with?
a letter or an underscore character
how do you create a multi variable?
x, y, z = (“Orange”, “Banana”, “Cherry”)
print(x)
print(y)
print(z)
how do you assign the same value to one variable?
x = y = z = (“Orange”)
print(x)
print(y)
print(z)
output = orange
orange
orange
How do you unpack a collection?
fruits = [“apple”, “banana”, “cherry”]
x, y, z, = fruits
print (x)
print (y)
print (z)
output = apple
banana
cherry
How do you output a variable?
x = “Print me”
print (x)
How do you output multiple variables?
With a comma
x = 5
y = “John”
print(x, y)
What are variables that are created outside of a function?
Global variables
What is an example of a global function?
x = “awesome”
def myfunc():
print(“Python is “ + x)
myfunc()
What happens when you create a variable with the same name inside a function?
Creates a local variable
What is an example of creating a local variable?
x = “awesome”
def myfunc():
x = “fantastic”
print(“Python is “ + x)
myfunc()
print(“Python is “ + x)
How do you create a global variable within a function?
def myfunc():
global x
x = “fantastic”
myfunc()
print(“Python is “ + x)
How do you CHANGE a global variable inside a function?
x = “awesome”
def myfunc():
global x
x = “fantastic”
myfunc()
print(“Python is “ + x)