Home>Article>Backend Development> How to define variables in python
Variables
A variable stores a value.
Example
message = "Hello Python world!" print(message)
A variable stores a value. You can change this value at any time.
message = "Hello Python world!" print(message) message = "Python is my favorite language!" print(message)
Naming rules
1. Variable names can only contain letters, numbers, and underscores. And it can only start with a letter or underscore.
2. Spaces are not allowed in variable names.
3. You cannot use Python keywords as variable names.
4. Variable names should be meaningful. Not too short or too long. For example: mc_wheels is better than wheels and number_of_wheels_on_a_motorycle.
5. Be careful with lowercase l and uppercase O . They are easily confused with 1 and 0.
NameError
NameError is a common variable error. Read the following code carefully to find out what went wrong:
message = "Thank you for sharing Python with the world, Guido!" print(mesage)
This error is caused by inconsistent variables. We can avoid this error as long as we ensure that the variable names are consistent. As shown below:
message = "Thank you for sharing Python with the world, Guido!" print(message)
The above is the detailed content of How to define variables in python. For more information, please follow other related articles on the PHP Chinese website!