
Variables
Variables in Python are used to store data values. They act as containers that can hold values and be referenced by a name.
Assigning Values to Variables
In Python, the equal sign =
is used to assign values to variables.
# Assigning a value to a variablex = 120
In this example, the value 120 is assigned to the variable x
using the equal sign =
. After this assignment, the variable x
holds the value 120.
Using print(x)
will display the value stored in the variable x
:
# Print the variable xprint(x)
Output:
120
To change a variable's value, simply assign it a new value. The new value will override the previous one.
# Change x's value to a string hellox = 'hello'print(x)
Output:
hello
Variables Names
When naming variables, follow these basic rules:
- Variable names must start with a letter or a underscore character. Variable names can't start with a number.
- Variable names can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
- Variable names are case-sensitive.
- A variable name cannot be any of th Python keywords.
Python Keywords List
Python keywords are reserved words that have special meanings and are used to define the syntax and structure of the language. These keywords cannot be used as identifiers (such as variable names or function names) in Python code. To obtain the list of keyowrds, you can use the 'keyword' module in Python:
# Import the keyword moduleimport keyword
# Print the list of Python keywordsprint(keyword.kwlist)
Output:
['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Example of invalid variable names
# Variable names can't start with numbers1var = "Hello"
Output:
1var = "Hello" ^SyntaxError: invalid syntax
# Variable names can't include hyphen -var-name = "Hello"
Output:
var-name = "Hello" ^SyntaxError: cannot assign to operator
# Variable names can't have a whitespacevar name = "Hello"
Output:
var name = "Hello" ^SyntaxError: invalid syntax
While the error messages vary, they all stem from illegal variable names that lead to errors.
Example of valid variable names
# Variable names can be all lowercase lettersvar = "Hello"
# Capital letters can be included in any part of a variable nameVar = "Hello"varName = "Hello"
# Variable names can be entirely in capital lettersVAR = "Hello"
# Numbers can be included in any part of a variable name except for the startvarname1 = "Hello"
# Variable names may contain underscores at the start, within, or at the end_var_name = "Hello"var_name = "Hello"var_name_ = "Hello"
Assign Multiple Values
You can assign many values to multiple variables in one line:
# Assign values to multiple variables in one linex, y, z = 1, 2, 3
print(x)print(y)print(z)
Output:
1
2
3
Or you can assign one value to multiple variables:
# Assign one value to multiple variables in one linex = y = z = "Hello"
print(x)print(y)print(z)
Output:
Hello
Hello
Hello
But you cannot assign multiple values to a single variable simultaneously. Instead, each assignment statement replaces the previous value. Consequently, the variable retains only the value assigned in the last assignment.
x = 1x = 2x = 3
print(x)
Output:
3
Now that we've laid the foundation of Python, including the print()
method and variables, we can move on to learning about Data Types and Data Structures. See you in the next lesson!