Variables
Assigning Variables
A variable is defined as a label that can be used to hold a value. A variable holds one value in memory that can be retrieved many times. For example:
age = 25 print(age)
>> 25
In the execution above, we assigned a value of 25
to the name age
and we used the assignment operator =
which assigns the value on the right to the name on the left.
Now to assign values to multiple names, we can stack variable declarations like so:
age = 25 name = "John" gender = "male" print(age, name, gender)
>> 25 John male
Python also allows you to do this in a more concise way using comma operators:
age, name, gender = 25, "John", "male" print(age, name, gender)
>> 25 John male
That is, age
name
and gender
are respectively equal to 25
John
and male
.
It is recommended to use this notation with closely related variables. In our case, age
name
and gender
represent characteristics of a person.
Try to keep your names as descriptive as possible to ensure that you're always aware of what's going on in your code.
Reassigning Variables
Let's say I already have the variable
age = 30 print(age)
>> 30
Now five years later I would like to reassign my age
age = 30 age = 35 print(age)
>> 35
The old value will be erased from memory and replaced by the new value 35
.
Values can also be assigned to themselves:
age = 30 age = age + 5 print(age)
>> 35
In this case, we assigned age to itself, which holds the old value 30, and then we added the value 5 to it. The old value of age was then replaced by the new value 35
.
the +=
Operator
Another way of writing the above declaration is using the **+=**
operator. This is more concise, as it allows you to avoid writing the variable name twice:
age = 30
age += 5
print(age)
>> 35
This notation is identical to the previous notation. It takes the pre-existing value of the variable age
and increments it by 5
, producing 35
.
the -=
Operator
Another operator is the -=
operator, this operator, as you guessed it, decrements your variable in a more concise way.
age = 30 age -= 5 print(age)
>> 25
Things to avoid
- Avoid Spaces in your variable name
You should also never have spaces in your variable names.
my blood type = "AB"
>> <ERROR>
The following syntax is invalid. In such a case, you would want to use an underscore to separate the words.
my_blood_type = "AB"
- Avoid using special characters in your variable name:
you should avoid using special characters $
, %
, !
in your variable name as this will also create errors.
name!@ = "John"
>> <ERROR>
- Avoid Camel Case
Camel Case is a very popular way of writing syntax in many programming languages. However, it is not conventional to do so in Python and should be avoided. All variable names should be lower case.
This Should Be Avoided
myBloodType = "AB"
- Avoid starting variables with numbers
You should avoid starting variables with numbers as this will render errors in your execution.
1234name = "John"
>> <ERROR>
- Avoid reserved words in python
You should avoid using reserved words as variable names. These words carry a special purpose in the Python programming language. Some of these words include: import
, return
, while
...
here is a list of all reserved words in the python programming language:
https://www.tutorialspoint.com/What-are-Reserved-Keywords-in-Python