【python】Ch10_赋值+表达式+打印

  • 赋值基于对象,就好比液体基于容器,只是倒进去的液体不是属于那个容器的,而是还属于原本倒出来的那个容器的,python 永远会记得这个对象的引用是来自何方,而不是直接把内容复制一个一摸一样的过去。
  • 变量名(容器)在第一次赋值时就会直接被创建,从此之后就可以使用在往后的编程中。
  • 变量名这个名字本身被引用前,首先必须先赋值,就算那个值是空的也无所谓,否则 python 会给你大大的叉。
  • 隐藏式的赋值语句:import, from, def, class, for, 函数参数等等。

赋值语句形式:
    o  基本型:        spam = 'Spam'
    o  元组赋值:     spam, ham = 'yum', 'YUM'
    o  列表赋值:     [spam, ham] = ['yum', 'YUM']
    o  序列赋值:    a, b, c, d = 'spam'
    o  多目标赋值: spam = ham = 'lnch'

    o  增强赋值:    spam += 42

其中第一种最为普遍应用在编程的赋值中,并且后来随着 python 版本不断的更新,赋值方式已经没有那么严格,tuple,list,string 的方式都是可以的。


高级序列赋值语句:

>>> string = 'spam'
>>> a, b, c, d = 'string
>>> a, b, c, d
('s', 'p', 'a', 'm')
# but if the number of the variables are not enough for assignment.. we do this:
>>> a, b, c = string[0], string[1], string[2:]        # index and slice
>>> a, b, c
('s', 'p', 'am')

# this approach generate the same approach
>>> a, b, c = list(string[:2]) + [string[2:]]
>>> a, b, c
('s', 'p', 'am')

# to assign a value into multiple variables at once
>>> a = b = c = 'spam'
>>> a, b, c
('spam', 'spam', 'spam')
# but one thing we should notice when applying this method: 
# if the value assigned to the different variable is changeable, such as []...
# the rest of the variables would be affected by the change of the amount variable like this:
>>> x = y = []
>>> x.append(3)
>>> x, y
([3], [3])

尚有很多方法,但是对于这个小单元中的小单元来说已经过于的花俏了,并且就写 python 在实际中的应用来说已经不实用,向更深入了解的可以执行查找教科书。


增强赋值

x += yx &= yx -= yx |= y
x *= yx ^= yx /= yx >>= y
x %= yx <<= yx **= yx //= y
这么写的一个好处就是可以减少书写量,变量可以是复杂的对象表达式。
上面这些算式的意思,拿 x += y 来举例,他原话就是:x = x + y。当然这在数学上是错误的,但是编程的世界里,他的意思就是,新的 x 会等于旧的 x 加上 y,刷新的概念。


变量名的规则

  • 开头可以是下划线或字母,后面才可以是除了破折号“-”以外的任何字符
  • 大小写对 python 来说是不同的
  • 已经是内置函数的名称是禁止被用来命名变量的,避免搞混。例如:if, pass, def, try...  这类的名字

函数调用的方法

spam(eggs, ham)         # normal function usage
spam.ham(eggs)          # every class has its own attribute and method. this is the way to call the method
a < b and c != b        # condition expression
y < x < z               # setting range expression

表达方式很多中,因地制宜需要自行判断。


打印语句
标准输出流程与原理和 C 语言的 stdout 指令类似,python 的 print 语句是一个人性化特征的写照,也 sys.stdout 模块里面的对象提供了简单的接口,再加上默认的格式设置而成。除了我们最一般常看到的 print 一个结果出来外,我们还可以直接把东西 print 进文件里面哦!

>>> import sys
>>> sys.stdout.write(str(x) + '\n')
>>> print(x)        # both these lines of code are same

# or we can change the directory for the result of print function by this approach:
>>> sys.stdout = open('log.txt', 'a')
>>> print(x, y, x)        # and the result would be shown in the file "log.txt“

这种设置输出的技巧在使用前如果我们就知道该把结果输出到哪里的话,就可以直接跳过 print 的过程,反而比较省事。

但是如果我们引入了 sys module 之后要还原本来的 print 设定也很简单,就是把 sys.stdout 这个 function 关掉即可,代码就是 sys.stdout.close(),一旦关掉后,print 就恢复正常了!

特别注意 print 与stdout 的等效关系,print 只是传送文本给 sys.stdout.write 方法的工具,但是 .write 这个方法有除了纯粹打印之外的更多 print 不具备的功能,如果需求明确,.write 方法可能是更适合编码人员的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值