Using variables is one of the most powerful features in Python. Use assignment statements to create variables and give them values.
From the chevron prompt in the Python interpreter:
>>> message = “You are doing a good job!”
In the code samples above, two variables were created. Now you could tell Python:
>>> print message
“You are doing a good job!”
You can name variables with almost whatever you want, but it is good practice to give them a name that is meaningful.
The two main rules for naming variables are:
- Do not use a Python reserved word.
- A variable name cannot start with a number.
You need to know how to use operators ‘+, -, *, /, and **’. The operator ‘**’ is for exponentiation.
The order of operations follows the same PEMDAS rules. For example:
>>> print x
Beware of the Division Operator
The division operator might not do what you expect.
>>> print y
Why zero? When both operands are integers, the result is an integer. In other words, the decimal part gets chopped off. To solve this you have to use float types.
One other operator, that is surprising useful, is ‘%’, the modulus operator. It yields the remainder.
>>> print y
Finally, use the ‘+’ operator for concatenation. That is, joining strings together.