Skip to Content
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
Explore Skills
Cloud Computing
Microsoft Azure
Amazon Web Services (AWS)
Google Cloud
Cloud Migration
Cloud Deployment
Cloud Platforms
Data Engineering
Data Warehouse
SQL
Apache Spark
Microsoft SQL Server
MySQL
Kafka
Data Lake
Streaming & Messaging
NoSQL Databases
Relational Databases
Data Science
Pandas
R
MATLAB
SAS
D3
Power BI
Tableau
Statistics
Exploratory Data Analysis
Data Visualization
AI & ML
Generative AI
Machine Learning
Artificial Intelligence (AI)
Deep Learning
Reinforcement Learning
Natural Language Processing
TensorFlow
Scikit-Learn
Hyperparameter Tuning
MLOps
Programming Languages
Java
JavaScript
Spring
Python
Go
C#
C++
C
Swift
Rust
Functional Programming
Software Architecture
Object-Oriented
Distributed Systems
Domain-Driven Design
Architectural Patterns
IT/Ops
Kubernetes
Docker
GitHub
Terraform
Continuous Delivery
Continuous Integration
Database Administration
Computer Networking
Operating Systems
IT Certifications
Security
Network Security
Application Security
Incident Response
Zero Trust Model
Disaster Recovery
Penetration Testing / Ethical Hacking
Governance
Malware
Security Architecture
Security Engineering
Security Certifications
Design
Web Design
Graphic Design
Interaction Design
Film & Video
User Experience (UX)
Design Process
Design Tools
Business
Agile
Project Management
Product Management
Marketing
Human Resources
Finance
Team Management
Business Strategy
Digital Transformation
Organizational Leadership
Soft Skills
Professional Communication
Emotional Intelligence
Presentation Skills
Innovation
Critical Thinking
Public Speaking
Collaboration
Personal Productivity
Confidence / Motivation
Features
All features
Verifiable skills
AI Academy
Courses
Certifications
Interactive learning
Live events
Superstreams
Answers
Insights reporting
Radar Blog
Buy Courses
Plans
Sign In
Try Now
O'Reilly Platform
book
Python语言及其应用(第2版)
by
Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from
Python语言及其应用(第2版)
42
第
4
章
if
语句
如果,在众人失去理智归咎于你时,你依然能镇定自若……
——
Rudyard Kipling
,
《如果》
在前几章,你已经见过了很多数据示例,但并未对其做太多的处理。使用交互式解释器的
代码示例大多很短。本章会学习如何组织
Python
代码,而不仅仅是数据。
许多计算机语言使用花括号(
{
和
}
)等字符或关键字(比如
begin
和
end
)来标记部分代
码。在这些语言中,一种好的做法是使用一致的缩进来提高代码的可读性,以方便自己和
他人。甚至还有一些工具可以帮助你格式化代码。
在设计
Python
时,
Guido van Rossum
认为缩进本身已经足以定义一个程序的结构,用不着
那些圆括号和花括号。
Python
使用
空白字符
来定义程序结构的做法并不常见。这是新人最
先要注意的一个方面,而对于有其他语言经验的人来说,会觉得这样很怪异。事实证明,
在编写了一段时间的
Python
代码后
,一切就成了自然而然的事了。你甚至会习惯于用省下
来的时间做点儿别的事。
本书最初的代码示例都是一些单行代码。下面来看看如何编写注释和多行代码。
4.1
使用
#
注释
注释
是程序中会被
Python
忽略的文本片段
。你可以使用注释表明周边代码的意图,记录将
来要修改的地方,或是写下想写的任何东西。
Python
使用
#
字符标记注释,从
#
开始到当
前行结束的部分都属于注释。你可以把注释作为单独的一行,如下所示:
>>> # 60
秒
/
分钟
* 60
分钟
/
小时
* 24
小时
/
天
>>> seconds_per_day = 86400
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.
Start your free trial
You might also like
Python编程入门与实战
Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南
Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程
保罗·J·戴特尔
Python面向对象编程指南
Posts & Telecom Press, Steven F. Lott
Publisher Resources
ISBN: 9787115586223