变量
一个变量存储一个值。
示例
message = "hello python world!"print(message)
一个变量存储一个值。你可以在任何时候改变这个值。
message = "hello python world!"print(message)message = "python is my favorite language!"print(message)
命名规则
1.变量名只能包含字母,数字,下划线。且只能以字母或下划线开头。
2.空格不允许出现在变量名中。
3.不能用python关键字作为变量名。
4.变量名应当是有意义的。不能过短或过长。例如:mc_wheels 就比 wheels 和 number_of_wheels_on_a_motorycle 好。
5.小心使用小写的 l 和大写的 o 。它们容易和1和0混淆。
nameerror
nameerror 是一种常见的变量错误。仔细阅读下述代码,找出哪里出错了:
message = "thank you for sharing python with the world, guido!"print(mesage)
这个错误是由于变量前后不一致导致的。我们只要保证变量名前后一致就能避免这个错误。如下所示:
message = "thank you for sharing python with the world, guido!"print(message)
以上就是python怎么定义变量的详细内容。