Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
数据:类型、值、变量和名称
23
下面这些都是有效名称。
a
a1
a_b_c___95
_abc
_1a
下面这些都是无效名称。
1
1a
1_
name!
another-name
2.6
赋值
Python
中,可以使用
=
为变量赋值。
我们在小学算术中都学过,
=
表示
等于
。那么为什么包括
Python
在内的许多
计算机语言使用
=
进行赋值呢?原因之一是,标准键盘缺乏逻辑上的替代方
案,比如左箭头键,而
=
似乎也没那么让人困惑。另外,在计算机程序中,
赋值操作远比等量测试用得多。
程序不同于代数,你在学校学习数学的时候,看到的方程是这样的:
y = x + 12
可以通过为
x
代入值来解这个方程。如果将
5
代入
x
5 + 12
等于
17
,所以
y
的值是
17
6
代入
x
的话,
y
的值则为
18
,以此类推。
计算机程序语句看起来像方程,但含义完全不同。在
Python
和其他计算机语言中
x
y
变量
Python
知道像
12
5
这样的纯数位序列是整数。下面这个小
Python
程序模拟了
方程,打印出
y
的结果:
>>> x = 5
>>> y = x + 12
>>> y
17
这是数学和程序之间的一个很大的不同。在数学中,
=
代表两侧的
等量关系
;在程序中,
=
则表示
赋值
:将右侧的值赋给左侧的变量。
另外,在程序中,右侧的一切都需要有值(这称为
初始化
)。右侧既可以是字面值,也可
以是已经赋过值的变量,或是二者的组合。
Python
知道
5
12
都是字面整数。第一行将
整数值
5
赋给变量
x
。现在可以在下一行中使用变量 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

可编程网络自动化

可编程网络自动化

Jason Edelman, Scott S. Lowe, Matt Oswalt
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
量子计算机编程:从入门到实践

量子计算机编程:从入门到实践

Eric R. Johnston, Nicholas Harrigan, Mercedes Gimeno-Segovia
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787115586223