4.variables

简介: 变量在python可以是字符也可以是数字。例如:   x = 2 price = 2.5 word = 'Hello'   变量名在等号左边,值在右边,一旦变量被指定,就可以在程序的其他地方使用它。

 

变量在python可以是字符也可以是数字。例如:
 
x = 2
price = 2.5
word = 'Hello'
 
变量名在等号左边,值在右边,一旦变量被指定,就可以在程序的其他地方使用它。
在上面的例子中,我们有三个变 xprice and  word,
变量名中不能有空格和特殊字符。
字符变量有三种定义方法:
 
 
word = 'Hello'
word = "Hello"
word = '''Hello'''
 
这取决于你的喜好,变凉定义之后是可以被替换和修改的。
 
x = 2
 
# increase x by one
x = x + 1
 
# replace x
x = 5
 
Python支持运算符+、-、/ *以及括号。变量可以使用print语句显示在屏幕上。
 
x = 5
print(x)
 
y = 3 * x
print(y)
 
# more detailed output
print("x = " + str(x))
print("y = " + str(y))
 
上面的第一个程序的输出是变量的原始值。这个str()函数将数值变量转换为文本。
目录
相关文章
|
4月前
|
Python
global
global
42 1
|
4月前
global使用和不使用的情况总结
global使用和不使用的情况总结
25 1
|
4月前
|
关系型数据库 MySQL
mysqldump unknown variable ‘set-gtid-purged=off‘ workbench
mysqldump unknown variable ‘set-gtid-purged=off‘ workbench
140 1
UnboundLocalError: local variable ‘loss’ referenced before assignment解决方法
UnboundLocalError: local variable ‘loss’ referenced before assignment解决方法
204 0
成功解决Instructions for updating: Use `tf.global_variables_initializer` instead.
成功解决Instructions for updating: Use `tf.global_variables_initializer` instead.
|
Python
local variable 'xxx' referenced before assignment
定义了一个全局变量 s ,Python的一个函数里面引用这个变量,并改变它的值, 结果报错local variable 'xxx' referenced before assignment, 代码如下: s = 2   def fun():        if s== 2:           print s         s= 3           错误的意思就是s这个变量在引用前还没有定义,这上面不是定义了么?但是后来我把s= 2这句去掉就好了。
5168 0
|
TensorFlow 算法框架/工具
|
关系型数据库 MySQL 数据库
|
移动开发 Shell 应用服务中间件