Python Variables
- Previous Page Python Comments
- Next Page Python Data Types
Create variables
Variables are containers for storing data values.
Unlike other programming languages, Python does not have a command to declare variables.
Variables are created only when they are first assigned a value.
Example
x = 10 y = "Bill" print(x) print(y)
Variables do not need to use any specific type declaration, and even their types can be changed after they are set.
Example
x = 5 # x is of type int x = "Steve" # x is now of type str print(x)
String variables can be declared using single or double quotes:
Example
x = "Bill" # is the same as x = 'Bill'
Variable name
Variables can use 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 underscore character
- Variable names cannot start with a number
- Variable names can only contain alphanumeric characters and underscores (A-z, 0-9, and _)
- Variable names are case-sensitive (age, Age, and AGE are three different variables)
Remember that variable names are case-sensitive
Assigning to multiple variables
Python allows you to assign multiple variables in one line:
Example
x, y, z = "Orange", "Banana", "Cherry" print(x) print(y) print(z)
You can assign the same value to multiple variables in one line:
Example
x = y = z = "Orange" print(x) print(y) print(z)
output variables
Python's print
statement is usually used to output variables.
To combine text with variables, Python uses +
Character:
Example
x = "awesome" print("Python is " + x)
You can also use the + character to add a variable to another variable:
Example
x = "Python is " y = "awesome" z = x + y print(z)
For numbers,+
Characters are used as mathematical operators:
Example
x = 5 y = 10 print(x + y)
If you try to combine strings and numbers, Python will give an error:
Example
x = 10 y = "Bill" print(x + y)
Global Variables
Variables created outside the function (as shown in all the above examples) are called global variables.
Global variables can be used by everyone inside and outside the function.
Example
Create a variable outside the function and use it inside the function:
x = "awesome" def myfunc(): print("Python is " + x) myfunc()
If you create a variable with the same name inside a function, then the variable will be a local variable and can only be used within the function. The global variable with the same name will remain unchanged and retain its original value.
Example
Create a variable with the same name as a global variable inside a function:
x = "awesome" def myfunc(): x = "fantastic" print("Python is " + x) myfunc() print("Python is " + x)
Global Keyword
Generally, when creating a variable inside a function, the variable is a local variable and can only be used within that function.
To create a global variable inside a function, you can use the global keyword.
Example
If you use the global keyword, then the variable belongs to the global scope:
def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x)
Additionally, if you want to change a global variable inside a function, please use the global keyword.
Example
To change the value of a global variable inside a function, please use the global keyword to refer to that variable:
x = "awesome" def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x)
- Previous Page Python Comments
- Next Page Python Data Types