Variables
A Python variable is a memory location reserved for storing value in a Python program. So, a variable is a container for the storage of value. In Python, a variable can be created on the go without for declaring it prior.
Example
n = 4
w = 'Biology'
y = "Hello"
To display the above variables, we will use the Python keyword print.
print(n)
Output:
4
print(w)
Output:
Biology
print(y)
Output:
Hello
Tips
While the above variables (n, w, y) follow the naming convention for variables, it is good practice to give meaning names to your variables.
So, we can rename the above variables as:
even_number = 4
subject = 'Biology'
greeting = "Hello"
In Python we don't need to explicitly declare the variable datatype and it can be changed without an issue. We can change the datatype for even_number variable:
even_number = "four"
Single Vs Double Quotes
In the above variable examples, we used both single and double quotes around our string values. Both quotes are the same.
name = "Google"
# it is the same as
name = 'Google'
If our value contains quotes, it matters. For example, we have two values to be stored: Amazon's 2024 revenue & the teacher said "not good enough."
amazon_revenue = "Amazon's 2024 revenue"
message = 'the teacher said "not good enough."'
Because there is a single in Amazon's, we could not enclose the value in single unless we use the backslash (\) to escape the single quote.
amazon_revenue = 'Amazon\'s 2024 revenue'
Naming Convention