create variable
Variables are containers for data values.
Unlike other programming languages, Python does not have a command to declare variables.
A variable is not created until it is assigned a value for the first time.
example
x = 10
y = "Bill"
print(x)
print(y)
run instance
Variables do not need to be declared with any specific type, and their type can even be changed after setting.
example
x = 5 # x is of type int
x = "Steve" # x is now of type str
print(x)
run instance
String variables can be declared using single or double quotes:
example
x = "Bill"
# is the same as
x = 'Bill'
run instance
variable name
Variables can have short names (such as x and y) or more descriptive names (age, carname, total_volume).
Python variable naming rules:
- Variable names must start with a letter or the underscore character
- Variable names cannot start with a number
- Variable names can only contain alphanumeric characters and underscores (Az, 0-9, and _)
- Variable names are case sensitive (age, Age, and AGE are three distinct variables)
Remember that variable names are case sensitive
Assign values to multiple variables
Python allows you to assign values to multiple variables in one line:
example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
run instance
Multiple variables can be assigned the same value in one line:
example
x = y = z = "Orange"
print(x)
print(y)
print(z)
run instance
output variable
Python's print statement is often used to output variables.
To combine literals and variables, Python uses the + character:
example
x = "awesome"
print("Python is " + x)
run instance
You can also add a variable to another variable using the + character:
example
x = "Python is "
y = "awesome"
z = x + y
print(z)
run instance
For numbers, the + character is used as a mathematical operator:
example
x = 5
y = 10
print(x + y)
run instance
If you try to combine strings and numbers, Python gives an error:
example
x = 10
y = "Bill"
print(x + y)
run instance
global variable
Variables created outside a function (as in all the above examples) are called global variables.
Global variables can be used by everyone inside and outside the function.
example
Create the variable outside the function and use it inside the function:
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
run instance
If you create a variable with the same name inside a function, that variable will be local and can only be used inside the function. Global variables with the same name will be left untouched, with their original values.
example
Create a variable inside the function with the same name as the global variable:
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
run instance
global keyword
Normally, when a variable is created inside a function, that variable is local and can only be used inside that function.
To create a global variable inside a function, the global keyword can be used.
example
If the global keyword is used, the variable belongs to the global scope:
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
run instance
Also, if you want to change a global variable inside a function, use the global keyword.
example
To change the value of a global variable inside a function, use the global keyword to refer to the variable:
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
run instance