Python Variables
A variable is a container that holds a specific type of data that can be further referenced and manipulated during the execution of the program.
Usually, the two main operations that we perform on variables are:
- Access its value
- Assign it a new value
Unlike other programming languages such as Go, C, C++, etc., we do not declare a variable in Python; we assign them, meaning the variable is created when the value is assigned to the variable.
In Python, variables do not have associated data types; instead, the values do. So we can say that you don't create a variable in Python to hold a specific type.
For example:
number = 5
print(type(number))
text = "Hello World!!!"
print(type(text))
Output
<class 'int'>
<class 'str'>
In the above example, we have created two variables named number
and text
and assigned the values as follows.
- For the
number
variable, we have assigned an integer value - For the
text
variable, we have assigned a string value
Creating variables in Python
The Syntax for creating a variable is:
variable_name = value
In the above example, the left part is the variable name followed by the assignment operator =
and the value. The value could be anything like string
, integer
, boolean
etc.
Assigning values to variables
As you already learned from Syntax, we can assign a value to the variable using the assignment operator =
. Let us look at examples of assigning various types of values.
num = 1
print(
"The variable has a value {value} and the type is {type}".format(
value=num, type=type(num)
)
)
text = "hello world"
print(
"The variable has a value {value} and the type is {type}".format(
value=text, type=type(text)
)
)
floating_number = 22.33
print(
"The variable has a value {value} and the type is {type}".format(
value=floating_number, type=type(floating_number)
)
)
isEnabled = True
print(
"The variable has a value {value} and the type is {type}".format(
value=isEnabled, type=type(isEnabled)
)
)
Output
The variable has a value of 1, and the type is <class 'int'>
The variable has a value hello world, and the type is <class 'str'>
The variable has a value of 22.33, and the type is <class 'float'>
The variable has a value of True, and the type is <class 'bool'>
Since Python is an type-inferred language, you do not associate the data type with the variable. Instead, Python automatically knows based on the value you assign.
Changing the value of the variable
Notice that for the same variable, we have assigned different value types in our example, and this is absolutely fine in Python because we do not assiocate the data types to variables.
text = "Welcome to Python Programming"
print(text)
print(type(text))
text = 12345
print(text)
print(type(text))
text = True
print(text)
print(type(text))
Output
Welcome to Python Programming
<class 'str'>
12345
<class 'int'>
True
<class 'bool'>
Assigning multiple values to multiple variables
If you want to assign the same value to multiple variables at once, we can do this as:
a = b = c = 1
print(a)
print(b)
print(c)
If you want to assign multiple different values to different variables, we can do this as:
x, y, z = 10, "Python", True
print(x)
print(y)
print(z)
Here we are assigning x = 10
, y = "Python"
and z = True
Variable Naming Guidelines
There are specific guidelines in Python when you name a variable. Let us look at these guidelines.
- Variables names can be a combination of characters, digits, and underscore. The characters include both uppercase [A-Z] and lowercase [a-z], digits include [0-9] and underscore [ _ ].
- Variables names cannot start with a number, but they can begin with an underscore.
- Variable names cannot contain spaces in between. If you want to separate the words into variables, use underscore.
- You cannot use the built-in keywords as a variable name.
# Allowed
count = 1
first_name = "Jack"
age_below_30 = True
_score = 30
# Not Allowed
1abc = "Hello"
last name = "Bing"
class = "7th"
Variable Naming Best Practices
- Python prefers a snake case for creating variable names.
- Create the variable names in all lowercase. Eg:
var1
,count
etc - In case of multiple words, separate them by an underscore. For Eg:
my_name
,employee_id
etc - Ensure to have a meaningful name for the variable. Avoid single-character variable names.
# Good Example
count = 1
first_name = "Jack"
age_below_30 = True
maths_score = 30
# Bad Example
TEXT = "Welcome" # use uppercase only for constants
EmployeeID = 344
var1 = "Bing"
a = "Joe"